Skip to content

Eliminate synchronized Observable/Vector and Long boxing in ElementBy… - #41537

Open
reuvenlax wants to merge 2 commits into
apache:masterfrom
reuvenlax:perf/element-byte-size-observer
Open

reuvenlax wants to merge 2 commits into
apache:masterfrom
reuvenlax:perf/element-byte-size-observer

Conversation

@reuvenlax

@reuvenlax reuvenlax commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Eliminate synchronized Observable/Vector and Long boxing in ElementByteSizeObserver

  • PrimitiveByteSizeObserver: add zero-field @internal abstract class extending Observer
    for primitive update(long) dispatch via primary supertype checks (avoiding JDK-8180450).
  • ElementByteSizeObserver: extend PrimitiveByteSizeObserver, add primitive update(long)
    fast path (exact long arithmetic when scalingFactor == 1.0), and remove class-level
    nullness suppression.
  • ElementByteSizeObservableIterator: replace java.util.Observable's synchronized Vector
    with an unsynchronized single-observer fast path plus immutable copy-on-write Observer[]
    snapshots, dispatching directly to ElementByteSizeObserver / PrimitiveByteSizeObserver
    and using exact Observer[].class checks.
  • ElementByteSizeObservableIterable: replace eager ArrayList with lazy null / single
    Observer / deduplicated Observer[] storage.
  • IterableLikeCoder.IteratorObserver: extend PrimitiveByteSizeObserver with branchless
    per-element overhead while preserving Observable forwarding on legacy update(Observable, Object).
  • Add unit tests (ElementByteSizeObservableIteratorTest, IterableCoderTest) and JMH
    benchmarks + smoke tests (ElementByteSizeObservableBenchmark[Test]).

@github-actions github-actions Bot 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.

📋 Review Summary

This PR optimizes element byte size observation on hot paths by adding a primitive update(long) overload to ElementByteSizeObserver, replacing java.util.Observable's synchronized Vector in ElementByteSizeObservableIterator with an unsynchronized single-observer fast path, and lazily allocating the observer list in ElementByteSizeObservableIterable. The changes are clean, preserve Observable's behavioral contract (including reverse notification order and deduplication), and are well-covered by unit tests and JMH benchmarks.

🔍 General Feedback

  • The single-observer fast path in ElementByteSizeObservableIterator and ElementByteSizeObserver extension in IterableLikeCoder.IteratorObserver effectively eliminate Long autoboxing and synchronization overhead during iteration.
  • Edge cases for Observable methods (addObserver, deleteObserver, notifyObservers, changed flag semantics) are thoroughly tested in ElementByteSizeObservableIteratorTest.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

@github-actions github-actions Bot added java core size/l L: 250-999 lines changed labels Nov 20, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @ahmedabu98 for label java.
Backup reviewer: Abacn (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

This pull request likely touches a core component ("core" label). Please review with scrutiny.

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

@codecov

codecov Bot commented Nov 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 58.66%. Comparing base (028a272) to head (9f3e794).
⚠️ Report is 33 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##             master   #41537   +/-   ##
=========================================
  Coverage     58.66%   58.66%           
  Complexity     2292     2292           
=========================================
  Files          1134     1134           
  Lines        170223   170223           
  Branches       1487     1487           
=========================================
  Hits          99856    99856           
  Misses        67826    67826           
  Partials       2541     2541           

☔ 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.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ahmedabu98 ahmedabu98 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.

LGTM

…teSizeObserver

- ElementByteSizeObserver: add primitive update(long) overload to avoid Long boxing.
- ElementByteSizeObservableIterator: replace java.util.Observable's synchronized Vector
  with an unsynchronized single-observer fast path (plus lazy ArrayList overflow) and
  dispatch directly to ElementByteSizeObserver.update(long).
- ElementByteSizeObservableIterable: lazily allocate observer list.
- IterableLikeCoder: avoid per-element Long boxing when observing element sizes.
- Add ElementByteSizeObservableIteratorTest and ElementByteSizeObservableBenchmark.

comments

comments
@reuvenlax
reuvenlax force-pushed the perf/element-byte-size-observer branch from 9f3e794 to 0fe6864 Compare November 25, 2026 05:44
@github-actions github-actions Bot added size/xl XL: >=1000 lines changed and removed size/l L: 250-999 lines changed labels Nov 25, 2026
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.

2 participants