Repository navigation
perf(sheet): recycle CellWriteHandlerContext across cells of a write - #1209
Draft
GOODBOY008 wants to merge 1 commit into
Draft
GOODBOY008 wants to merge 1 commit into
GOODBOY008 wants to merge 1 commit into
Conversation
GOODBOY008
force-pushed
the
perf/write-alloc-pr3
branch
2 times, most recently
from
October 10, 2026 01:54
e99dbfc to
784a860
Compare
GOODBOY008
marked this pull request as draft
October 10, 2026 02:07
GOODBOY008
force-pushed
the
perf/write-alloc-pr3
branch
2 times, most recently
from
October 10, 2026 14:05
3562ab8 to
c74df90
Compare
The write path allocated a fresh CellWriteHandlerContext for every cell; JFR allocation profiling (jdk.ObjectAllocationSample, byte-weighted) attributed ~12% of write-side allocation bytes to this single site — the largest fesod-internal write allocation. Recycle it Tomcat-style instead: the write session owns one instance that is reset at every cell boundary. - CellWriteHandlerContext.reset(...) restores every field to the state a freshly built context would have, including the fields only some write paths set (targetCellDataType, ignoreFillStyle) - the new CellWriteHandlerContextRecycler hosts the instance on the WriteWorkbookHolder (one per writer) and guards it: a nested write on the same writer falls back to a fresh context rather than resetting the live one, and release() drops the instance when the session finishes (also on exception) so no Row/Cell references survive a closed writer - all five cell-producing sites migrate (add x3, fill, header row); WriteHandlerUtils.createCellWriteHandlerContext stays unchanged - the context contract is documented on the class: it is only valid for the duration of one cell's handler chain — handlers must not retain it beyond afterCellDispose (the same contract Tomcat documents for recycled request objects) Signed-off-by: gongzhongqiang <[email protected]>
GOODBOY008
force-pushed
the
perf/write-alloc-pr3
branch
from
October 10, 2026 14:33
c74df90 to
4c1ec0e
Compare
This was referenced Oct 15, 2026
1 of 2 tasks
3 tasks done
3 tasks done
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.
Summary
Recycle the
CellWriteHandlerContextacross all cells of a write session instead of allocating a fresh one per cell — Tomcat-style owner-scoped recycling (the same patternorg.apache.catalina.connector.Requestuses): one mutable carrier per writer, reset at every cell boundary, no pool, no statics, no thread-locals.JFR allocation profiling (
jdk.ObjectAllocationSample, byte-weighted, MEDIUM XLSX write) attributes ~12% of write-side allocation bytes to this single allocation site (656 MB in the sampled profile) — the largest fesod-internal write allocation; only the POI/xmlbeans serialization layer above it allocates more.What changes
CellWriteHandlerContext.reset(...)— re-initializes every field to the state a freshly built context would have: the per-cell arguments plus nulls for the fields only some write paths ever set (targetCellDataTypeCSV branch,ignoreFillStylemulti-variable fill, the converter/originalValuefamily). The class javadoc documents the contract: the context is only valid for the duration of one cell's handler chain.CellWriteHandlerContextRecycler(new, ~100 lines) — hosts the single session instance and owns the guard rails:renew(...)returns the recycled instance reset for the next cell; while that instance is still in flight (a nested write on the same writer inside a cell callback) it falls back to a fresh allocation instead of resetting the live one — worst case degrades to today's behavior, never corrupts.cellCompleted(context)clears the in-flight mark on an identity match, so a nested write completing cannot free the outer cell's instance.release()drops the instance when the session finishes.WriteContextImpl.initCurrentWorkbookHolderand hosted on theWriteWorkbookHolder(following the existingworkbookWriteHandlerContextprecedent); executors cache it in a constructor-initialized field.finish(boolean onException)releases it at the top, so noRow/Cellreferences survive a closed writer — and becauseExcelBuilderImplalready routes exceptions tofinish(true), a session that dies mid-cell (e.g. a caughtExcelWriteDataConvertException) also detaches the instance. Note that exception snapshots row/column/cellData/property into its parent at construction.renew(...), pluscellCompleted(...)afterafterCellDispose.WriteHandlerUtils.createCellWriteHandlerContextstays unchanged and public.Compatibility
No API change (only additive:
resetand the recycler class). Handlers now receive a reused instance instead of a fresh one per cell — the documented contract (valid only during the callback, do not retain or key maps on it) matches what Tomcat documents for recycled request objects. Built-in handlers were audited on this baseline: none retain the context.Benchmark
Same-machine A/B (Apple M1, Zulu JDK 17;
FesodBenchmark.write, MEDIUM = 10,000 rows × 20 columns ≈ 200 K cells; avgt mode,-prof gc, 3 forks, 2×2 s warmup, 5×2 s measurement, n = 15 per config; merge base2c1e4b22vs this branch, both shaded jars built fresh and the PR jar verified to contain the recycler):The per-cell arithmetic confirms the mechanism: −88 B/cell on both formats — exactly one
CellWriteHandlerContext(18 reference fields + object header), matching the design estimate.gc.alloc.rate.normis deterministic (±0.001% across measurement iterations), so the allocation deltas sit hundreds of standard deviations from noise. Wall-clock is unchanged within the confidence intervals: this change targets allocation rate (GC pressure on sustained write workloads), not per-op latency, and the numbers show precisely that.Tests
mvn -pl fesod-sheet clean verify -Dmaven.test.skip=false(root pom defaults to skipping tests): 966 tests green, 8 new —reset)targetCellDataType,ignoreFillStyle, …)release()semanticsafterCellDisposesees exactly its own cell's state, and that all cells (header included) went through one recycled instanceSigned-off-by: gongzhongqiang [email protected]