Skip to content

perf(core): stop not(out().has()) at the first matched neighbor - #3486

Open
kccrcodes wants to merge 2 commits into
apache:masterfrom
kccrcodes:fix-3480-not-exists-limit
Open

kccrcodes wants to merge 2 commits into
apache:masterfrom
kccrcodes:fix-3480-not-exists-limit

Conversation

@kccrcodes

Copy link
Copy Markdown

Purpose of the PR

not(), filter(), where(traversal), and() and or() only check whether their child traversal has
any result (hasNext()). But when the child ends with out(label).has(...) on a high-degree vertex,
HugeGraph still read about 500 edges per start vertex before returning the first one:

  1. HugeVertexStep drops any limit when has() must be filtered after reading;
  2. BinaryEntryIterator merges up to INLINE_BATCH_SIZE (500) columns into one entry before returning;
  3. CachedGraphTransaction.fetchEdgeBatch reads up to 101 edges to decide whether to cache the result.

On RocksDB with 2,000 edges per vertex, not(out(high).has(k, v)) took ~21 ms per vertex, versus
~1 ms for the same check on a low-degree label.

Main Changes

  • Query.batchSizeHint() (hugegraph-struct): a new, optional hint for how many results the caller
    expects to need. It never limits results. It defaults to NO_LIMIT, is copied in copyBasic(),
    and is not part of equals/hashCode.
  • BinaryEntryIterator: when the query has a hint < 500, the first entry merges only hint columns,
    and each later entry doubles in size until it reaches 500. Without a hint, behaviour is unchanged.
  • CachedGraphTransaction.fetchEdgeBatch: the cache probe reads at most min(hint, 101) edges, and
    caches only when the source is exhausted, so a partial result is never cached. Without a hint, behaviour is unchanged.
  • HugeVertexStep.constructEdgesQuery: when a limit can't be pushed down because of filtering,
    it becomes a hint instead of being dropped (out().has().limit(n) benefits as well).
  • HugeVertexStepStrategy: a HugeVertexStep that is the end step of a local child of
    NotStep/TraversalFilterStep/ConnectiveStep gets hint 1. If anything follows the step
    (count(), path(), tree(), order(), range(), more steps), or the step sits in
    where() with labels, local(), optional() or at the root, it is not hinted.

A hint is used instead of limit(1) because a hard limit fails with uncommitted records in the
transaction ("not allowed to query with offset/limit when there are uncommitted records"). It would
also be wrong for HugeVertexStepByBatch IN-queries that cover several owner vertices, and the
backend can't apply a limit before has() filtering anyway.

Verifying these changes

  • Trivial rework / code cleanup without any test coverage. (No Need)
  • Already covered by existing tests: full unit-test (memory, 1489 tests) and core-test
    (memory and rocksdb, 1036 tests each) pass, including the paging test from [Bug] Paging returns the page-boundary record twice when limit is a multiple of 500 (BinaryEntryIterator; RocksDB and HStore) #3191.
  • Need tests and can be verified as follows:
    • BinaryBackendEntryTest#testEntryIteratorReadsSmallBatchesFirstWithHint: entry sizes
      [10] with no hint, [1, 2, 4, 3] with hint 1, and [10] with hint 501
    • CachedGraphTransactionTest#testHintedEdgeBatchProbesOnlyHintedEdgesForCache
    • TraversalUtilOptimizeTest#testVertexStepStrategyHintsExistenceCheck,
      #testVertexStepStrategyNotHintAllResultsNeeded, #testVertexStepStrategyKeepsLimitWithHint
    • EdgeCoreTest#testQueryExistenceOfAdjacentVerticesOverBatchSize: a vertex with 600 edges checked
      with not/filter/where/and/or, plus checks for the hint

Benchmark (RocksDB, 50 start vertices × 2,000 high edges, in-JVM median per query, ms):

g.V().hasLabel(..).filter(not(<child>)) child master this PR
out(low).has(keep, false) 0.67 0.73
out(high).has(keep, false) 20.83 1.25
out(high) 20.93 1.78
outE(high) 21.48 1.04
out(high).has(keep, false).limit(1) 21.48 1.38
out(high).has(keep, false), no neighbor matches (reads all edges) 326 318–323
low-degree out(..).has(..) (2–100 edges, cache-populate trade-off) 1.9 2.4–2.7
root out(high).count() (not hinted) 103 104–117

End-to-end HTTP median for the issue query: 28.3 ms → 6.6 ms.

Does this PR potentially affect the following parts?

  • Dependencies
  • Modify configurations
  • The public API
  • Other affects (typed here): adds a field to Query in hugegraph-struct (shared with hstore;
    backends that don't use it ignore it). Hinted queries that match 2–100 edges no longer
    populate the edge cache, but they still read from it.
  • Nope

Documentation Status

  • Doc - TODO
  • Doc - Done
  • Doc - No Need: no user-visible documentation is affected.

When a vertex step filters results after reading (e.g. out().has(k, v)),
its limit is reset to NO_LIMIT, so the backend merged up to 500 edges of
an owner vertex into the first entry and the edge cache probed up to 101
edges before the first result was returned.

Add Query.batchSizeHint(): the number of results the caller expects to
need. It never limits the results. HugeVertexStep keeps a limit it can't
push down as the hint, BinaryEntryIterator reads the first entry at the
hinted size and doubles it up to INLINE_BATCH_SIZE, and
CachedGraphTransaction doesn't read past the hint to fill the edge cache.
Queries without a hint behave as before.
not(), filter(), where(traversal), and() and or() only call hasNext() on
their child traversals, but HugeVertexStep read edges as if every result
was needed. On a vertex with 2,000 edges of one label,
not(out(label).has(k, v)) read about 500 edges per start vertex.

HugeVertexStepStrategy now marks a HugeVertexStep that ends such a child
traversal with a batch size hint of 1. A step followed by any other step
(count(), path(), tree(), order(), range() and so on) is left unchanged.

Fixes apache#3480
Copilot AI balanced review requested due to automatic review settings November 22, 2026 17:48

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@imbajin imbajin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Score: 8.0/10 · Confidence: medium

Blocking: no

Summary

No new actionable issues found in the batch hint and cache changes.

Evidence

Reviewed hint propagation, iterator batching, cache completeness, traversal strategy, regression tests and the available discussion with five independent lanes.

Validation notes

No tests or performance benchmarks were run independently; distributed backend behavior remains unverified and core CI is still running.

@codecov

codecov Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.30769% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.62%. Comparing base (8828d82) to head (3a2d13b).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...egraph/backend/serializer/BinaryEntryIterator.java 85.71% 0 Missing and 1 partial ⚠️
...e/hugegraph/traversal/optimize/HugeVertexStep.java 80.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3486      +/-   ##
============================================
+ Coverage     58.52%   58.62%   +0.10%     
- Complexity     2717     3105     +388     
============================================
  Files           779      780       +1     
  Lines         69439    69657     +218     
  Branches       9577     9609      +32     
============================================
+ Hits          40637    40836     +199     
- Misses        25108    25126      +18     
- Partials       3694     3695       +1     
Flag Coverage Δ *Carryforward flag
commons 96.37% <ø> (ø) Carriedforward from 8828d82
hstore 36.16% <34.61%> (-0.06%) ⬇️
pd 57.01% <ø> (+0.17%) ⬆️
server_hbase 42.53% <ø> (+0.04%) ⬆️ Carriedforward from 8828d82
server_memory 57.18% <92.30%> (+0.52%) ⬆️
server_rocksdb 42.86% <92.30%> (-0.15%) ⬇️
store 36.17% <ø> (-0.04%) ⬇️

*This pull request uses carry forward flags. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bitflicker64 bitflicker64 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Blocking: no. Score: 9/10. Summary: batchSizeHint only changes how BinaryEntryIterator and the edge cache probe split reads, never the results, and the focused unit tests plus EdgeCoreTest pass on memory and RocksDB. Hinted existence checks on vertices with 2-100 edges stop filling the edge cache, as the PR body notes.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Gremlin not(out().has()) is slower on an edge label with more matching neighbors

4 participants