Skip to content

TypeFactory cache performance degradation with constructSpecializedType() #3876

@carterkozak

Description

@carterkozak

Describe the bug
TypeFactory cache interactions taking seconds in real-world scenarios under heavy load (e.g. Jersey ProviderBase, where 'constructSpecializedType' is used) where the operation itself takes microseconds.

TypeFactory.constructSpecializedType adds elements to the cache which contain PlaceholderForType type arguments. These type arguments all have the same hashCode (based on Object.class plus a hard-coded value for all placeholders) while overriding equals to perform reference equality. Each time that constructSpecializedType is called, more data is added to the cache with the same hash code, but equality never succeeds due to instances differing, not only reducing cache hit probability, but making it increasingly difficult to find the values we want due to following pointers on heavy cache collisions where the data structure degenerates into a linked list.
I've committed a change which bypasses the cache in cases where PlaceholderForType is used. I don't know if this is an ideal solution, but it improves the case where I've encountered this edge case.

Version information
Impacts versions prior to 2.14.0, but the issue becomes more pronounced in 2.14.0 due to #3531 which doesn't simply clear the map upon reaching max size. #3675 seems to make the issue even more pronounced in some environments due to heavy hash collisions.

To Reproduce
Reproducer can be found here: FasterXML/jackson-benchmarks#5

PR here: #3875

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions