Skip to content

Python: share one JavaType.Variable per variable - #10313

Merged
knutwannheden merged 3 commits into
mainfrom
python-share-javatype.variable-per-declared-variable
Dec 4, 2026
Merged

knutwannheden merged 3 commits into
mainfrom
python-share-javatype.variable-per-declared-variable

Conversation

@knutwannheden

@knutwannheden knutwannheden commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

During a ParseProject, the RPC server's send-side ref map and Java's RewriteRpc.remoteRefs keep every type sent for the whole project. With ty on, the Python type mapping built a new JavaType.Variable for 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 of values.append(str(i).zfill(4)) held 402 Variables:

name references attribute references
abseil-py, 71 files, before 19,749 Variables 3,596 Variables
abseil-py, 71 files, after 6,588 1,510
the values file, after 2 –

Key on ty's symbol, not the variable's name

The mapping now requests includeSymbols from 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 on SessionTypeCache:

name or parameter      (symbolId, name, type)
attribute              (owner, name, type)      not shared when the owner is unknown
no symbol, no owner    a Variable of its own, as before

A name alone would merge different variables, and so would ty's qualifiedName. The locals r in a property's getter and setter both read m.C.<locals of function 'value'>.r, yet their symbol ids differ. The id also keeps a class body's x = x as two variables. Java likewise keys a local by its method and position, and a field by its class.

  • The type stays in the key because ty types each reference as narrowed there.
  • Attributes assigned in a method have no symbol, so they share by owner, like a Java field.
  • A ty-types without symbol support ignores the request, and each reference keeps its own Variable.

Bindings described by id resolve to the session's symbol entry, so consumers such as _binding_owner read 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.py pins sharing between a parameter and its uses, separate getter and setter locals, the class body's x = 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.py checks 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

  • A Variable for a name reference still has owner None. Java's would be the enclosing method, and adding it changes what recipes see.
  • The declaration of *args or **kwargs is not typed yet, so it doesn't share its uses' Variable. That is a separate, older bug.

Requires ty-types 1.0.2.

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.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Dec 4, 2026
@knutwannheden knutwannheden self-assigned this Dec 4, 2026
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
knutwannheden merged commit daf1de6 into main Dec 4, 2026
1 check passed
@knutwannheden
knutwannheden deleted the python-share-javatype.variable-per-declared-variable branch December 4, 2026 07:10
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Dec 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant