Opt-in JIT-compiled formatter for the read path - reduce item formatting by 98% - #586
Opt-in JIT-compiled formatter for the read path - reduce item formatting by 98%#586anatolzak wants to merge 8 commits into
Conversation
✅ Deploy Preview for electrodb-dev canceled.
|
|
hey @tywalch 👋! finally got to work on the JIT optimization I shared with you a while ago. to give you an idea of the improvement, I have an API route, which takes in total under a second, that processes a lot of items, and I measured the total time spent in there's still more on the table. right now the doc client unmarshalls the raw DynamoDB response, which is also quite slow because it's megamorphic, and then ElectroDB walks the result again to format it. we can fold the unmarshalling into the JIT formatter so it will be monomorphic. in my benchmarks that unmarshall pass costs as much as ~50% of ElectroDB's normal formatting (in the same route, 88ms unmarshalling on top of the 175ms format). I manually tested a fused unmarshall + JIT formatter and it's also about ~95% faster. so combined, that's ~263ms of unmarshall+format CPU per request on this route today. I will try to work on the unmarshall stuff at some point, but even this PR should already provide much relief and reduce the cpu work done in the formatter. one nice thing about this PR is that the whole test suite now runs with thanks in advance for taking the time to review this 🙏 |
… in the shared paths Map
2049ecb to
73fd0cc
Compare
Closes #585
What
Adds an opt-in, JIT-compiled document formatter for the read path (
get/query/scan/parse). Instead of re-walking the schema for every item through the interpreted formatter, ElectroDB compiles a formatter specialized to a single entity's schema once, atEntityconstruction, and reuses it. Output is byte-for-byte identical; only the per-item formatting cost changes.Design & safety
new Entity(schema, { table, client, compile: true }), or globally via theELECTRODB_COMPILEenv var.ELECTRODB_COMPILE=on, which forces compilation and throwsCompilationFailedwhere runtime codegen is unavailable (e.g. strict CSP,new Functionblocked).Correctness
ELECTRODB_COMPILE=verifyruns every read through both the compiled and interpreted paths and throws on any divergence. The full suite now runs under verify mode by default (npm run test:unit:verify, wired intotest:run), so both paths are exercised on every CI run in a single pass.The generated function body is snapshotted in
test/snapshots/compile/*.txtso codegen changes surface as a reviewable diff:npm run test:snapshots— verify the generated source hasn't drifted from the committed snapshots.npm run test:snapshots:update— regenerate the snapshots (review the diff to confirm the change makes sense, then commit).