Skip to content

perf: ArrayBuffer.from without the isDirect() call - #2076

Open
triniwiz wants to merge 1 commit into
mainfrom
perf/arraybuffer-from
Open

triniwiz wants to merge 1 commit into
mainfrom
perf/arraybuffer-from

Conversation

@triniwiz

@triniwiz triniwiz commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

What

ArrayBuffer.from(byteBuffer) called ByteBuffer.isDirect() in Java to choose between the direct and copying paths. GetDirectBufferAddress already returns null for a heap buffer, so the null check now picks the path and the Java call is gone. A direct buffer whose address is unavailable falls back to the copying path, which works for both kinds.

The nativeObject key set on the returned buffer is now one internalized v8::Eternal string, so a new string is no longer made on every call.

Measurements

Measured together with #2075 (same setup and numbers there): grid-dashboard and tree-fanout attached 0.96, other scenarios flat. This part on its own removes one JNI call into Java and one string allocation per ArrayBuffer.from.

Testing

  • :runtime:assembleRelease -Poptimized -Pabis=arm64-v8a builds; ran in the benchmark app as above.
  • The runtime test suite has not been run.

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of Java byte buffers when creating array buffers. Direct buffers can share their underlying storage, while other buffers retain their remaining bytes through a copy. This helps ensure buffer contents are available in the resulting array buffer across both buffer types.

GetDirectBufferAddress already returns null for a heap buffer, so the
Java isDirect() call is not needed to pick the path. The "nativeObject"
key is now one internalized string instead of a new string per call.
@coderabbitai

coderabbitai Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cd630ee9-65de-43f4-b8b6-b6d796337fa8
Commits

Reviewing files that changed from the base of the PR and between bdda733 and 17268bf.

Files selected for processing: 2
  • test-app/runtime/src/main/cpp/ArrayBufferHelper.cpp
  • test-app/runtime/src/main/cpp/ArrayBufferHelper.h

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 2 remain after this review.

Walkthrough

ArrayBufferHelper now checks the direct buffer address to select a direct backing store or copy the remaining bytes. It also caches the "nativeObject" V8 key used to attach the Java object to the returned array buffer.

Changes: ArrayBuffer conversion

Layer / File(s) Summary
Backing-store selection and object attachment
test-app/runtime/src/main/cpp/ArrayBufferHelper.cpp, test-app/runtime/src/main/cpp/ArrayBufferHelper.h
The helper checks GetDirectBufferAddress() to select a direct backing store or copy the remaining bytes into a new backing store. It replaces the JNI method ID with a cached V8 key for "nativeObject" and uses that key when attaching the Java object.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Refactor

Suggested reviewers: edusperoni

Merge Risk: Minimal · up to 17268

No actionable merge-blocking risk is established; the change is mergeable with normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title names the main change: ArrayBuffer.from no longer calls Java ByteBuffer.isDirect(). The raw summary confirms ArrayBufferHelper stops calling isDirect() and uses GetDirectBufferAddress() with…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: ArrayBuffer.from no longer calls Java ByteBuffer.isDirect(). The raw summary confirms ArrayBufferHelper stops calling isDirect() and uses GetDirectBufferAddress() with a null check. The title also has the conventional 'perf:' prefix. It omits the secondary nativeObject key change, which is acceptable because the title does not need every detail.

Full details: Docstring Coverage

Explanation

Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 2 functions across 2 files.

Resolution

Write docstrings for the functions missing them to satisfy the coverage threshold.

Finishing Touches
Generate docstrings
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit checks the buffer’s way,
A direct address leads the day.
If none is found, the bytes are copied,
A cached key keeps the object adopted.
Then off the bunny bounds away.

@triniwiz
triniwiz marked this pull request as ready for review November 2, 2026 22:08

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant