[
https://issues.apache.org/jira/browse/CALCITE-7965?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18150552#comment-18150552
]
Bruno Volpato edited comment on CALCITE-7965 at 11/24/26, 10:19 PM:
--------------------------------------------------------------------
Good point on avoiding a caching layer. Checking the JFR caller profile showed
that >90% of SingleRel.getInputs(), BiRel.getInputs(), and SingletonIterator
allocations come from a few internal VolcanoPlanner hot paths (RelSubset parent
traversal, TopDownRuleDriver, VolcanoRuleCall.matchRecurse, and
VolcanoPlanner.getCost / AbstractRelNode.onRegister), so we do not need
cachedInputs to get the savings.
I prototyped and benchmarked two cache-free alternatives against the original
PR on OpenJDK 21 (interleaved fresh-JVM runs, exact allocations via
ThreadMXBean, identical plans and costs):
* Internal iteration (RelNode.forEachInput(Consumer) and anyInput(Predicate),
cache-free): On join6 TopDown this only cut allocations by 25.9% and wall-clock
by 0.8% (vs 46.8% / 7.9% for cachedInputs), and on tpch_q2 Iterative 9.8% /
3.7%. JFR showed that because RelSet.parents is megamorphic (15+ RelNode
subclasses), C2 failed to inline and scalar-replace capturing lambdas like
{{parent.anyInput(in -> in == this)}}: {{RelSubset$$Lambda}} became the #1
allocation site in the JVM (22.1% of all allocations on join6 TopDown; 6.7% on
tpch_q2), and megamorphic interface dispatch pushed RelSubset.getParentRels
self CPU from 0.88% to 6.15%. Several hot loops also need arity checks ({{size
== 1}}), reverse traversal, or early-null folds (VolcanoPlanner.getCost,
fixUpInputs, AbstractRelNode.onRegister, VolcanoRuleCall ordered loops), which
do not fit Consumer/Predicate cleanly.
* Cache-free indexed access ({{default int getInputCount()}} + existing
{{RelNode.getInput(int)}} overridden on SingleRel and BiRel): Eliminates the
caching layer and beats cachedInputs on allocations across all workloads
(-48.4% on join6 TopDown, saving 3.47 GB/query; -17.1% on tpch_q2 Iterative).
Keeping a commented SingleRel/BiRel fast path in RelSubset (falling back to
getInputCount()/getInput\(i\)) also avoids the megamorphic invokeinterface
penalty in RelSubset.getParentRels (0.64% vs 6.12% self CPU).
I have pivoted PR #5448 to this cache-free {{RelNode.getInputCount()}} +
{{getInput(int)}} design, dropped {{cachedInputs}} completely, and added
comments in RelSubset explaining the fast path. Final interleaved A/B numbers
vs main:
* join6 TopDown: -11.2% median time (-9.6% mean), -48.4% allocations (7,155.6
-> 3,689.3 MB)
* join5 TopDown: -7.7% median time, -28.7% allocations
* tpch_q9 TopDown: -4.6% median time, -24.0% allocations
* tpch_q2 Iterative: -6.5% median time, -17.1% allocations
* wide6 Iterative: -3.5% median time, -10.8% allocations
* join6 Iterative: -4.0% median time, -9.8% allocations
was (Author: [email protected]):
Good point on avoiding a caching layer. Checking the JFR caller profile showed
that >90% of SingleRel.getInputs(), BiRel.getInputs(), and SingletonIterator
allocations come from a few internal VolcanoPlanner hot paths (RelSubset parent
traversal, TopDownRuleDriver, VolcanoRuleCall.matchRecurse, and
VolcanoPlanner.getCost / AbstractRelNode.onRegister), so we do not need
cachedInputs to get the savings.
I prototyped and benchmarked two cache-free alternatives against the original
PR on OpenJDK 21 (interleaved fresh-JVM runs, exact allocations via
ThreadMXBean, identical plans and costs):
* Internal iteration (RelNode.forEachInput(Consumer) and anyInput(Predicate),
cache-free): On join6 TopDown this only cut allocations by 25.9% and wall-clock
by 0.8% (vs 46.8% / 7.9% for cachedInputs), and on tpch_q2 Iterative 9.8% /
3.7%. JFR showed that because RelSet.parents is megamorphic (15+ RelNode
subclasses), C2 failed to inline and scalar-replace capturing lambdas like
{{parent.anyInput(in -> in == this)}}: {{RelSubset$$Lambda}} became the #1
allocation site in the JVM (22.1% of all allocations on join6 TopDown; 6.7% on
tpch_q2), and megamorphic interface dispatch pushed RelSubset.getParentRels
self CPU from 0.88% to 6.15%. Several hot loops also need arity checks ({{size
== 1}}), reverse traversal, or early-null folds (VolcanoPlanner.getCost,
fixUpInputs, AbstractRelNode.onRegister, VolcanoRuleCall ordered loops), which
do not fit Consumer/Predicate cleanly.
* Cache-free indexed access ({{default int getInputCount()}} + existing
{{RelNode.getInput(int)}} overridden on SingleRel and BiRel): Eliminates the
caching layer and beats cachedInputs on allocations across all workloads
(-48.4% on join6 TopDown, saving 3.47 GB/query; -17.1% on tpch_q2 Iterative).
Keeping a commented SingleRel/BiRel fast path in RelSubset (falling back to
getInputCount()/getInput(i)) also avoids the megamorphic invokeinterface
penalty in RelSubset.getParentRels (0.64% vs 6.12% self CPU).
I have pivoted PR #5448 to this cache-free {{RelNode.getInputCount()}} +
{{getInput(int)}} design, dropped {{cachedInputs}} completely, and added
comments in RelSubset explaining the fast path. Final interleaved A/B numbers
vs main:
* join6 TopDown: -11.2% median time (-9.6% mean), -48.4% allocations (7,155.6
-> 3,689.3 MB)
* join5 TopDown: -7.7% median time, -28.7% allocations
* tpch_q9 TopDown: -4.6% median time, -24.0% allocations
* tpch_q2 Iterative: -6.5% median time, -17.1% allocations
* wide6 Iterative: -3.5% median time, -10.8% allocations
* join6 Iterative: -4.0% median time, -9.8% allocations
> Add RelNode.getInputCount and avoid getInputs allocations in VolcanoPlanner
> ---------------------------------------------------------------------------
>
> Key: CALCITE-7965
> URL: https://issues.apache.org/jira/browse/CALCITE-7965
> Project: Calcite
> Issue Type: Improvement
> Components: core
> Reporter: Bruno Volpato
> Priority: Minor
> Labels: pull-request-available
>
> Profiling VolcanoPlanner across multi-way join and TPC-H queries (under both
> TopDownRuleDriver and IterativeRuleDriver) shows that SingleRel.getInputs(),
> BiRel.getInputs(), and RelSubset.inputSubsets() are the largest source of
> transient allocations during optimization:
> * SingleRel and BiRel do not override AbstractRelNode.getInput(int), so even
> rel.getInput(0) allocates a fresh ImmutableList.of(input) or
> FlatLists.of(left, right) on every call (10% to 18% of all allocations under
> TopDown).
> * RelSubset.getParents(), getParentSubsets(), and getParentRels() wrap parent
> inputs through inputSubsets(parent), allocating a SingletonIterator for every
> SingleRel parent (up to 23% of all allocations and 2.5% to 4.9% self CPU).
> We can eliminate these allocations cleanly by overriding getInput(int) on
> SingleRel and BiRel, caching their immutable input lists with an identity
> check against the current input fields (so direct protected field writes in
> subclasses remain safe), and fast-pathing SingleRel and BiRel parents in
> RelSubset without iterator wrappers.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)