/ jruby Public
Rework the Regexp cache to be a simple WeakHashMap #2188
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.
This means that cache keys are shorter-lived than the SoftReferences used before, but it will
allow for the rapid expiration of memory-hungry one-shot regexes as well.
Implemented per discussion with @headius in IRC.
Testing with the following script demonstrates the pathological case in master, and the much improved handling with the WeakRefCache:
I think the issue here is that when there are a large number of keys in the caches, the overhead of the actual ConcurrentHashMap currently used to back the cache becomes prohibitive and starves the JRuby process. This results in something like this (and JRuby either stalls or crashes once it gets into this boundary behavior):
However, with the WeakHashMap cache, behavior is more like this (and the test finishes in ~9 sec):