Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ty] Improve flow snapshot performance#26012

Merged
charliermarsh merged 4 commits into
mainfrom
charlie/codex-lazy-flow-state-snapshots
Jun 16, 2026
Merged

[ty] Improve flow snapshot performance#26012
charliermarsh merged 4 commits into
mainfrom
charlie/codex-lazy-flow-state-snapshots

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jun 15, 2026

Copy link
Copy Markdown
Member

Summary

When we build a semantic index, we currently snapshot flow state by eagerly cloning every symbol and member's bindings and declarations at control-flow branches. We also apply scope-wide reachability constraints to every live place immediately, even though most places are unchanged before the branches merge.

We now represent snapshots with copy-on-write shared place states and record scope-wide reachability constraints in an append-only parent-linked structure. A place materializes its pending constraints only when it is read or mutated. When both branches still share the same place state, we merge only their path constraints, allowing complementary truthy and falsy paths to cancel without rebuilding the place.

The specialized star-import snapshot path remains narrowly scoped and avoids allocating a temporary member list.

Performance

The current CodSpeed comparison reports a 6.78% overall improvement, with 12 improved benchmarks and no regressions. DateType improved by 15.51%, anyio by 9.10%, and attrs by 6.96%. CodSpeed notes that some comparisons used different runtime environments, which may affect the exact percentages.

The typing-conformance and ecosystem comparisons reported no diagnostic changes, and retained memory was effectively unchanged.

@astral-sh-bot astral-sh-bot Bot added the ty Multi-file analysis & type inference label Jun 15, 2026
@astral-sh-bot astral-sh-bot Bot changed the title [codex] [ty] Improve flow snapshot performance [ty] [codex] [ty] Improve flow snapshot performance Jun 15, 2026
@charliermarsh charliermarsh changed the title [ty] [codex] [ty] Improve flow snapshot performance [ty] Improve flow snapshot performance Jun 15, 2026
@astral-sh-bot

astral-sh-bot Bot commented Jun 15, 2026

Copy link
Copy Markdown

Typing conformance results

No changes detected ✅

Current numbers
The percentage of diagnostics emitted that were expected errors held steady at 94.37%. The percentage of expected errors that received a diagnostic held steady at 89.00%. The number of fully passing files held steady at 94/134.

@astral-sh-bot

astral-sh-bot Bot commented Jun 15, 2026

Copy link
Copy Markdown

Memory usage report

Summary

Project Old New Diff Outcome
flake8 33.50MB 33.50MB -0.01% (4.80kB) ⬇️
trio 82.03MB 82.02MB -0.01% (8.10kB) ⬇️
sphinx 200.04MB 200.03MB -0.00% (9.28kB) ⬇️
prefect 527.62MB 527.61MB -0.00% (10.33kB) ⬇️

Significant changes

Click to expand detailed breakdown

flake8

Name Old New Diff Outcome
semantic_index 9.03MB 9.03MB -0.05% (4.80kB) ⬇️

trio

Name Old New Diff Outcome
semantic_index 19.72MB 19.71MB -0.04% (8.10kB) ⬇️

sphinx

Name Old New Diff Outcome
semantic_index 40.52MB 40.51MB -0.02% (9.28kB) ⬇️

prefect

Name Old New Diff Outcome
semantic_index 121.73MB 121.72MB -0.01% (10.33kB) ⬇️

@astral-sh-bot

astral-sh-bot Bot commented Jun 15, 2026

Copy link
Copy Markdown

ecosystem-analyzer results

No diagnostic changes detected ✅

Flaky changes detected. This PR summary excludes flaky changes; see the HTML report for details.

Full report with detailed diff (timing results)

@codspeed-hq

codspeed-hq Bot commented Jun 15, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 6.22%

⚠️ 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

⚡ 12 improved benchmarks
✅ 59 untouched benchmarks
⏩ 60 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation DateType 248.4 ms 215.9 ms +15.07%
Simulation anyio 1.2 s 1.1 s +9.09%
Simulation attrs 560.7 ms 526.4 ms +6.5%
Simulation ty_micro[many_tuple_assignments] 72.2 ms 68.4 ms +5.47%
Simulation ty_micro[complex_constrained_attributes_2] 74.8 ms 70.9 ms +5.44%
Simulation ty_micro[complex_constrained_attributes_1] 75.2 ms 71.5 ms +5.17%
Simulation ty_micro[very_large_tuple] 77.1 ms 73.3 ms +5.12%
Simulation ty_micro[complex_constrained_attributes_3] 80.5 ms 76.6 ms +5.09%
Simulation ty_micro[gradual_vararg_call] 75.6 ms 72.2 ms +4.74%
Simulation ty_micro[recursive_typed_dict_union_contextual_inference] 86.7 ms 82.9 ms +4.57%
Simulation ty_micro[many_string_assignments] 84 ms 80.5 ms +4.42%
Simulation ty_micro[pydantic_core_schema_dict] 87.6 ms 83.9 ms +4.41%

Tip

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


Comparing charlie/codex-lazy-flow-state-snapshots (e9f4e17) with main (bcae1b7)

Open in CodSpeed

Footnotes

  1. 60 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-lazy-flow-state-snapshots branch 2 times, most recently from 1302874 to a1ccd7b Compare June 15, 2026 16:56
@AlexWaygood AlexWaygood added the performance Potential performance improvement label Jun 15, 2026
@charliermarsh charliermarsh marked this pull request as ready for review June 15, 2026 17:40
@charliermarsh charliermarsh requested a review from a team as a code owner June 15, 2026 17:40
@astral-sh-bot astral-sh-bot Bot requested a review from dhruvmanila June 15, 2026 17:40

@dhruvmanila dhruvmanila left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is great, thank you!

It also helped me revise how we do control-flow analysis :)

Comment thread crates/ty_python_core/src/use_def.rs Outdated
Comment on lines +1264 to +1267
debug_assert_ne!(
current, event.parent,
"pending reachability must be an ancestor"
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this an actual invariant which needs to be followed? It seems like if this invariant is somehow broken in the release build, it will enter infinite loop given that current = event.parent which should be the same. And, given that unapplied is going to keep on pushing, it might eventually lead to OOM.

Should we try to handle this case here by either breaking out of the loop or using assert_ne instead?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good call, this is a required invariant -- changing to assert_ne.

Comment thread crates/ty_python_core/src/use_def.rs Outdated
Comment on lines +1307 to +1310
debug_assert_ne!(
current, event.parent,
"pending reachability must be an ancestor"
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same comment as above regarding if this is an invariant that needs to be followed and how to handle if it breaks.

Comment on lines +1281 to +1286
fn materialize_ref<'a>(
&self,
pending: &'a mut PendingPlaceState,
target: PendingReachabilityId,
reachability_constraints: &mut ReachabilityConstraintsBuilder,
) -> &'a PlaceState {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we document when the caller should use materialize_ref instead of materialize? Given that the parameters are the same and based on the return type, it seems important to understand this difference for the optimization to keep using the shared Rc

@charliermarsh charliermarsh force-pushed the charlie/codex-lazy-flow-state-snapshots branch from 23c6d01 to e9f4e17 Compare June 16, 2026 13:29
@charliermarsh charliermarsh enabled auto-merge (squash) June 16, 2026 13:29
@charliermarsh charliermarsh merged commit 1de944d into main Jun 16, 2026
59 checks passed
@charliermarsh charliermarsh deleted the charlie/codex-lazy-flow-state-snapshots branch June 16, 2026 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Potential performance improvement ty Multi-file analysis & type inference

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants