Skip to content

Look up getLibraryApi's boundary roots by hash, and emit id tables in ascending order - #41

Merged
knutwannheden merged 1 commit into
mainfrom
libapi-boundary-perf
Dec 4, 2026
Merged

knutwannheden merged 1 commit into
mainfrom
libapi-boundary-perf

Conversation

@knutwannheden

Copy link
Copy Markdown
Contributor

A getLibraryApi request for transformers 5.26.0 takes 9.9 s of CPU, and 29% of it goes to deciding whether each class lies inside the request's roots. The request names every one of the wheel's 2,979 files as a root, and is_external compared a class's file with each of them through Path::starts_with, for every class literal and generic alias the registry builds.

CPU of v1.0.3 and this branch in ms, both built locally with identical flags, median of 5 interleaved runs:

request v1.0.3 this branch change
getLibraryApi transformers (2,979 files) 9,916 6,572 −34%
getLibraryApi openai (1,877 files) 2,767 1,305 −53%
getLibraryApi mypy (1,097 files) 2,674 1,825 −32%
getLibraryApi sqlalchemy (257 files) 1,280 1,100 −14%
getLibraryApi pydantic (106 files) 421 400 −5%
getTypes for each of fastapi's 1,233 files 7,019 6,238 −11%

Peak RSS drops as well, from 1,577 MB to 1,405 MB for transformers. Every response parses to the same JSON as v1.0.3's.

What changes

  • Boundary::UnderRoots holds an FxHashSet. A file is inside the boundary when one of its ancestors, itself included, is in the set, so each call costs the path's depth rather than the number of roots.
  • JsonRpcResponse::success serializes its result once into a RawValue. It no longer builds a serde_json::Value tree for every response, which is where the getTypes gain above comes from.

Tables keyed by id come out in ascending order

Serializing through Value sorted every object's keys, so v1.0.3 wrote types and symbols in string order ("1", "10", "2"). Written directly, a std HashMap comes out in an order that changes with every process. Clients read these tables in arrival order: rewrite-python keeps the first class literal per simple class name and streams a library's classes in the order it found them.

These tables, and the one-shot mode's files, are now BTreeMaps, in ascending id order. The README states that order. A response is byte-identical across processes again, and stayed so over the five runs of each request above.

responses_are_byte_identical_across_processes sends the same getTypes and getLibraryApi requests to two processes and compares the raw response lines. With the tables back on HashMap it fails.

… ascending order

A getLibraryApi request names each file of a wheel as a root, and
is_external compared every class's file with every root through
Path::starts_with. Keep the roots in a hash set and look up the file's
ancestors instead.

Serialize each result once into a RawValue rather than through a
serde_json::Value tree. The Value tree had sorted every object's keys,
so tables keyed by id are now BTreeMaps: clients read them in arrival
order, and a response stays byte-identical across processes.
@knutwannheden knutwannheden self-assigned this Dec 4, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Dec 4, 2026
@knutwannheden
knutwannheden merged commit f544db0 into main Dec 4, 2026
3 checks passed
@knutwannheden
knutwannheden deleted the libapi-boundary-perf branch December 4, 2026 15:46
@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