-
-
Notifications
You must be signed in to change notification settings - Fork 295
fix: avoid quadratic inline footnote membership checks #478
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
Open
susyimes
wants to merge
4
commits into
lepture:main
Choose a base branch
from
susyimes:blackhole/footnote-defs-membership-quadratic
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.
+37
−1
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """Synthesized standalone repro for the footnote_defs defect (complexity). | ||
|
|
||
| Discovered autonomously by blackhole_agent.upstream_discovery. Runs a doubling | ||
| ladder against the source tree given as argv[1]; exits 1 while the defect is | ||
| present, 0 once repaired. Usage: python <this file> <path-to-src-dir> | ||
| """ | ||
| import json, math, sys, time | ||
|
|
||
| sys.path.insert(0, sys.argv[1]) | ||
| PLUGINS = ['footnotes'] | ||
| KIND = 'complexity' | ||
| EXPONENT_THRESHOLD = 1.75 | ||
| TIME_FLAG_FLOOR = 0.3 | ||
|
|
||
| def gen(n): | ||
| refs = ''.join('[^%d] ' % i for i in range(n)) | ||
| defs = '\n'.join('[^%d]: x' % i for i in range(n)) | ||
| return refs + '\n\n' + defs | ||
|
|
||
| import mistune | ||
|
|
||
| _RENDERERS = {} | ||
|
|
||
| def render(text, plugins): | ||
| key = tuple(plugins) | ||
| md = _RENDERERS.get(key) | ||
| if md is None: | ||
| md = mistune.create_markdown(plugins=list(plugins)) | ||
| _RENDERERS[key] = md | ||
| md(text) | ||
|
|
||
| render('warmup', PLUGINS) | ||
| n = 16000 | ||
| times = [] | ||
| crashed = None | ||
| limit = max(16000 * 4, 16000 + 1) | ||
| while n <= limit: | ||
| text = gen(n) | ||
| t0 = time.perf_counter() | ||
| try: | ||
| render(text, PLUGINS) | ||
| elapsed = time.perf_counter() - t0 | ||
| except Exception as e: | ||
| crashed = type(e).__name__ | ||
| break | ||
| times.append((n, elapsed)) | ||
| if elapsed >= 1.0 and len(times) >= 2: | ||
| # Always measure at least two sizes: one load-inflated run must not | ||
| # end the ladder before any growth pair exists. | ||
| break | ||
| n *= 2 | ||
|
|
||
| if crashed is not None: | ||
| print(json.dumps({'defect': True, 'kind': KIND, 'crash': crashed})) | ||
| sys.exit(1) | ||
| worst = 0.0 | ||
| for (n1, t1), (n2, t2) in zip(times, times[1:]): | ||
| if t1 >= 0.02 and n2 > n1: | ||
| worst = max(worst, math.log2(max(t2, 1e-9) / t1) / math.log2(n2 / n1)) | ||
| t_max = max((t for _, t in times), default=0.0) | ||
| defect = worst >= EXPONENT_THRESHOLD and t_max >= TIME_FLAG_FLOOR | ||
| print(json.dumps({'defect': defect, 'kind': KIND, 'exponent': round(worst, 3), 't_max': round(t_max, 4)})) | ||
| sys.exit(1 if defect else 0) | ||
17 changes: 17 additions & 0 deletions
17
tests/test_contribution_footnote_defs_membership_quadratic.py
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,17 @@ | ||
| """Regression test synthesized by blackhole_agent.upstream_contribution. | ||
|
|
||
| Runs the minimized standalone repro for defect footnote-defs-membership-quadratic against the | ||
| patched source tree; the repro exits 0 only once the defect is repaired. | ||
| """ | ||
|
|
||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
| REPRO = Path(__file__).resolve().parent / 'footnote_defs_quadratic.py' | ||
| SRC = Path(__file__).resolve().parents[1] / 'src' | ||
|
|
||
|
|
||
| def test_footnote_defs_membership_quadratic_regression() -> None: | ||
| proc = subprocess.run([sys.executable, str(REPRO), str(SRC)], capture_output=True, text=True) | ||
| assert proc.returncode == 0, proc.stderr[-400:] | ||
|
susyimes marked this conversation as resolved.
Outdated
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.