#4034 Cache Type instances in TypeFactory.getType(...) to avoid redundant work - #4129
Open
AnkitaAdvitot wants to merge 1 commit into
Open
AnkitaAdvitot wants to merge 1 commit into
AnkitaAdvitot wants to merge 1 commit into
Conversation
…oid redundant work
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4034.
Background & Motivation
In MapStruct's annotation processor,
TypeFactory.getType(...)is invoked frequently across rounds and model element processors (mapping methods, source/target property resolution, accessor discovery, enclosing element inspection). Previously,TypeFactoryinstantiated a freshTypeobject on nearly every invocation:typeUtils.isSubtypeErased(...)checks.Typememoized caches (such asisNullMarked(), accessors, and method lists) were repeatedly re-allocated or re-queried instead of shared.Solution
typeElementCache(IdentityHashMap<TypeElement, Type>) inTypeFactoryto intern prototypical types derived directly from non-genericTypeElementinstances.typeCache(HashMap<TypeCacheKey, Type>) using an immutable structural fingerprint (TypeFingerprint) that safely distinguishes:TypeElement, type argument fingerprints, and enclosing type fingerprint).TypeKind).isLiteralandalwaysImport.canBeProcessed(mirror)ensures erroneous/unresolvable types are never cached or suppressed, preservingTypeHierarchyErroneousExceptionsemantics.Issue4034TestandIssue4034Mapperverifying that identicalTypeinstances are returned for repeated lookups (getType(TypeElement),getType(TypeMirror), genericList<String>), differing type arguments (List<String>vsList<Integer>) produce distinct instances, and flag variants (isLiteral,alwaysImport) remain distinct.