Skip to content

perf(workflow-core): reuse one SizeOf engine for tuple sizing - #9618

Draft
Matthew-DWM wants to merge 2 commits into
apache:mainfrom
Matthew-DWM:perf/reuse-sizeof-engine
Draft

Matthew-DWM wants to merge 2 commits into
apache:mainfrom
Matthew-DWM:perf/reuse-sizeof-engine

Conversation

@Matthew-DWM

Copy link
Copy Markdown

What changes were proposed in this PR?

  • Tuple computed inMemSize with SizeOf.newInstance().deepSizeOf(this), so every row built a new sizing engine. On JDK 17 the sizeof library can't attach its agent, so each call first built an AgentSizeOf that throws, then an UnsafeSizeOf, and every new engine started with empty reflection caches.
  • Tuple now keeps one engine in its companion object and still calls deepSizeOf(this) when a row is built. The walk is the same, so every row's inMemSize (used by flow control, operator statistics and the Python worker queue) is unchanged. Each deepSizeOf call keeps its own traversal state and the caches are thread-safe, so workers can share the engine.
Before:  each Tuple -> new SizeOf engine    (AgentSizeOf throws, empty caches) -> deepSizeOf
After:   each Tuple -> shared SizeOf engine (warm caches)                      -> deepSizeOf, same bytes

Local workflow runs (median of several runs):

Workflow Rows main This PR
CSV → count 100K 2.11 s 1.35 s (−36%)
CSV → 4 projections → count 100K 1.93 s 1.28 s (−34%)
CSV → count 1M 17.2 s 11.7 s (−32%)
CSV → hash join → count 200K 24.9 s 18.5 s (−26%)
CSV → sort → count 1M 65.3 s 54.1 s (−17%)
CSV → Python UDF → count 200K 90.4 s 91.2 s (no change)

Tradeoff: the engine's soft reflection cache can keep classes compiled at runtime (for example a Java UDF) loaded until memory runs low. It never holds row values.

Any related issues, documentation, discussions?

Closes #9617.

Related: #9614, which makes the same inMemSize line lazy.

How was this PR tested?

  • New TupleSpec cases check that inMemSize equals what a brand-new SizeOf engine reports for: every attribute type, objects shared within a row, ANY values with cycles and nested tuples, the size taken at construction, Attribute/Schema subclass state, and 8 threads building rows at once. A new FlowControlSpec case sends a frame of real tuples at exactly its credit and stashes it one byte short. All new cases pass on main and with this change; a lazily computed size or a cached schema size makes some of them fail.
sbt "WorkflowCore/testOnly org.apache.texera.amber.core.tuple.*" \
    "WorkflowExecutionService/testOnly *FlowControlSpec *DataPayloadSpec *WorkerBatchInternalQueueSpec *AmberMessageEnvelopesSpec"
sbt scalafmtCheckAll "scalafixAll --check"

182 and 52 tests pass; lint is clean.

  • The unit (skip-integration) and integration suites of every module that depends on WorkflowCore give the same results as on main.
  • Sizes of 30,000 random rows, compared row by row between main and this PR in separate JVMs (different JVM memory layouts, sizeof 0.4.3 and 0.4.4, 8 threads): identical.

Was this PR authored or co-authored using generative AI tooling?

Generated-by: Claude Code (Claude Opus 5.5)

Tuple.inMemSize is the byte charge that flow control, operator statistics and
the Python worker queue rely on. Pin it before changing how it is computed:
each new TupleSpec case compares a tuple's charge with what a brand-new SizeOf
engine reports for the same tuple, across every attribute type, objects shared
within a row, ANY values with cycles and nested tuples, the snapshot taken at
construction, Attribute and Schema subclass state, and rows built concurrently.
A FlowControlSpec case pins admission of a frame of real tuples at exactly its
credit, and stashing one byte short.
Tuple computed inMemSize with SizeOf.newInstance().deepSizeOf(this), so every
row built a new sizing engine. On JDK 17 the library cannot attach its agent to
the running JVM, so each newInstance() built an AgentSizeOf, caught the
UnsupportedOperationException it threw, then built an UnsafeSizeOf, and the
walk that followed rebuilt reflection caches for every class it reached.

Tuple now keeps one engine in its companion object and still calls
deepSizeOf(this) eagerly in the constructor, so each row's charge is the same.
deepSizeOf keeps each walk's traversal state local to the call and caches only
per-class facts in concurrent maps, so the shared engine is thread-safe.
@github-actions

Copy link
Copy Markdown
Contributor

Automated Reviewer Suggestions

Based on the git blame history of the changed files, we recommend the following reviewers:

  • Contributors with relevant context: @aglinxinyuan
    You can notify them by mentioning @aglinxinyuan in a comment.

@Matthew-DWM Matthew-DWM changed the title Perf/reuse sizeof engine perf(workflow-core): reuse one SizeOf engine for tuple sizing Nov 22, 2026
@Matthew-DWM
Matthew-DWM marked this pull request as draft November 23, 2026 17:49

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Reuse one SizeOf engine for Tuple.inMemSize instead of creating one per row

1 participant