perf(sdk): cut checkpoint and batch hot-path CPU self-time by ~48%#717
Merged
Conversation
ParidelPooya
force-pushed
the
perf/sdk-hot-path-optimizations
branch
5 times, most recently
from
July 14, 2026 20:49
3767d27 to
0b21469
Compare
yaythomas
reviewed
Jul 14, 2026
yaythomas
reviewed
Jul 14, 2026
CPU profiling of the integration examples against the local runner (with SDK source-level attribution) showed ~78% of SDK self-time concentrated in checkpoint-manager.ts, serdes.ts, and batch-result.ts. This change removes the redundant work in those hot paths without any wire-format or replay semantic changes: - checkpoint-manager: compute checkpoint item wire size once at enqueue using Buffer.byteLength (was JSON.stringify + TextEncoder per item on every processQueue pass), drain batches with a cursor + single slice (was O(n^2) shift), resolve waiting operations via a hashed-id reverse index (was MD5-hashing every tracked operation per status change), and make hot-path log arguments lazy. - batch-result: compute success/failure/started counts in a single pass at construction and memoize succeeded()/failed()/started(). Native #private fields keep JSON.stringify output byte-identical. - concurrent-execution-handler: Map-based index join for itemStatuses (was O(n^2) find-inside-map). - step-id-utils: memoize hashId (bounded cache, 10k entries). - logger: cache the DURABLE_VERBOSE_MODE flag at module load (with refreshLogConfig() for tests) and support thunk data args so disabled logging skips argument construction. refreshLogConfig() is exported from the public index for callers that mutate the env var after module load: run-durable.ts refreshes after its assignment, and the testing CLI's setupEnvironment refreshes it (feature-detected for older peer SDKs, whose module cache freezes the flag at CLI startup) and also sets the env var before the handler import to cover dual-SDK-instance module graphs. Also adds scripts/profile-sdk.ts (profiles examples against copied SDK source, same mechanism as the coverage scripts). Measured with identical profile parameters (5 iterations, 100us sampling): total SDK self-time 4438 -> 2310 hits (-47.9%); checkpoint-manager -66%, logger -98%, step-id-utils -77%, batch-result -23%. Validation: 1057 SDK unit tests, 182 example history-assertion tests, and 2 ESM vitest tests pass; tsc, eslint, prettier clean.
ParidelPooya
force-pushed
the
perf/sdk-hot-path-optimizations
branch
from
July 14, 2026 21:22
0b21469 to
e240d6c
Compare
yaythomas
approved these changes
Jul 14, 2026
ParidelPooya
marked this pull request as ready for review
July 14, 2026 21:38
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.
CPU profiling of the integration examples against the local test runner (with SDK
source-level attribution) showed ~78% of SDK CPU self-time concentrated in three files:
checkpoint-manager.ts,serdes.ts, andbatch-result.ts. This PR removes the redundantwork in those hot paths with no wire-format or replay-semantic changes — checkpointed
payload shapes and event histories are byte-identical.
Changes:
checkpoint-manager.tsBuffer.byteLength(previously
JSON.stringify+TextEncoder.encodeper item on everyprocessQueuepass — for large payloads this re-serialized 100KB+ items repeatedly just to read a length)
slice()(previously O(n²) per-itemshift())tracked operation on each status change)
batch-result.ts— success/failure/started counts computed in a single pass atconstruction;
succeeded()/failed()/started()memoized. Native#privatefields keepJSON.stringifyoutput identical to the previous getter-based implementation.concurrent-execution-handler.ts—Map-based index join foritemStatuses(previously O(n²)
find-inside-map)step-id-utils.ts—hashIdmemoized (bounded cache, 10k entries)logger.ts—DURABLE_VERBOSE_MODEcached at module load (readingprocess.envis anative call, and
log()sits in checkpoint hot paths);refreshLogConfig()added forcallers that mutate the env var after load.
run-durable.tsrefreshes the cache after itsassignment, and the testing CLI now calls
setupEnvironmentbefore the handler importloads the SDK.
scripts/profile-sdk.ts(examples) — new profiling tool that runs examples against thecopied SDK source (same mechanism as the coverage scripts) and emits Chrome
DevTools-compatible
.cpuprofilefiles with per-SDK-file attribution.Demo/Screenshots
Profile comparison (identical parameters: 5 iterations, 100µs sampling, same example set):
utils/checkpoint/checkpoint-manager.tsutils/serdes/serdes.tshandlers/concurrent-execution-handler/batch-result.tsutils/logger/logger.tsutils/step-id-utils/step-id-utils.tsReproduce with:
npx tsx scripts/profile-sdk.ts --cpu --examples comprehensive-operations,map/large-scale,parallel/basic,step/with-retry,promise/all --iterations 5