Skip to content

Reuse transactions across repeated TSP type queries - #4702

Open
Punisheroot wants to merge 1 commit into
facebook:mainfrom
Punisheroot:perf/tsp-reuse-type-query-transaction
Open

Reuse transactions across repeated TSP type queries#4702
Punisheroot wants to merge 1 commit into
facebook:mainfrom
Punisheroot:perf/tsp-reuse-type-query-transaction

Conversation

@Punisheroot

Copy link
Copy Markdown
Contributor

Summary

Fixes #4312.

Reuse the existing revision-aware TransactionManager for TSP type queries instead of creating and discarding a fresh transaction for every request.

This applies to:

  • getDeclaredType
  • getComputedType
  • getExpectedType
  • resolveImport

Both the main TSP connection and auxiliary IPC connections now retain transaction data across requests. The retained transaction is automatically replaced when the committed State revision changes.

Repeated type queries against imported but unopened modules previously started from a fresh transaction each time. Consequently, distinct getComputedType requests for the same module and snapshot repeatedly ran the module's solve work.

With this change:

  • the first query solves the imported module;
  • subsequent queries on the same TSP connection and snapshot reuse its answers;
  • after a file change produces a new snapshot, the module is recomputed normally.

Performance evidence

The regression test records transaction telemetry for two different ranges in the same unopened imported module:

  • first query: step_answers_count > 0;
  • second query: step_answers_count == 0;
  • second query: run_todo_count == 0.

In the synthetic test logs, the first query took approximately 0.09s, while the second was reported as 0.00s at the current logging precision.

This does not reproduce the full Django/Pylance benchmark from the issue, but it verifies that the repeated solve responsible for the reported regression is eliminated within the same connection and snapshot.

Correctness

The regression test also modifies the imported file and advances the snapshot. The following query:

  • performs solve work again;
  • observes the updated type.

Transaction reuse is scoped per TSP connection; separate auxiliary connections may each perform their own initial solve.

Test Plan

  • Focused regression test passed
  • TSP interaction suite: 95 passed
  • Full pyrefly package: 8195 passed, 5 ignored
  • Repository formatting and lint checks passed
  • git diff --check passed

@meta-codesync

meta-codesync Bot commented Aug 28, 2026

Copy link
Copy Markdown
Contributor

This pull request has been imported. If you are a Meta employee, you can view this in D117845472. (Because this pull request was imported automatically, there will not be any future comments.)

@github-actions

Copy link
Copy Markdown

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avoid repeated Require::Everything solves for TSP type queries

2 participants