Issue #2116: Catalog initialization from plugin leads to lots of contention#2120
Issue #2116: Catalog initialization from plugin leads to lots of contention#2120nick-at-finix wants to merge 1 commit intokillbill:masterfrom
Conversation
9e48ef4 to
08b7874
Compare
|
|
||
| private static final Map<Class<?>, LinkedList<Field>> perCatalogClassNonRequiredFields = new HashMap<>(); | ||
| // Cache of prototype instances with default values already set, avoids repeated reflection | ||
| private static final Map<String, Object> prototypeCache = new ConcurrentHashMap<>(); |
There was a problem hiding this comment.
It is a lot faster to hash a String instead of an entire Class. Also - this collection never gets larger than 23 entries in our testing, so that size of this Map should not be a concern.
| /** | ||
| * Applies values from the prototype to null fields in the target object for minimal reflection usage. | ||
| */ | ||
| private static void applyPrototypeValues(Object prototype, Object target) throws IllegalAccessException { |
There was a problem hiding this comment.
This avoids all subsequent annotation checking, value determinations, and type checks for a given Catalog Object.
| private static final Map<String, Object> prototypeCache = new ConcurrentHashMap<>(); | ||
|
|
||
| // Cache for zero-length arrays | ||
| private static final Map<Class<?>, Object> zeroLengthArrayCache = new ConcurrentHashMap<>(); |
There was a problem hiding this comment.
No need to keep creating these either, there are 9 array types that end up in here.
| */ | ||
| private static void applyPrototypeValues(Object prototype, Object target) throws IllegalAccessException { | ||
| for (final Field field : target.getClass().getDeclaredFields()) { | ||
| if (!field.trySetAccessible()) { |
There was a problem hiding this comment.
Java 9+ has Field#trySetAccessible, which restricts the change in access to the calling method, and doesn't require us to change it back.
|
If this approach is generally acceptable I will clean this up, add some logging, and additional checks. |
|
Hm, over time this is creating a large number of fields: I will iterate on this approach and improve |
Different approach from the previous pull request, completely eliminates contention in CatalogSafetyInitializer for our usages.