Repository navigation
Conversation
…/op call `normalise_peer_refs` runs for every `/v1/op` request, and it asked `_peer_ref_fields` to call `typing.get_type_hints` on the request struct each time. The ops and models modules use postponed annotations, so each of those calls `eval`s every annotation string of the struct again. That is about 78 us per request, for an answer that depends on the request type alone. `_peer_ref_fields` is now `functools.cache`d. The key set is bounded by the registry (668 distinct request types across 678 operations), and a cached lookup costs about 0.05 us. Every caller now shares one mapping, so it is returned as a `MappingProxyType`. The only caller reads it and never writes to it, but a stray write would otherwise change peer parsing for every later request of that type. It also matters for the test run. On Python 3.14 coverage uses its sys.monitoring core, which keeps every code object it sees for the rest of the session, including the throwaway `<string>` ones from those `eval`s. So each dispatched test left a little behind. PR #22 fixed the large case in tests/test_registry_contract.py; this is the leftover it listed. The new test checks that `"@alice"` is still parsed for a scalar and a list `PeerRef` field, that three calls read the hints once, and that the returned mapping refuses writes. It fails with the cache removed.
This branch has not been deployed
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.
What was wrong
normalise_peer_refsruns on every/v1/oprequest. Each time, it called_peer_ref_fields(spec.request), which calledtyping.get_type_hintson the request struct. The ops and models modules use postponed annotations, so every one of those callsevals all of the struct's annotation strings again. That costs about 78 µs per request, and the answer depends only on the request type.Under coverage's
sys.monitoringcore (the default on Python 3.14), each of thoseevals also leaves a<string>code object that stays alive for the rest of the test run. #22 fixed the large version of this intests/test_registry_contract.pyand listed this one under "Not in this PR".The fix
tlgr/daemon/dispatch.py:_peer_ref_fieldsis nowfunctools.cached.MappingProxyTypetyped asMapping[str, bool], since every caller shares one cached mapping.normalise_peer_refsis the only caller, and it only reads the mapping.Tests
tests/test_dispatch.py: a new test covers three things:"@alice"is still parsed for a scalarPeerRef | Nonefield and alist[PeerRef]field, and a plainstrfield is left alone.normalise_peer_refscalls,get_type_hintsreads the request type only once.I also ran the test with the cache bypassed (
__wrapped__), and the read-count assertion failed, so the test catches a regression.Full run:
make lint typecheck teston macOS, Python 3.14.7, withTLGR_HOMEset to a scratch directory. Ruff is clean, strict mypy is clean (139 files) and all 13,306 tests passed.