Repository navigation
[JSC] shrinkFootprintNow(KeepCodeInUse) can release the code of RegExps that are not in use (off by default) - #633
Merged
Jarred-Sumner merged 1 commit intoSep 14, 2026
Conversation
Jarred-Sumner
force-pushed
the
claude/jsc-cold-code-memory
branch
from
September 12, 2026 06:48
209144a to
affb84c
Compare
Jarred-Sumner
force-pushed
the
claude/regexp-release-idle-code
branch
from
September 12, 2026 06:56
a45f8e3 to
83d0bac
Compare
Preview Builds
|
Jarred-Sumner
marked this pull request as ready for review
September 12, 2026 08:17
Jarred-Sumner
force-pushed
the
claude/jsc-cold-code-memory
branch
from
September 13, 2026 01:24
affb84c to
72ea768
Compare
…ps that are not in use After an interactive session of a large bundled CLI application the executable pool holds 5.0 MB of allocated code (6.7 MB of committed pages) two minutes after the last input, and 3.7 MB (4.7 MB) after the deep-idle VM::shrinkFootprintNow(KeepCodeInUse). By owner: Yarr code of 580 RegExps 3.85 MB (6.6 KB each), Baseline 0.56 MB in use + 0.60 MB cached on unlinked code blocks whose CodeBlocks are gone (released by its lease before the shrink), DFG 0.22 MB. The pool itself is fine: MetaAllocator decommits a page when its last allocation dies and only 1.0-1.7 MB of committed pages are partly used. What stays is RegExp code, which KeepCodeInUse never touches (KeepCodeThatNeedsParsing drops all of it). With Options::releaseIdleRegExpCodeWhenShrinkingFootprint (off by default) the KeepCodeInUse shrink calls RegExp::deleteCode() on every RegExp in the RegExpCache that has not matched since the last full collection began. The embedder shrinks after the program has been at rest well beyond its last idle collection, so that means "not during the quiet period"; a RegExp on a timer keeps its code. A RegExp whose code was dropped is in the NotCompiled state, exactly as after VM::deleteAllRegExpCode(), and compiles again from its pattern when it next matches. - In use: RegExp::compileIfNecessary[MatchOnly], which every match that runs the matcher goes through first (one that the minimum-length filter rejects does not count), stores the low byte of the heap's marking version (one per full collection) in the new RegExp::m_lastUseEpoch. A stale byte that happens to equal the current one after 256 full collections only keeps some code for another round. The byte takes one of the three bytes of padding between m_constructionErrorCode and m_numSubpatterns; RegExp stays in the 80-byte size class. - Optimized code: DFG and FTL never call a RegExp's Yarr code by address. RegExpExec/RegExpTest/... call operations that end up in RegExp::match; RegExpTestInline emits a fresh copy of the matcher into the DFG/FTL code (Yarr::jitCompileInlinedTest from SpeculativeJIT::compileRegExpTestInline and FTL::LowerDFGToB3::compileRegExpTestInline), built from the pattern; the get*MatchAddr() accessors of YarrCodeBlock are only used to print. The first-character bitmap that the fast paths in JIT code test belongs to the RegExp, not to its code; m_minimumSize goes back to 0, which sends their length check to the slow path, and a minimum size folded as a constant is a property of the pattern. So no machine code refers to what deleteCode() frees. What the compiler threads do read is the YarrCodeBlock's inline statistics (DFGStrengthReductionPhase, then compileRegExpTestInline asserts getRegExpJITCodeBlockConcurrently()), so the release only happens when VM::numberOfActiveJITPlans() is zero; plans are started by the mutator, i.e. not during the shrink. The KeepCodeThatNeedsParsing shrink gets the same guarantee from Heap::deleteAllCodeBlocks() completing all plans first. - Running matches: shrinkFootprintNow() refuses to run with a VMEntryScope (JS on the stack), and RegExp::match neither re-enters the run loop nor is called by the embedder outside of the API lock, so no Yarr code is on the native stack of this VM when the shrink runs. deleteCode() takes the RegExp's cellLock like every other state change. - RegExpCache: every RegExp is created through RegExpCache::lookupOrCreate() and, unless its pattern has a syntax error (no code then), is in its weak map for as long as it lives; entries whose RegExp is dead are skipped. The small strong cache (recently created patterns) is left alone: it only keeps RegExp cells alive, not their code. The same application, 20 inputs, idle 240 s, 10 more inputs, idle 240 s: executable pool after each shrink 3.67 MB allocated / 4.70 MB committed -> 0.13 / 0.97, RssAnon at rest 3.7-6 MB lower. The second session compiles ~450 RegExps again, and that is not free: user-mode instructions of the whole process, five interleaved pairs, 33.94 G (33.75-34.24) without and 34.91 G (34.46-35.34) with the option, +0.97 G (+2.9 %) for one wake-up. Three RegExps account for most of both the memory and the cost: patterns of 530 KB, 169 KB and 20 KB (machine-generated) compile to 1.97 MB, 0.71 MB and 0.17 MB of code in two variants each; the other 570 RegExps hold 1.0 MB together. Hence off by default.
Jarred-Sumner
force-pushed
the
claude/regexp-release-idle-code
branch
from
September 13, 2026 01:25
83d0bac to
26c7020
Compare
Jarred-Sumner
added a commit
that referenced
this pull request
Sep 14, 2026
…ps that are not in use (#633) After an interactive session of a large bundled CLI application the executable pool holds 5.0 MB of allocated code (6.7 MB of committed pages) two minutes after the last input, and 3.7 MB (4.7 MB) after the deep-idle VM::shrinkFootprintNow(KeepCodeInUse). By owner: Yarr code of 580 RegExps 3.85 MB (6.6 KB each), Baseline 0.56 MB in use + 0.60 MB cached on unlinked code blocks whose CodeBlocks are gone (released by its lease before the shrink), DFG 0.22 MB. The pool itself is fine: MetaAllocator decommits a page when its last allocation dies and only 1.0-1.7 MB of committed pages are partly used. What stays is RegExp code, which KeepCodeInUse never touches (KeepCodeThatNeedsParsing drops all of it). With Options::releaseIdleRegExpCodeWhenShrinkingFootprint (off by default) the KeepCodeInUse shrink calls RegExp::deleteCode() on every RegExp in the RegExpCache that has not matched since the last full collection began. The embedder shrinks after the program has been at rest well beyond its last idle collection, so that means "not during the quiet period"; a RegExp on a timer keeps its code. A RegExp whose code was dropped is in the NotCompiled state, exactly as after VM::deleteAllRegExpCode(), and compiles again from its pattern when it next matches. - In use: RegExp::compileIfNecessary[MatchOnly], which every match that runs the matcher goes through first (one that the minimum-length filter rejects does not count), stores the low byte of the heap's marking version (one per full collection) in the new RegExp::m_lastUseEpoch. A stale byte that happens to equal the current one after 256 full collections only keeps some code for another round. The byte takes one of the three bytes of padding between m_constructionErrorCode and m_numSubpatterns; RegExp stays in the 80-byte size class. - Optimized code: DFG and FTL never call a RegExp's Yarr code by address. RegExpExec/RegExpTest/... call operations that end up in RegExp::match; RegExpTestInline emits a fresh copy of the matcher into the DFG/FTL code (Yarr::jitCompileInlinedTest from SpeculativeJIT::compileRegExpTestInline and FTL::LowerDFGToB3::compileRegExpTestInline), built from the pattern; the get*MatchAddr() accessors of YarrCodeBlock are only used to print. The first-character bitmap that the fast paths in JIT code test belongs to the RegExp, not to its code; m_minimumSize goes back to 0, which sends their length check to the slow path, and a minimum size folded as a constant is a property of the pattern. So no machine code refers to what deleteCode() frees. What the compiler threads do read is the YarrCodeBlock's inline statistics (DFGStrengthReductionPhase, then compileRegExpTestInline asserts getRegExpJITCodeBlockConcurrently()), so the release only happens when VM::numberOfActiveJITPlans() is zero; plans are started by the mutator, i.e. not during the shrink. The KeepCodeThatNeedsParsing shrink gets the same guarantee from Heap::deleteAllCodeBlocks() completing all plans first. - Running matches: shrinkFootprintNow() refuses to run with a VMEntryScope (JS on the stack), and RegExp::match neither re-enters the run loop nor is called by the embedder outside of the API lock, so no Yarr code is on the native stack of this VM when the shrink runs. deleteCode() takes the RegExp's cellLock like every other state change. - RegExpCache: every RegExp is created through RegExpCache::lookupOrCreate() and, unless its pattern has a syntax error (no code then), is in its weak map for as long as it lives; entries whose RegExp is dead are skipped. The small strong cache (recently created patterns) is left alone: it only keeps RegExp cells alive, not their code. The same application, 20 inputs, idle 240 s, 10 more inputs, idle 240 s: executable pool after each shrink 3.67 MB allocated / 4.70 MB committed -> 0.13 / 0.97, RssAnon at rest 3.7-6 MB lower. The second session compiles ~450 RegExps again, and that is not free: user-mode instructions of the whole process, five interleaved pairs, 33.94 G (33.75-34.24) without and 34.91 G (34.46-35.34) with the option, +0.97 G (+2.9 %) for one wake-up. Three RegExps account for most of both the memory and the cost: patterns of 530 KB, 169 KB and 20 KB (machine-generated) compile to 1.97 MB, 0.71 MB and 0.17 MB of code in two variants each; the other 570 RegExps hold 1.0 MB together. Hence off by default.
Jarred-Sumner
added a commit
that referenced
this pull request
Sep 15, 2026
…ps that are not in use (#633) After an interactive session of a large bundled CLI application the executable pool holds 5.0 MB of allocated code (6.7 MB of committed pages) two minutes after the last input, and 3.7 MB (4.7 MB) after the deep-idle VM::shrinkFootprintNow(KeepCodeInUse). By owner: Yarr code of 580 RegExps 3.85 MB (6.6 KB each), Baseline 0.56 MB in use + 0.60 MB cached on unlinked code blocks whose CodeBlocks are gone (released by its lease before the shrink), DFG 0.22 MB. The pool itself is fine: MetaAllocator decommits a page when its last allocation dies and only 1.0-1.7 MB of committed pages are partly used. What stays is RegExp code, which KeepCodeInUse never touches (KeepCodeThatNeedsParsing drops all of it). With Options::releaseIdleRegExpCodeWhenShrinkingFootprint (off by default) the KeepCodeInUse shrink calls RegExp::deleteCode() on every RegExp in the RegExpCache that has not matched since the last full collection began. The embedder shrinks after the program has been at rest well beyond its last idle collection, so that means "not during the quiet period"; a RegExp on a timer keeps its code. A RegExp whose code was dropped is in the NotCompiled state, exactly as after VM::deleteAllRegExpCode(), and compiles again from its pattern when it next matches. - In use: RegExp::compileIfNecessary[MatchOnly], which every match that runs the matcher goes through first (one that the minimum-length filter rejects does not count), stores the low byte of the heap's marking version (one per full collection) in the new RegExp::m_lastUseEpoch. A stale byte that happens to equal the current one after 256 full collections only keeps some code for another round. The byte takes one of the three bytes of padding between m_constructionErrorCode and m_numSubpatterns; RegExp stays in the 80-byte size class. - Optimized code: DFG and FTL never call a RegExp's Yarr code by address. RegExpExec/RegExpTest/... call operations that end up in RegExp::match; RegExpTestInline emits a fresh copy of the matcher into the DFG/FTL code (Yarr::jitCompileInlinedTest from SpeculativeJIT::compileRegExpTestInline and FTL::LowerDFGToB3::compileRegExpTestInline), built from the pattern; the get*MatchAddr() accessors of YarrCodeBlock are only used to print. The first-character bitmap that the fast paths in JIT code test belongs to the RegExp, not to its code; m_minimumSize goes back to 0, which sends their length check to the slow path, and a minimum size folded as a constant is a property of the pattern. So no machine code refers to what deleteCode() frees. What the compiler threads do read is the YarrCodeBlock's inline statistics (DFGStrengthReductionPhase, then compileRegExpTestInline asserts getRegExpJITCodeBlockConcurrently()), so the release only happens when VM::numberOfActiveJITPlans() is zero; plans are started by the mutator, i.e. not during the shrink. The KeepCodeThatNeedsParsing shrink gets the same guarantee from Heap::deleteAllCodeBlocks() completing all plans first. - Running matches: shrinkFootprintNow() refuses to run with a VMEntryScope (JS on the stack), and RegExp::match neither re-enters the run loop nor is called by the embedder outside of the API lock, so no Yarr code is on the native stack of this VM when the shrink runs. deleteCode() takes the RegExp's cellLock like every other state change. - RegExpCache: every RegExp is created through RegExpCache::lookupOrCreate() and, unless its pattern has a syntax error (no code then), is in its weak map for as long as it lives; entries whose RegExp is dead are skipped. The small strong cache (recently created patterns) is left alone: it only keeps RegExp cells alive, not their code. The same application, 20 inputs, idle 240 s, 10 more inputs, idle 240 s: executable pool after each shrink 3.67 MB allocated / 4.70 MB committed -> 0.13 / 0.97, RssAnon at rest 3.7-6 MB lower. The second session compiles ~450 RegExps again, and that is not free: user-mode instructions of the whole process, five interleaved pairs, 33.94 G (33.75-34.24) without and 34.91 G (34.46-35.34) with the option, +0.97 G (+2.9 %) for one wake-up. Three RegExps account for most of both the memory and the cost: patterns of 530 KB, 169 KB and 20 KB (machine-generated) compile to 1.97 MB, 0.71 MB and 0.17 MB of code in two variants each; the other 570 RegExps hold 1.0 MB together. Hence off by default.
Jarred-Sumner
added a commit
that referenced
this pull request
Sep 15, 2026
…ps that are not in use (#633) After an interactive session of a large bundled CLI application the executable pool holds 5.0 MB of allocated code (6.7 MB of committed pages) two minutes after the last input, and 3.7 MB (4.7 MB) after the deep-idle VM::shrinkFootprintNow(KeepCodeInUse). By owner: Yarr code of 580 RegExps 3.85 MB (6.6 KB each), Baseline 0.56 MB in use + 0.60 MB cached on unlinked code blocks whose CodeBlocks are gone (released by its lease before the shrink), DFG 0.22 MB. The pool itself is fine: MetaAllocator decommits a page when its last allocation dies and only 1.0-1.7 MB of committed pages are partly used. What stays is RegExp code, which KeepCodeInUse never touches (KeepCodeThatNeedsParsing drops all of it). With Options::releaseIdleRegExpCodeWhenShrinkingFootprint (off by default) the KeepCodeInUse shrink calls RegExp::deleteCode() on every RegExp in the RegExpCache that has not matched since the last full collection began. The embedder shrinks after the program has been at rest well beyond its last idle collection, so that means "not during the quiet period"; a RegExp on a timer keeps its code. A RegExp whose code was dropped is in the NotCompiled state, exactly as after VM::deleteAllRegExpCode(), and compiles again from its pattern when it next matches. - In use: RegExp::compileIfNecessary[MatchOnly], which every match that runs the matcher goes through first (one that the minimum-length filter rejects does not count), stores the low byte of the heap's marking version (one per full collection) in the new RegExp::m_lastUseEpoch. A stale byte that happens to equal the current one after 256 full collections only keeps some code for another round. The byte takes one of the three bytes of padding between m_constructionErrorCode and m_numSubpatterns; RegExp stays in the 80-byte size class. - Optimized code: DFG and FTL never call a RegExp's Yarr code by address. RegExpExec/RegExpTest/... call operations that end up in RegExp::match; RegExpTestInline emits a fresh copy of the matcher into the DFG/FTL code (Yarr::jitCompileInlinedTest from SpeculativeJIT::compileRegExpTestInline and FTL::LowerDFGToB3::compileRegExpTestInline), built from the pattern; the get*MatchAddr() accessors of YarrCodeBlock are only used to print. The first-character bitmap that the fast paths in JIT code test belongs to the RegExp, not to its code; m_minimumSize goes back to 0, which sends their length check to the slow path, and a minimum size folded as a constant is a property of the pattern. So no machine code refers to what deleteCode() frees. What the compiler threads do read is the YarrCodeBlock's inline statistics (DFGStrengthReductionPhase, then compileRegExpTestInline asserts getRegExpJITCodeBlockConcurrently()), so the release only happens when VM::numberOfActiveJITPlans() is zero; plans are started by the mutator, i.e. not during the shrink. The KeepCodeThatNeedsParsing shrink gets the same guarantee from Heap::deleteAllCodeBlocks() completing all plans first. - Running matches: shrinkFootprintNow() refuses to run with a VMEntryScope (JS on the stack), and RegExp::match neither re-enters the run loop nor is called by the embedder outside of the API lock, so no Yarr code is on the native stack of this VM when the shrink runs. deleteCode() takes the RegExp's cellLock like every other state change. - RegExpCache: every RegExp is created through RegExpCache::lookupOrCreate() and, unless its pattern has a syntax error (no code then), is in its weak map for as long as it lives; entries whose RegExp is dead are skipped. The small strong cache (recently created patterns) is left alone: it only keeps RegExp cells alive, not their code. The same application, 20 inputs, idle 240 s, 10 more inputs, idle 240 s: executable pool after each shrink 3.67 MB allocated / 4.70 MB committed -> 0.13 / 0.97, RssAnon at rest 3.7-6 MB lower. The second session compiles ~450 RegExps again, and that is not free: user-mode instructions of the whole process, five interleaved pairs, 33.94 G (33.75-34.24) without and 34.91 G (34.46-35.34) with the option, +0.97 G (+2.9 %) for one wake-up. Three RegExps account for most of both the memory and the cost: patterns of 530 KB, 169 KB and 20 KB (machine-generated) compile to 1.97 MB, 0.71 MB and 0.17 MB of code in two variants each; the other 570 RegExps hold 1.0 MB together. Hence off by default.
Jarred-Sumner
added a commit
that referenced
this pull request
Sep 15, 2026
…ps that are not in use (#633) After an interactive session of a large bundled CLI application the executable pool holds 5.0 MB of allocated code (6.7 MB of committed pages) two minutes after the last input, and 3.7 MB (4.7 MB) after the deep-idle VM::shrinkFootprintNow(KeepCodeInUse). By owner: Yarr code of 580 RegExps 3.85 MB (6.6 KB each), Baseline 0.56 MB in use + 0.60 MB cached on unlinked code blocks whose CodeBlocks are gone (released by its lease before the shrink), DFG 0.22 MB. The pool itself is fine: MetaAllocator decommits a page when its last allocation dies and only 1.0-1.7 MB of committed pages are partly used. What stays is RegExp code, which KeepCodeInUse never touches (KeepCodeThatNeedsParsing drops all of it). With Options::releaseIdleRegExpCodeWhenShrinkingFootprint (off by default) the KeepCodeInUse shrink calls RegExp::deleteCode() on every RegExp in the RegExpCache that has not matched since the last full collection began. The embedder shrinks after the program has been at rest well beyond its last idle collection, so that means "not during the quiet period"; a RegExp on a timer keeps its code. A RegExp whose code was dropped is in the NotCompiled state, exactly as after VM::deleteAllRegExpCode(), and compiles again from its pattern when it next matches. - In use: RegExp::compileIfNecessary[MatchOnly], which every match that runs the matcher goes through first (one that the minimum-length filter rejects does not count), stores the low byte of the heap's marking version (one per full collection) in the new RegExp::m_lastUseEpoch. A stale byte that happens to equal the current one after 256 full collections only keeps some code for another round. The byte takes one of the three bytes of padding between m_constructionErrorCode and m_numSubpatterns; RegExp stays in the 80-byte size class. - Optimized code: DFG and FTL never call a RegExp's Yarr code by address. RegExpExec/RegExpTest/... call operations that end up in RegExp::match; RegExpTestInline emits a fresh copy of the matcher into the DFG/FTL code (Yarr::jitCompileInlinedTest from SpeculativeJIT::compileRegExpTestInline and FTL::LowerDFGToB3::compileRegExpTestInline), built from the pattern; the get*MatchAddr() accessors of YarrCodeBlock are only used to print. The first-character bitmap that the fast paths in JIT code test belongs to the RegExp, not to its code; m_minimumSize goes back to 0, which sends their length check to the slow path, and a minimum size folded as a constant is a property of the pattern. So no machine code refers to what deleteCode() frees. What the compiler threads do read is the YarrCodeBlock's inline statistics (DFGStrengthReductionPhase, then compileRegExpTestInline asserts getRegExpJITCodeBlockConcurrently()), so the release only happens when VM::numberOfActiveJITPlans() is zero; plans are started by the mutator, i.e. not during the shrink. The KeepCodeThatNeedsParsing shrink gets the same guarantee from Heap::deleteAllCodeBlocks() completing all plans first. - Running matches: shrinkFootprintNow() refuses to run with a VMEntryScope (JS on the stack), and RegExp::match neither re-enters the run loop nor is called by the embedder outside of the API lock, so no Yarr code is on the native stack of this VM when the shrink runs. deleteCode() takes the RegExp's cellLock like every other state change. - RegExpCache: every RegExp is created through RegExpCache::lookupOrCreate() and, unless its pattern has a syntax error (no code then), is in its weak map for as long as it lives; entries whose RegExp is dead are skipped. The small strong cache (recently created patterns) is left alone: it only keeps RegExp cells alive, not their code. The same application, 20 inputs, idle 240 s, 10 more inputs, idle 240 s: executable pool after each shrink 3.67 MB allocated / 4.70 MB committed -> 0.13 / 0.97, RssAnon at rest 3.7-6 MB lower. The second session compiles ~450 RegExps again, and that is not free: user-mode instructions of the whole process, five interleaved pairs, 33.94 G (33.75-34.24) without and 34.91 G (34.46-35.34) with the option, +0.97 G (+2.9 %) for one wake-up. Three RegExps account for most of both the memory and the cost: patterns of 530 KB, 169 KB and 20 KB (machine-generated) compile to 1.97 MB, 0.71 MB and 0.17 MB of code in two variants each; the other 570 RegExps hold 1.0 MB together. Hence off by default.
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.
Stacked on #628 (one commit on top of its branch; it hangs off the KeepCodeInUse shrink mode that PR adds). Off by default.
the other two follow-ups.
Options::releaseIdleRegExpCodeWhenShrinkingFootprint, default off: this PR adds themechanism and says why it is not switched on.
Why
After an interactive session of a large bundled CLI application the executable pool holds 5.0 MB of allocated code (6.7 MB
of committed pages) two minutes after the last input, and 3.7 MB (4.7 MB) after the deep-idle
VM::shrinkFootprintNow(KeepCodeInUse)of #628. By owner: Yarr code of 580 RegExps 3.85 MB (6.6 KB each), Baseline 0.56 MBin use + 0.60 MB cached on unlinked code blocks whose CodeBlocks are gone, DFG 0.22 MB. The pool itself is fine
(MetaAllocator decommits a page when its last allocation dies; only 1.0-1.7 MB of committed pages are partly used). What
stays is RegExp code, which
KeepCodeInUsenever touches (KeepCodeThatNeedsParsingdrops all of it).Measured with the option on (20 inputs, idle 240 s, 10 more inputs, idle 240 s):
The second session compiles ~450 RegExps again. Three of them account for most of both the memory and the cost: generated
patterns of 530 KB, 169 KB and 20 KB that compile to 1.97 MB, 0.71 MB and 0.17 MB of code in two variants each; the other
570 RegExps hold 1.0 MB together. +2.9 % instructions for one wake-up is not a trade to make for every embedder, hence off
by default; an application without such outliers gets ~1 MB for close to nothing and can turn it on.
What
With the option on, the
KeepCodeInUseshrink callsRegExp::deleteCode()on every RegExp in the RegExpCache that has notmatched since the last full collection began. The embedder shrinks after the program has been at rest well beyond its last
idle collection, so that means "not during the quiet period"; a RegExp on a timer keeps its code. A RegExp whose code was
dropped is in the
NotCompiledstate, exactly as afterVM::deleteAllRegExpCode(), and compiles again from its patternwhen it next matches.
RegExp::compileIfNecessary[MatchOnly], which every match that runs the matcher goes through first, stores the lowbyte of the heap's marking version (one per full collection) in the new
RegExp::m_lastUseEpoch. One byte store permatch, in every configuration (this is the only cost with the option off). A stale byte that happens to equal the current
one after 256 full collections only keeps some code for another round. A match that the minimum-length filter rejects
before it reaches the matcher does not count as a use, which errs on the side of releasing.
m_constructionErrorCode;sizeof(RegExp)stays 80(checked on release and debug builds of this branch).
RegExpCache::deleteCodeNotUsedInCurrentFullCollectionCycle()walks the weak map underm_lock; entries whose RegExp isdead are skipped. The small strong cache is left alone: it keeps RegExp cells alive, not their code.
Safety
RegExpExec/RegExpTest/... call operations thatend up in
RegExp::match;RegExpTestInlineemits a fresh copy of the matcher into the DFG/FTL code(
Yarr::jitCompileInlinedTest), built from the pattern; theget*MatchAddr()accessors ofYarrCodeBlockare only usedto print. Re-checked on this base, which has the first-character-bitmap and minimum-length fast paths from upstream: the
bitmap is owned by the RegExp (
m_firstCharacterBitmap), not by its code, anddeleteCode()leaves it alone;m_minimumSizeis reset to 0 bydeleteCode(), which sends the run-time check in JIT code to the slow path, and aminimum size folded into JIT code as a constant is a property of the pattern and stays true.
YarrCodeBlock's inline statistics (DFGStrengthReductionPhase, thencompileRegExpTestInlineassertsgetRegExpJITCodeBlockConcurrently()), so the release only happens whenVM::numberOfActiveJITPlans()is zero; plans are started by the mutator, i.e. not during the shrink. TheKeepCodeThatNeedsParsingshrink gets the same guarantee fromHeap::deleteAllCodeBlocks()completing all plans first.shrinkFootprintNow()refuses to run with aVMEntryScope(JS on the stack) or from inside thecollector, and
RegExp::matchneither re-enters the run loop nor is called by the embedder outside of the API lock, so noYarr code is on the native stack of this VM when the shrink runs.
deleteCode()takes the RegExp'scellLocklike everyother state change (
matchConcurrentlytakes it too).Yarr code; a later DFG compile then finds no inline statistics and does not inline until the RegExp has matched through
the runtime once more. A missed optimization after a deep idle, not a correctness matter.
Tests
All runs against jsc shells (release, and debug + ASan with
ASAN_OPTIONS=detect_stack_use_after_return=0) of this branch(
1b7bf3e5a380) and of the base (209144af8f6c), base lists retaken in the same session.New test
JSTests/stress/shrink-footprint-releases-idle-regexp-code.js(JIT and--useJIT=0): 200 RegExps match, a fullcollection, ten of them match again,
$vm.shrinkFootprintWhenIdle(keepCodeThatNeedsParsing, keepCodeInUse); asserts via$vm.codeBlockCensus().regExpsWithCodethat the idle ones lost their code, that the ten kept theirs, and that all of themmatch and have code again afterwards. 2/2 on release, 2/2 on ASan. With the option off it fails ("idle RegExps lost their
code: 200 -> 200"), so it cannot pass vacuously.
JSTests/stress, release, all 5,849 files (197 skipped by their own directives), failures / passes:
int8-repeat-in-then-out-of-bounds.js, flaky)--collectContinuously=1codeblock-aging-execution-count.js, flaky)--releaseIdleRegExpCodeWhenShrinkingFootprint=1--collectContinuously=1JSTests/stress, debug + ASan, every fourth file (1,462; 41 skipped), option on: 15 / 1406, the same 15 as base.
Because the suite only reaches the new code through the few tests that shrink the footprint, one more run (a throwaway
driver, not part of the PR): 334 RegExp / String.prototype.{replace,match,split} stress tests executed with
run()in ONEVM, then full collection +
shrinkFootprintWhenIdle(KeepCodeInUse), all 334 again, shrink again, all 334 a third time;every file must give the same result in each pass. RegExps with code 3,280 -> 0 after each shrink -> 3,281 after the next
pass; 0 differences; release with default tiering, release with eager non-concurrent JIT
(
thresholdForJITAfterWarmUp=10 thresholdForOptimizeAfterWarmUp=20 thresholdForFTLOptimizeAfterWarmUp=50)@@ASANDRIVER@@.Not done
the measurement should be repeated before flipping the default.