Skip to content

perf(runtime): store instance fields in the object and share one slot for monitor, hash and VM data, cutting a small object from 107 to 32 bytes - #102

Merged
Project516 merged 3 commits into
nextfrom
p516/jobject-layout
Nov 23, 2026
Merged

Project516 merged 3 commits into
nextfrom
p516/jobject-layout

Conversation

@Project516

Copy link
Copy Markdown
Owner

What this changes

Every Java object was a JS object with cls, f (a separate array of its fields), mon, hash and x: about 112 B before any field. A parked virtual thread still held 1.3 to 1.6 KB of such objects, so Skynet (1,000,000 virtual threads in -Xmx1500m) ran out of memory.

  • The monitor, identity hash and VM data share one slot s: null, the hash once taken, or a Monitor that holds the hash and the VM data too. Compiled code gets an inline monitor fast path.
  • Instance fields are properties f0, f1, ... of the object itself (JField.key). One constructor is generated per pattern of field kinds, so classes with the same kinds share one engine shape. The interpreter uses a named load for the first 8 slots, and Unsafe offsets go through per-slot accessors.
  • An object whose class has a finalizer keeps its fields in a second object st, which the Finalizer holds, so the object can be collected while finalize() still sees the fields.
  • Boot snapshot format 3 to 4: older images are ignored and rebuilt.
  • AGENTS.md glossary: object layout.

Tests

Bytes per object (array slot included), next against this branch:

Object next branch
0 fields 74 17
2 fields 107 32
8 fields 154 80
Integer 97 28
ArrayList 112 40
HashMap 154 97
VirtualThread 916 572
parked virtual thread, depth 0 / 10 / in wait 2,523 / 3,323 / 3,062 2,077 / 2,862 / 2,705
  • Timing, 7 alternating rounds (median, next / branch): park ping-pong 67 / 52 ms, synchronized loop 221 / 197 ms, CPU bench total 561 / 545 ms, javac 6.1 / 6.0 s (heap after GC 175 / 175 MB), cold start 0.56 / 0.52 s, start from snapshot 0.34 / 0.32 s. Interpreter only: 2,172 / 2,135 ms.
  • Skynet (-Xmx1500m, 10 iterations) now passes, 28 to 43 s per iteration, peak RSS 1.85 GB. It ran out of memory before.
  • pnpm test: 270 pass, 0 fail, 3 skipped, also with a check that makes any read of the old f, mon, hash and x properties throw. New program test/java/ObjectLayout.java (including classes of 300 and 1,200 fields) matches HotSpot.
  • All 1,057 OpenJDK tests in passing.txt: 1,052 pass. The two multicast tests that fail and two of the three timeouts (DatagramSocketExample, EarlyOrDelayedParsing) do the same on next in the same environment. The third, java/net/httpclient/ResponsePublisher.java, timed out once under load after all 160 of its cases passed; it passed on next run alone.

Noticed, not fixed

  • In the full CPU bench, sort after objects and strings is about 17% slower because a 5 ms scavenge lands in it. Alone or after any other single benchmark it is equal, and steady-state fill and sort are equal.

@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: d236adc3-a16b-407d-ad76-8de8372bda26

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review
  • 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

@project516-review-bot project516-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This PR changes the Java object layout: instance fields are now stored as properties f0, f1... on the object itself instead of a separate f array; monitor, identity hash, and VM data share a single s slot. The change reduces object size significantly and enables Skynet to run in 1.5GB heap. However, cloneObject incorrectly copies the s slot (monitor/hash/VM data) to the clone via setData(copy, dataOf(o)), causing the clone to share the monitor with the original.

Not approved: the diff of src/natives/stackwalk.ts, src/natives/strings.ts, src/runtime.ts, src/snapshot.ts, test/java/ObjectLayout.java was too large to read.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict comment

@Project516

Copy link
Copy Markdown
Owner Author

On cloneObject: the clone does not share the original's monitor. copy is new, so copy.s is null, and setData(copy, x) then calls monitorFor(copy), which makes a new Monitor for the copy and stores x in it (src/runtime.ts, setData and monitorFor). When x is undefined nothing is created. The original's monitor state (owner, count, waiters) and its identity hash are not copied, so the clone gets its own hash when asked. This matches the old code, which copied only the VM data (copy.x = o.x), not mon or hash. ObjectLayout checks clone, hash and monitor on the same objects and matches HotSpot.

@Project516

Copy link
Copy Markdown
Owner Author

Review of the files the review bot could not read (src/runtime.ts, src/snapshot.ts, src/natives/strings.ts, src/natives/stackwalk.ts, test/java/ObjectLayout.java) at edf898a. No correctness bugs found.

Checked:

  • Slots and keys: kindsOf walks superclasses first in declared order, the same order jvm.ts assigns slots in, and Unsafe offsets are the slot, so readField and the other helpers hit f<slot>. strings.ts resolves keys through findField, which covers inherited fields.
  • Defaults match defaultValue: long 0n, references null, the rest 0.
  • s: monitorFor carries the hash into a new Monitor, setHash writes into an existing one, setData(o, undefined) creates nothing, and the JIT's inline monitor path tests for an object in s.
  • Finalizable objects: the accessors go to st, the Finalizer watches st, and a restored Finalizer picks up the decoded st.
  • Snapshot: format 4. Only idle monitors (no owner, count 0, no waiters) are dropped, so waiting threads find theirs. Hash is set at creation, fields in fill, and a decoded Monitor takes the hash before setData reuses it.
  • Generated dump/load source is valid, including a class with no fields.

Minor, not bugs:

  • wrapperOver runs the finalizable constructor, which allocates a storage object that st then replaces: one extra short-lived object per resurrection.
  • view() in misc.ts keeps its DataView through setData, so a primitive array that Unsafe reads at another width now gets a Monitor to hold it.
  • ObjectLayout does not cover a finalizable class, a snapshot round trip, or Unsafe on a slot of 4096 or more (the non-generated fallback). The full suite and the OpenJDK tests exercise the first two.

@project516-review-bot project516-review-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This PR overhauls the Java object layout: instance fields move from a separate f array to inline properties f0, f1…; the monitor, identity hash, and VM data share a single slot s (a Monitor object holds all three once any is needed); finalizable objects keep their fields in a second st object. The interpreter, JIT, JVM, and all natives are updated to use fld.key / dataOf / setData / monitorOf / monitorFor / fieldsOf / setFields / hashOf / setHash / readField / writeField / casField / addField / swapField instead of the old f[], mon, hash, x accesses. Boot snapshot format bumps from 3 to 4. Tests pass (270/0/3, Skynet now passes under -Xmx1500m, 1052/1057 OpenJDK tests pass).

Not approved: the diff of src/natives/stackwalk.ts, src/natives/strings.ts, src/runtime.ts, src/snapshot.ts, test/java/ObjectLayout.java was too large to read.


review-bot, model nvidia/nemotron-3-ultra-550b-a55b:free, verdict comment

@Project516
Project516 merged commit 16239b4 into next Nov 23, 2026
12 checks passed
@Project516
Project516 deleted the p516/jobject-layout branch November 23, 2026 14:44
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