Nuno Santos created OAK-12456:
---------------------------------

             Summary: Template.getPropertyTemplate(String) uses a linear scan; 
use binary search on the hash-sorted properties
                 Key: OAK-12456
                 URL: https://issues.apache.org/jira/browse/OAK-12456
             Project: Jackrabbit Oak
          Issue Type: Improvement
          Components: segment-tar
            Reporter: Nuno Santos


{{org.apache.jackrabbit.oak.segment.Template#getPropertyTemplate(String)}} 
looks up a property by name. It walks {{properties[]}} linearly until it 
reaches the first entry whose name hash is not smaller than the target hash. 
The array is already sorted by name hash (see the constructor), so every lookup 
costs O(n) in the number of properties of the node.

This method is called for every property read or write of every segment node 
({{SegmentNodeState.hasProperty/getProperty/...}} and 
{{DefaultSegmentWriter.writeNodeUncached}}). For nodes with many properties, 
this becomes expensive. {{writeNodeUncached}} does one lookup per property 
template for both {{state}} and {{before}}, so writing a node with n properties 
costs O(n²) hash comparisons.

h3. Proposal

Replace the linear prefix scan with a binary search for the lower bound of the 
target hash ({{lowerBoundByHash(int)}}). The hash-collision run walk that 
follows is unchanged, so behaviour is identical, including the tie-breaking on 
colliding hashes. This brings lookups to O(log n) and the node write to O(n log 
n).

h3. Evidence

JMH microbenchmark (JMH 1.37, JDK 25). Each lookup sweeps all properties once, 
and the numbers are per-lookup cost:

|| Properties || Linear scan || Binary search || Speedup ||
| 1 | 3.3 ns | 3.6 ns | ~1x (noise) |
| 8 | 4.6 ns | 5.0 ns | linear slightly faster |
| 32 | 9.4 ns | 7.2 ns | 1.3x |
| 128 | 42.0 ns | 10.6 ns | 4.0x |
| 1,024 | 346.5 ns | 46.5 ns | 7.5x |
| 8,192 | 2,790 ns | 83.6 ns | 33x |
| 40,000 | 13,826 ns | 117 ns | 118x |

Below about 16-32 properties the two are within noise. The crossover is 
decisive by 128 properties, which is a typical AEM content node.

Real-world impact, measured in an internal test:
* CPU profiling of a long-running deployment showed the thread that serializes 
all commits spending 60% of its CPU time in this method.
* In a test that commits a single node with about 10,000 properties every 15 
minutes, the pre-fix build showed write throughput collapsing to ~5% of 
baseline for 3+ minutes on each large write. After the fix, the dip lasts 
~20-30 seconds and recovers fully. Commit latency dropped from 2.9-6.1 s to 
under 0.5 s.
* In CPU profiling recordings of both builds over 12 minutes, samples in the 
commit path fell by about 99%, and total CPU samples fell by 62%.

h3. Tests

New unit tests in {{TemplateTest}} cover:
* no properties
* a single property
* multiple properties (odd count)
* a hash collision ({{"Aa"}} and {{"BB"}} share {{hashCode()}} 2112), which 
exercises the collision walk after the binary search

No behavioural change is intended.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to