Repository navigation
Recycle temporary expression scopes on success and failure #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
284km
wants to merge
1
commit into
main
Choose a base branch
from
perf/reuse-scratch-scopes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+122
−10
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| # Run with MERE_RUBY_STORE_STATS=1 to compare pool_news as count grows. | ||
| count = Integer(ARGV.fetch(0, '20000')) | ||
| marker = :kept | ||
| keep = proc { marker } | ||
| cell = Struct.new(:value).new(0) | ||
| items = [0] | ||
| count.times do |i| | ||
| raise 'comparison' if i + 1 != 1 + i | ||
| cell.value += 1 | ||
| items[0] += 1 | ||
| end | ||
|
|
||
| class ScratchFailure | ||
| def value | ||
| raise 'getter' | ||
| end | ||
| def [](_index) | ||
| raise 'index' | ||
| end | ||
| def ==(_other) | ||
| raise 'comparison' | ||
| end | ||
| end | ||
| bad = ScratchFailure.new | ||
| errors = 0 | ||
| count.times do | ||
| begin | ||
| bad.value += 1 | ||
| rescue RuntimeError => error | ||
| raise unless error.message == 'getter' | ||
| errors += 1 | ||
| end | ||
| begin | ||
| bad[0] += 1 | ||
| rescue RuntimeError => error | ||
| raise unless error.message == 'index' | ||
| errors += 1 | ||
| end | ||
| begin | ||
| bad == Object.new | ||
| rescue RuntimeError => error | ||
| raise unless error.message == 'comparison' | ||
| errors += 1 | ||
| end | ||
| end | ||
| raise 'values' unless cell.value == count && items[0] == count && errors == count * 3 | ||
| raise 'capture' unless keep.call == :kept | ||
| puts 'scratch scopes: values, captures and exceptions preserved' |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| #!/usr/bin/env python3 | ||
| """Temporary expression scopes must return to the frame pool as a loop grows.""" | ||
| import os | ||
| from pathlib import Path | ||
| import re | ||
| import subprocess | ||
|
|
||
| ROOT = Path(__file__).resolve().parent.parent | ||
| binary = os.environ.get('MR_BIN', str(ROOT / 'mere-ruby')) | ||
| for indexing in ('1', '0'): | ||
| news = [] | ||
| for count in (100, 10000): | ||
| env = dict(os.environ, MERE_RUBY_STORE_STATS='1') | ||
| if indexing == '0': | ||
| env['MERE_RUBY_NO_HASH_INDEX'] = '1' | ||
| else: | ||
| env.pop('MERE_RUBY_NO_HASH_INDEX', None) | ||
| result = subprocess.run([binary, str(ROOT / 'bench/scratch_scopes.rb'), str(count)], | ||
| env=env, capture_output=True, text=True, timeout=60, check=True) | ||
| assert result.stdout == 'scratch scopes: values, captures and exceptions preserved\n', result.stdout | ||
| match = re.search(r'^pool_news\t(\d+)$', result.stderr, re.M) | ||
| assert match, result.stderr | ||
| news.append(int(match[1])) | ||
| # This measures allocation counts, not a platform-dependent RSS threshold. | ||
| assert news[1] <= news[0] + 32, (indexing, news) | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The allocation-count comparison is a useful regression guard for this leak across platforms. The workload covers all three expression paths and both successful and exceptional exits; I reproduced |
||
| print(f'hash indexing {indexing}: fresh frames at 100/10000 iterations = {news}') | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good use of the existing captured-frame lifecycle here:
blk_env_putboth avoids recycling a genuinely captured scratch frame and records it inlv_cap. I checked a binding created byself.binding ||= nil, plus a proc created through that binding; both retained their values after repeated frame reuse and explicit GC, including under GC stress.