Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ScopeFactory API #136

Merged
merged 1 commit into from
Dec 4, 2019
Merged

Add ScopeFactory API #136

merged 1 commit into from
Dec 4, 2019

Conversation

Leland-Takamine
Copy link
Collaborator

Resolves #125

Copy link

@andreasnomikos andreasnomikos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving open for pending questions


private ScopeFactory() {}

private static final Map<Class<?>, Constructor<?>> scopeClassToCreateMethod = new HashMap<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this caching layer might be redundant post android 7.0 where the reflection apis where optimized with native invocations. At that point the methods are already cached in the native representation of the class Object so the vm maintains its own cache for them and we don't need to tax the heap with it.
Do you want to spend time to look into that and decide if this needs to be api gated ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting - didn't know about this. Do you remember where you read this or the relevant source code?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

http://androidxref.com/9.0.0_r3/xref/art/runtime/native/java_lang_Class.cc

I guess we will still incur the overhead of the result array
So the tradeoff of the heap cache is that we are taxing the heap with
the map vs allocating a new array every time we need to create a root scope
either to get the result of getDeclaredConstructors or to pass params to getDeclaredConstructor

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it - in this case I'll move forward with the current approach since it's the simplest and safest.

if (constructor == null) {
Class<?> scopeImplClass = getScopeImplClass(scopeClass);
constructor = scopeImplClass.getDeclaredConstructors()[0];
scopeClassToCreateMethod.put(scopeClass, constructor);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this second field access is redundant just hoist it to a local var at the top

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what you mean here.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

scopeClass is a field reference so its doing a double field read

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather tackle this as a post processing step at the binary level if we're concerned about field access performance.

lib/src/main/java/motif/ScopeFactory.java Outdated Show resolved Hide resolved
private static Class<?> getScopeImplClass(Class<?> scopeClass) {
String scopeImplClassName = getScopeImplClassName(scopeClass);
try {
return Class.forName(scopeImplClassName, false, scopeClass.getClassLoader());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason that you avoid the initialization here?
If it hasn't been done already It will probably have to do it anyway immediately on the new instance
unless you are considering warming the cache separately for some reason.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which initialization are you referring to here?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the class initialization controlled by the false parameter

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha - will update

lib/src/main/java/motif/ScopeFactory.java Outdated Show resolved Hide resolved

// Inspired by https://github.com/square/javapoet/issues/295
private static String getScopeImplClassName(Class<?> scopeClass) {
StringBuilder sb = new StringBuilder();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smarter init value than 16 chars ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be a better initial value?


private static final Map<Class<?>, Constructor<?>> scopeClassToCreateMethod = new HashMap<>();

public static <S extends Creatable<NoDependencies>> S create(Class<S> scopeClass) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@andreasnomikos @ugonna @Ericliu001 Heads up that I've added this convenience API for Scopes that don't have any dependencies. Let me know if you have any concerns with this.

} else {
return (S) constructor.newInstance(dependencies);
}
} catch (IllegalAccessException e) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're handling them all the same, do you not want to:

try {
} catch (IllegalAccessException | InvocationTargetException | InstantiationException e) {}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would implicitly bump the min-sdk to 19:
image

While it may make sense to only support SDK 19 and up, I'd rather make that a conscious decision rather than forcing that limitation to optimize just this piece of code.

private static final Map<Class<?>, Constructor<?>> scopeClassToCreateMethod = new HashMap<>();

public static <S extends Creatable<NoDependencies>> S create(Class<S> scopeClass) {
return create(scopeClass, NO_DEPENDENCIES);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if someone sends in a Creatable with a non-zero arg constructor to this method? We would be passing NO_DEPENDENCIES to that constructor. That's legal, but incorrect usage. Do we want to warn or fail against that case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The class that we end up instantiating is the ScopeImpl associated with the Scope class that is passed in. If someone passes in a Creatable that is not a Scope then we will fail to find the ScopeImpl and crash at runtime. We could add an explicit error if the passed class is not annotated with @Scope but that adds some runtime overhead. Might be worth it - we can discuss this as a potential follow up.

@Leland-Takamine Leland-Takamine changed the base branch from creatable to master December 3, 2019 23:41
@Leland-Takamine Leland-Takamine merged commit 09b1c95 into master Dec 4, 2019
@Leland-Takamine Leland-Takamine deleted the scope-factory branch December 4, 2019 00:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Creatable / ScopeFactory Proposal
4 participants