Skip to content

Lazily create builtin bindings#26510

Merged
charliermarsh merged 4 commits into
mainfrom
charlie/codex-codspeed-speedup
Jul 7, 2026
Merged

Lazily create builtin bindings#26510
charliermarsh merged 4 commits into
mainfrom
charlie/codex-codspeed-speedup

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jul 1, 2026

Copy link
Copy Markdown
Member

Summary

The linter previously inserted every version-specific builtin, magic global, notebook builtin, and configured custom builtin into the module scope before walking the AST, even when a file never referenced most of those names.

This moves builtin identification into SemanticModel and creates concrete builtin bindings only when a load or binding operation needs them.

@codspeed-hq

codspeed-hq Bot commented Jul 1, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 9.79%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 10 improved benchmarks
✅ 50 untouched benchmarks
⏩ 93 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation linter/default-rules[numpy/globals.py] 179.1 µs 148.7 µs +20.42%
Memory linter/default-rules[pydantic/types.py] 348.9 KB 306.7 KB +13.78%
Memory linter/default-rules[numpy/ctypeslib.py] 164.2 KB 146.2 KB +12.31%
Simulation linter/all-with-preview-rules[numpy/globals.py] 832.5 µs 749.1 µs +11.13%
Simulation linter/all-rules[numpy/globals.py] 717 µs 664.6 µs +7.88%
Memory linter/all-rules[pydantic/types.py] 648.8 KB 606.6 KB +6.97%
Simulation linter/default-rules[unicode/pypinyin.py] 370.1 µs 346.3 µs +6.87%
Memory linter/all-with-preview-rules[pydantic/types.py] 667.2 KB 625 KB +6.76%
Memory linter/all-rules[numpy/ctypeslib.py] 289.3 KB 271.3 KB +6.64%
Memory linter/all-with-preview-rules[numpy/ctypeslib.py] 317.3 KB 299.3 KB +6.01%

Tip

Curious why this is faster? Use the CodSpeed MCP and ask your agent.


Comparing charlie/codex-codspeed-speedup (634be4d) with main (71d151f)

Open in CodSpeed

Footnotes

  1. 93 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@charliermarsh
charliermarsh force-pushed the charlie/codex-codspeed-speedup branch from 675a38b to 8e9a3a6 Compare July 1, 2026 20:53
@charliermarsh
charliermarsh requested a review from ntBre July 1, 2026 20:54
@charliermarsh charliermarsh added the performance Potential performance improvement label Jul 1, 2026
@charliermarsh
charliermarsh marked this pull request as ready for review July 1, 2026 20:55
@ntBre

ntBre commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Nice! The performance improvement looks great, but I'm a bit wary of the API. It seems like it would be easy to forget to call ensure_builtin_binding everywhere it's necessary. Is there any way to use a Cell or something to initialize the builtins automatically when we try to access them?

As one example, Codex claims that this snippet:

from airflow.operators.python import PythonOperator

task = PythonOperator(
    task_id="task",
    op_args="{{ ti.xcom_pull(task_ids='len') }}",
)
ruff check --preview --select AIR201 --unsafe-fixes repro.py

has no fix on this branch but offers a fix on main. The result of the fix actually looks like a bug on main (playground), but it's still an example of a behavior change that I think is accidental.

In general Codex claims "every caller must remember whether lookup_symbol alone is sufficient or whether it also needs has_builtin_binding," so it seems nicer if we could hide the builtin construction inside lookup_symbol somehow.

@charliermarsh
charliermarsh marked this pull request as draft July 7, 2026 11:50
@astral-sh-bot

astral-sh-bot Bot commented Jul 7, 2026

Copy link
Copy Markdown

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

@charliermarsh
charliermarsh marked this pull request as ready for review July 7, 2026 12:18
@charliermarsh

Copy link
Copy Markdown
Member Author

Good feedback -- how does this look?

@ntBre ntBre left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me, thank you!

@charliermarsh
charliermarsh merged commit 2dff4da into main Jul 7, 2026
47 checks passed
@charliermarsh
charliermarsh deleted the charlie/codex-codspeed-speedup branch July 7, 2026 13:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Potential performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants