Repository navigation
perf(param-graph): reuse decoded schemas across client instances - #30687
Conversation
Signed-off-by: Levi Laine <shiueo.csh@gmail.com>
Signed-off-by: Levi Laine <shiueo.csh@gmail.com>
|
Important Review skippedReview was skipped as selected files did not have any reviewable changes.
|
| Layer / File(s) | Summary |
|---|---|
Cache implementation and validation packages/param-graph/src/param-graph.ts, packages/param-graph/src/param-graph.test.ts, .github/workflows/test-template.yml |
ParamGraph.deserialize reuses decoded data for the same serialized object and still creates a new wrapper per call. Tests cover cache reuse, wrapper-specific enum lookups, schema isolation, returned views, and retry behavior. CI adds package test steps to two jobs. |
Benchmark and cache documentation packages/param-graph/helpers/benchmark-deserialization.ts, packages/param-graph/README.md, AGENTS.md |
The benchmark reports deserialization timing and additional heap use. The README and architecture note describe cache behavior and benchmark usage. |
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Refactor
Suggested reviewers: aqrln
Merge Risk: 
Minimal · previous review 4e32d
-
No merge-blocking issue was identified; the change is ready for normal merge checks.
Security Architecture Review
-
Security architecture risk: 🔵 Low · up to
4e32dSchema reuse remains isolated by object identity, and each client retains its own wrapper and enum lookup. No new request-controlled path to shared schema data was established. The main residual risk is compatibility with code that mutates a schema after initialization, which the new contract explicitly prohibits.
Retained concerns
No architecture-level concerns identified.Security review details
-
Security Blast Radius
- inferred — The shared-state scope is client wrappers using the same serialized object in the same loaded ParamGraph class. Distinct schema objects have separate entries. Violating schema immutability could affect multiple wrappers within that scope, but no request-controlled producer of the schema object was established.
Trust Boundaries and Controls
- observed — Generated schema configuration supplies structural data at engine construction. Query inputs reach parameterization later and do not supply SerializedParamGraph. Enum lookup remains per-wrapper, and returned root and edge records do not expose cached structural records directly.
Resilience and Maintainability Implications
- inferred — For ordinary generated objects, synchronous construction prevents partially decoded data from becoming visible through the cache. Weak keys do not keep discarded schema objects alive, and decoded values do not retain the key object itself. Collection timing remains runtime-dependent.
Hardening Proposals
- proposed — If custom loaders or runtime schema replacement become supported, enforce immutable schema ownership or replace the entire serialized object when changing schemas. This would strengthen the cache contract; it is not remediation for a verified attack path.
-
Pre-merge checks | 
4 | 
1
-
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 3 files. (3 skipped: 3 unsupported.)
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Repository: prisma/orm/.coderabbit.yml
- Review profile: ASSERTIVE
- Plan: Advanced
- Run ID:
23a107de-bd1c-489f-8eda-7827561a1850
📒 Files selected for processing (6)
-
.github/workflows/test-template.yml
AGENTS.md
packages/param-graph/README.md
packages/param-graph/helpers/benchmark-deserialization.ts
packages/param-graph/src/param-graph.test.ts
packages/param-graph/src/param-graph.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
ℹ️ Recent review info
-
⚙️ Run configuration
-
- Configuration used: Repository: prisma/orm/.coderabbit.yml
- Review profile: ASSERTIVE
- Plan: Advanced
- Run ID:
23a107de-bd1c-489f-8eda-7827561a1850
📒 Files selected for processing (6)
-
.github/workflows/test-template.ymlAGENTS.mdpackages/param-graph/README.mdpackages/param-graph/helpers/benchmark-deserialization.tspackages/param-graph/src/param-graph.test.tspackages/param-graph/src/param-graph.ts
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
-
The package README already describes the cache and the benchmark. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: willbot <w.a.madden+machine@gmail.com> Signed-off-by: Will Madden <madden@prisma.io>
wmadden-electric
left a comment
There was a problem hiding this comment.
Thank you. The decoded graph is only read, so sharing it across clients is safe. I merged v7 in and removed the unrelated AGENTS.md edit; the CI change stays, since nothing else runs these tests on v7.
Workers builds carry prisma/orm#30687 for @prisma/client 7.10.1: the runtime the Workers client imports caches ParamGraph.deserialize's result in a WeakMap keyed by the serialized schema. The build fails if the function or version does not match, or if nothing was patched. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Linked issue
Refs #30645. This addresses repeated ParamGraph deserialization in Prisma 7; it does not resolve the issue's first-client startup costs.
Summary
Creating a new Prisma Client per request repeatedly decodes the same parameterization schema, allocating a complete input/output graph for every live client. This is especially expensive for request-scoped clients in Cloudflare Workers, where a single client cannot own database sockets across requests.
Cache decoded
ParamGraphDatain aWeakMapkeyed by the generated client's immutableSerializedParamGraphobject. Each call still creates a freshParamGraphwrapper, string index and enum lookup. Different schema objects remain isolated, failed decodes are not cached, and no client, adapter, connection or query data is shared. This source change applies to the Prisma 7 runtimes rather than only the edge bundle.Testing performed
pnpm --filter @prisma/param-graph test: 17 tests pass (8 new wrapper/cache tests and 9 existing serialization tests). Before the implementation, the two cache-reuse assertions fail with 2/4 decodes instead of 1/2.pnpm --filter @prisma/param-graph build: CJS/ESM bundles and declarations build successfully; both built entry points pass a construction smoke check.git diff --checkpass.actionlint .github/workflows/test-template.ymlpasses. The package tests are added to the Linux client-internals Node matrix and the Windows/macOSno-dockerjob.The full Prisma Client functional/database suites have not been run in this checkout.
Reproduction and measurements
From the repository root after installing dependencies:
The committed script uses a synthetic graph: 4,096 input nodes, 4,096 output nodes, 16 fields per node and a 16-entry string table. It keeps 32 wrappers alive, measures the first construction separately, and measures additional retained heap after explicit GC. It performs no database work and verifies graph access on every wrapper. Copy the same script onto the base revision to compare.
Example run on Windows, Node v24.12.0, base
97ea359250335c1a174504c9b2b7e5fe030ab937:These are illustrative measurements of the ParamGraph factory, not end-to-end Prisma Client timings. The deliberately small string table does not represent the per-instance string-index cost of a large generated schema. Time measurements are not asserted by the unit tests.
Skill update
n/a: internal Prisma 7 runtime change. Package documentation and agent notes describe the reuse contract and standalone reproduction.
Checklist
v7contribution rules; the change is scoped to repeated deserialization.Notes for the reviewer
The cache assumes the generated serialized schema is not mutated after a successful decode; this is now explicit in the factory documentation. Weak keys avoid retaining discarded schema objects. The first decode remains eager, and query-compiler initialization is unchanged. Linking the issue with
Refsis intentional so merging this improvement does not close the broader startup report.Summary by CodeRabbit