perf: handle some concurrency in ModelSet #3736
Merged
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.
Reading the model sometimes leads to it's modification: for example,
CtTypeReferenceImpl.getTypeDeclaration()
may start building a shadow type which is then added to the model. This causes modification of thetypes
ModelSet in CtPackageImpl, which is backed by a TreeSet (which is in turn backed by a TreeMap). Thus a TreeMap can be modified concurrently when multiple threads try to get a type declaration (or an executable declaration), which sometimes causes an infinite loop.This fix is mostly "treating the symptoms" and it doesn't provide sufficient synchronization across the library so ConcurrentModificationException is still possible, but it at least prevents the infinite loop.
This fix doesn't seem to have too much impact on performance (at least in the "real-world scenario" of building the model by a Launcher and then reading it by multiple threads).