-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Reviewed some Quarkus core classes #40630
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very nice!
IGNORED_PROPERTY_ANNOTATIONS = ignoredPropertyAnnotations.toArray(new Class[0]); | ||
RECORDABLE_CONSTRUCTOR_ANNOTATIONS = recordableConstructorAnnotations.toArray(new Class[0]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As we're making things more efficient, we can make new Class[0]
a constant, no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd say it's unnecessary - compiler knows this pattern well. Also, any static constant takes memory forever :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
compiler knows this pattern well
kk
@@ -43,7 +43,7 @@ public Property[] apply(Class<?> type) { | |||
if (existingGetter == null || existingGetter.getReturnType().isAssignableFrom(i.getReturnType())) { | |||
getters.put(name, i); | |||
} | |||
} else if (i.getName().startsWith("is") && i.getName().length() > 3 && i.getParameterCount() == 0 | |||
} else if (i.getName().startsWith("is") && i.getName().length() > 2 && i.getParameterCount() == 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
static final ConcurrentMap<Class<?>, Property[]> CACHE = new ConcurrentHashMap<>(); | ||
|
||
private static final Function<Class<?>, Property[]> FUNCTION = new Function<Class<?>, Property[]>() { | ||
private static final ClassValue<Property[]> CACHE = new ClassValue<Property[]>() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
N.B. I'm not sure the original code would leak, but this seems much safer to me. I'm referring to classloader leaks in case there's a recorder triggered as this is only capturing properties of recorders so I don't think it would happen.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup
f682f4f
to
92e5a67
Compare
|
||
public FieldsHelper(final Class<?> aClass) { | ||
final Field[] declaredFields = aClass.getDeclaredFields(); | ||
this.fields = new HashMap<>(declaredFields.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure it's a good idea to do that, your Map
will be resized always given the default loadFactor
of 0.75
.
In HV, we had a util who correctly created a Map of the right size for all the maps initialized at bootstrap: https://github.com/hibernate/hibernate-validator/blob/729cd018599d046aa5db1dbe35700521dd596c4e/engine/src/main/java/org/hibernate/validator/internal/util/CollectionHelper.java#L142-L147 .
And sure, it's a micro optimization but in your case, you might create more harm than none by always sizing the map a bit too small.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what are you suggesting, would you prefer I simply use new HashMap<>()
; ?
FWIW I know about the optimisation in Validator and we had a similar thing in ORM, but it's not taking into account the likelyhood of hash collisions.. that's why there is a load factor in the hashmap implementation: it's hard to estimate perfectly. One could argue that the default loadfactor is a bit low.. My rule of thumb is currently that as long as we resize just a couple times it's fine, so I like passing the estimated size (when we have it) so to have it right at least within order of magnitude. I guess since in this case we expect the lifespan of the map to be short one should rather overestimate: I'll shift it right once so to double its size?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW just checked, on recent JVMs the HashMap implementation is different and it looks like that this is no longer a problem.
Might be time to simplify Validator as well :)
This comment has been minimized.
This comment has been minimized.
Something seems way off .. I'll need to separate these. |
92e5a67
to
ad96389
Compare
Status for workflow
|
I had it run again after removing the one fix which was introducing the ClassValue, and it's passing now. So I'll merge these and scrutinize my understanding of ClassValue .. |
Very weird... I'd like to know why ClassValue was problems, if anything I expected to be better :) |
Same. I'll send a PR with that change individually - should have done so from the beginning but didn't want to flood CI (and reviewers) with all the noise from small changes. |
As I was profiling the bootstrap process, I spotted some small inefficiencies and a couple of potential bugs.
Best reviewed by individual commits, as each change is independent - wrapped together as they are all very small changes.