Repository navigation
Python: share one JavaType.Variable per variable - #10313
Merged
knutwannheden merged 3 commits intoDec 4, 2026
Merged
Conversation
The Method interning tests read each parsed tree straight from local_objects. Since a project's parse streams file by file, that slot holds a placeholder until the tree is resolved, so the tests visited the placeholder and failed. Resolve it through resolve_local_object, as the other tests in the file do.
The Python type mapping built a new JavaType.Variable for every reference to a variable. During a ParseProject, the RPC send-side ref map and the receiving side's remote refs keep every type sent for the whole project, so these per-reference objects made the ref maps grow with the project's references rather than with its variables. The mapping now asks ty-types for symbol ids (`includeSymbols`). Each binding then names its symbol by a session-stable id, parameters included, and the response describes each symbol once per session. References to a name or parameter share one Variable per symbol id and type, through a table on the session's SessionTypeCache. The type stays in the key because ty types each reference as narrowed there. An attribute reference has no symbol of its own when the attribute is assigned in a method, so it shares by owner, name and type. Nothing is shared when the owner is unknown, since an unknown owner does not tell two receivers apart. Bindings described by id resolve to the session's symbol entry, so the existing binding consumers read the same fields. ty's symbol identifies the variable, not its spelling. Same-named locals of a property getter and setter, whose qualified names coincide, keep their own Variables. So do the two sides of a class body's `x = x`. ty describes a symbol only in the first response that names it, as it does a type. A getTypes request that times out still has its late answer's types and symbols added to the session tables, so later files' bindings resolve. A ty-types without symbol support ignores the request and answers with plain bindings. Each reference then keeps its own Variable. On abseil-py's 71 files, name references hold 6,588 Variables instead of 19,749, and attribute references 1,510 instead of 3,596. A file of `values.append(str(i).zfill(4))` repeated 200 times holds 2 Variables instead of 402.
The test that checks a project's parse holds no tree between the files it hands out allowed two trees above the starting point. A tree not yet garbage collected beside the one being handed out came within 2% of that, and one CI run went over (2,065,714 bytes against 2,030,592). Holding every tree of the 8-file project would take about 8 trees, so a limit of half the files keeps that failure caught with room for a late collection.
knutwannheden
deleted the
python-share-javatype.variable-per-declared-variable
branch
December 4, 2026 07:10
This was referenced Dec 4, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
During a ParseProject, the RPC server's send-side ref map and Java's
RewriteRpc.remoteRefskeep every type sent for the whole project. With ty on, the Python type mapping built a newJavaType.Variablefor every reference to a variable, as it did for Methods before #10311.So the ref maps grew with a project's references rather than its variables. A file of
i = 0,values = []and 200 lines ofvalues.append(str(i).zfill(4))held 402 Variables:valuesfile, afterKey on ty's symbol, not the variable's name
The mapping now requests
includeSymbolsfrom ty-types (openrewrite/ty-types#38, #39). Every binding then names its symbol by a session-stable id, parameters and loop targets included. Variables are shared through a table onSessionTypeCache:A name alone would merge different variables, and so would ty's
qualifiedName. The localsrin a property's getter and setter both readm.C.<locals of function 'value'>.r, yet their symbol ids differ. The id also keeps a class body'sx = xas two variables. Java likewise keys a local by its method and position, and a field by its class.Bindings described by id resolve to the session's symbol entry, so consumers such as
_binding_ownerread the same fields as before. ty describes a symbol only in the first response naming it, so a getTypes request that timed out still has its late answer's tables added to the session.Tests
test_type_interning.pypins sharing between a parameter and its uses, separate getter and setter locals, the class body'sx = x, sharing among one owner's attributes, and separate attributes on receivers of unknown type. The last was mutation-checked against the unknown-owner guard.test_ty_client.pychecks that a late response's symbols join the session.The first commit repairs the Method interning tests, which broke when #10312 started streaming parses. The last raises the limit of #10312's streaming memory test from 2 trees to half the project's 8. A tree not yet garbage collected took one CI run 2% over the old limit, while holding every tree would take about 8.
Not changed here
*argsor**kwargsis not typed yet, so it doesn't share its uses' Variable. That is a separate, older bug.Requires ty-types 1.0.2.