Skip to content

fix(trimmer): avoid repeated serialization in batch API - #314

Merged
LuoShiying merged 1 commit into
mainfrom
fix/recursive-dump-shared-idl-20261118
Nov 18, 2026
Merged

LuoShiying merged 1 commit into
mainfrom
fix/recursive-dump-shared-idl-20261118

Conversation

@LuoShiying

@LuoShiying LuoShiying commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Problem and root cause

TrimBatchContentWithConfig calls recursiveDump after trimming. The helper follows every include path and calls dump.DumpIDL before assigning the result to a filename-keyed output map. Shared include subgraphs are therefore serialized repeatedly, even though each filename appears only once in the returned map. Repeated diamonds make the serialization work grow with the number of paths rather than the number of files.

The CLI deduplication in #311 covers recurseDump in tool/trimmer/main.go; it does not cover this batch API helper.

Change

Return immediately when the filename is already present in the per-invocation output map. This adds three lines to the helper and leaves the successful path/content output unchanged.

The parser caches ASTs by resolved filename, the public API checks include cycles, and each API invocation creates its own output map. The guard checks key presence, including an empty serialized file; successful serialization is still recorded before visiting includes. Errors still propagate to the caller. No process-wide cache or concurrency changes are introduced.

Reproduction

The added tests use synthetic IDLs only.

  1. Check out this PR and run:

    go test ./tool/trimmer/trim -run 'TestRecursiveDumpDedup|TestRecursiveDumpCompletedASTDoesNoSerializationWork' -count=1 -v
  2. From a checkout of the PR commit, copy the new test into a disposable worktree at the unmodified PR base:

    repro_dir="$(mktemp -d)"
    git worktree add --detach "$repro_dir" 6e9c23bcd91ac44fb9bd84f958b62d49ab3ec7b9
    git show HEAD:tool/trimmer/trim/recursive_dump_dedup_test.go > "$repro_dir/tool/trimmer/trim/recursive_dump_dedup_test.go"
    (
      cd "$repro_dir"
      go test ./tool/trimmer/trim -run TestRecursiveDumpCompletedASTDoesNoSerializationWork -count=1 -v
    )

    Before the fix this fails with completed file was serialized again: 11 allocs. With the fix it passes with zero serialization allocations. The input is a parsed, valid root/leaf include graph with its actual completed output; no wall-clock threshold or dump hook is used.

  3. TestRecursiveDumpDedupPreservesOracle compares every output byte with the old traversal. Its depth-14 shared DAG has 30 unique files and causes 49,151 dump calls in the old traversal. The final maps are identical after deduplication. Other tests cover empty output, independent invocations, and parallel calls using independent ASTs/maps.

Validation

Base: 6e9c23bcd91ac44fb9bd84f958b62d49ab3ec7b9 (main; the repository currently exposes no develop branch).

  • Regression fails before the three-line fix and passes after it.
  • go test ./tool/trimmer/... passes.
  • go test -race ./... passes.
  • go vet -stdmethods=false ./... passes (the repository Makefile uses the same vet flag).
  • Generator integration suites pass: fastgo, fieldmask, options (102 cases), and unknown_fields. The last suite was rerun separately after allowing its missing public dependency into the Go cache; the first attempt stopped on a local cache-lock permission error.
  • gofmt and git diff --check pass.
  • Synthetic depth-14 DAG benchmark, Go 1.24.0, macOS/arm64, Apple M3 Pro: 7,092 ns/op, 8,372 B/op, 175 allocs/op. This is a local microbenchmark, not a production latency claim.

The parallel test covers this helper with independent ASTs/maps; it does not claim that the entire trimming API is safe for concurrent use.

Downstream adoption

Consumers need to update the github.com/cloudwego/thriftgo module dependency to a resolvable upstream commit/version containing this fix and record the resolved version and checksums. Updating the standalone thriftgo executable does not update an application that imports the batch trimming API. No downstream configuration, deployment, merge, or release is part of this PR.

The current PR head a37e41fbe7b77e71f679e0bde28f526a2c221425 resolves through GoProxy as v0.4.6-0.20261118083004-a37e41fbe7b7. The downloaded source and module files match the submitted commit. This is an unmerged PR commit, not a release; downstream dependency upgrades still need their own review and integration checks.

@CLAassistant

CLAassistant commented Nov 18, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@LuoShiying
LuoShiying marked this pull request as ready for review November 18, 2026 08:41
@LuoShiying
LuoShiying requested a review from a team as a code owner November 18, 2026 08:41
@LuoShiying
LuoShiying merged commit 38f3e54 into main Nov 18, 2026
6 checks passed
@LuoShiying
LuoShiying deleted the fix/recursive-dump-shared-idl-20261118 branch November 18, 2026 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants