<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>JVM on Trevor's Blog</title><link>https://hitrevor.com/en/tags/jvm/</link><description>Recent content in JVM on Trevor's Blog</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Thu, 03 Dec 2026 07:10:29 +0800</lastBuildDate><atom:link href="https://hitrevor.com/en/tags/jvm/index.xml" rel="self" type="application/rss+xml"/><item><title>Kotlin 2.5's full value classes didn't get any faster on my machine</title><link>https://hitrevor.com/en/posts/k25-value/</link><pubDate>Thu, 03 Dec 2026 07:10:29 +0800</pubDate><guid>https://hitrevor.com/en/posts/k25-value/</guid><description>Same two doubles: a value class has 5 fewer methods and 21% less bytecode, yet the same arithmetic over 20M points ran 4.6% slower.</description><content:encoded><![CDATA[<p>Kotlin 2.5.0 is out. The headline is that Kotlin/Wasm went Stable, but for someone writing Kotlin Multiplatform the real news is full value classes: multiple properties, immutable, no generated <code>copy</code> or <code>componentN</code>.</p>
<p>I compiled them on this machine. Three conclusions up front:</p>
<ul>
<li><strong>It is subtraction, not addition.</strong> Same fields, five fewer methods, 21% less bytecode than a data class</li>
<li><strong>At call sites it is still an object.</strong> Nothing gets erased, so boxing and the null check stay</li>
<li><strong>It did not get faster.</strong> Over 20M points, the value class ran 4.6% slower than the data class, and 21.6% slower than a flat array</li>
</ul>
<h2 id="how-the-experiment-was-set-up">How the experiment was set up</h2>
<p>Kotlin 2.5.0 command line compiler, JDK 21.0.13.1, one file holding three equivalent types, all compiled in the same unit.</p>
<p><img alt="Same data, three declarations" loading="lazy" src="/images/k25-value/fig1.png"></p>
<p>Full value classes need a flag. Without it the compiler refuses, in these exact words:</p>
<blockquote>
<p>the feature &ldquo;full value classes&rdquo; is experimental and should be enabled explicitly. This can be done by supplying the compiler argument &lsquo;-Xfull-value-classes&rsquo;</p>
</blockquote>
<p>The other flag is <code>-Xvalhalla-value-classes</code>, which compiles Kotlin value classes into JVM value classes. The announcement states it needs JDK 28 or later, for both compilation and execution. This machine runs JDK 21, so that path is untested in this article.</p>
<h2 id="the-bytecode-is-genuinely-smaller">The bytecode is genuinely smaller</h2>
<p><code>javap -p</code> on both classes: identical fields, two doubles each. The difference is the methods.</p>
<p><img alt="11 methods vs 6" loading="lazy" src="/images/k25-value/fig2.png"></p>
<p>The data class carries <code>component1</code>, <code>component2</code>, <code>copy</code>, <code>copy$default</code>. The value class has none of them. What disappears is not just four methods but the bytecode behind them: 2188 B against 1729 B, a 459 B gap.</p>
<p>That is by design. An instance is fully determined by its primary properties, so the compiler generates no <code>copy</code> and no positional <code>componentN</code>. It generates <code>equals</code>, <code>hashCode</code>, <code>toString</code> and nothing else of that family.</p>
<p>What pays for the saving is a hard constraint: <strong>a value class cannot have a field outside its primary constructor</strong>. Want a <code>length</code>? It has to be a getter recomputed on every access, as in the official example <code>val length get() = Math.sqrt(x*x, y*y)</code>. No storage, no constructor parameter, and no role in equality.</p>
<p>So what got removed is less the features than the escape hatch that lets an object quietly carry extra state. A data class allows you to add a <code>var</code> beside the constructor, and the moment you do, an instance is no longer uniquely determined by its primary properties.</p>
<h2 id="at-call-sites-inline-and-full-are-two-different-things">At call sites, inline and full are two different things</h2>
<p>This is the part I think matters most. Both are called value classes, and they behave completely differently in a method signature.</p>
<p><img alt="inline is erased, full stays an object" loading="lazy" src="/images/k25-value/fig3.png"></p>
<p>The two inline functions lose the type entirely: <code>useInline-lc-6ta0(String)</code>, the parameter is a bare <code>String</code>. Even the nullable version <code>useInlineNullable-oNwqeZs(String)</code> is a <code>String</code>, with null meaning absence and no boxing.</p>
<p>The full value class gets <code>useFull(FullPoint)</code>, a real object reference. Inside it calls <code>checkNotNullParameter</code> first, then reads fields through virtual calls. Four instructions in the disassembly: <code>aload_0</code>, <code>invokevirtual getX</code>, <code>invokevirtual getY</code>, <code>dadd</code>.</p>
<p>The inline nullable version is three: <code>aload_0</code>, <code>ifnonnull</code>, <code>aconst_null</code>. No object anywhere in between.</p>
<p>Across the whole <code>MainKt</code>, <code>box-impl</code> appears exactly once, on the <code>listOf(id)</code> line. Boxing did not disappear; it was deferred to the moment a value must become an <code>Object</code>.</p>
<p>Which moments those are: entering any generic container, being used as <code>Any</code>, being stored in a field, being locked by <code>synchronized</code>, being reached by reflection. The first is the one you hit daily, and a single <code>List&lt;InlineUserId&gt;</code> puts every element back into an object.</p>
<p>So &ldquo;zero overhead value classes&rdquo; needs a qualifier: zero overhead on the stack and in parameter positions. Once a container is involved they cost the same as ordinary objects.</p>
<h2 id="the-benchmark-a-tie-leaning-slow">The benchmark: a tie, leaning slow</h2>
<p>Bytecode alone is not enough, so I wrote a crude measurement: 20 million points, the same <code>s += x + y</code>, only the memory layout changes, 7 rounds per group taking the median, three independent runs.</p>
<p><img alt="Three layouts, three timings" loading="lazy" src="/images/k25-value/fig4.png"></p>
<p>Median per run: 13.5 / 13.6 / 13.6 ms (data class), 14.3 / 14.2 / 14.2 ms (value class), 10.7 / 10.7 / 10.7 ms (flat <code>DoubleArray</code>).</p>
<p>The value class is 4.6% slower than the data class and 25% slower than the flat array. I will not spin that 4.6% into &ldquo;value classes are slower&rdquo;: on an array of objects it is noise. The number that means something is 21.6%, and that is the cost of boxing.</p>
<p>Which is to say: <strong>on JDK 21 a full value class buys you API constraints, not performance.</strong> The performance column waits for Valhalla, and Valhalla waits for JDK 28.</p>
<p>A caveat on the measurement itself: this is not JMH. No fork isolation, no control over GC. It measures the relative gap between three layouts on one machine, not absolute throughput. The order of magnitude is the claim, not the decimals.</p>
<h2 id="two-syntax-boundaries-i-hit">Two syntax boundaries I hit</h2>
<p>The first: <code>@JvmInline</code> with two properties is rejected outright.</p>
<blockquote>
<p>error: @JvmInline value class must have exactly one primary constructor parameter.</p>
</blockquote>
<p>The second is more interesting. The announcement says inline value classes become multiplatform under a unified <code>inline</code> modifier starting in 2.5.20. So what happens in 2.5.0 if you write <code>inline value class R(val x: Double, val y: Double)</code>?</p>
<p>I tried it: <strong>it compiles, and quietly becomes a full value class</strong>. The <code>R.class</code> it produced has fields, a constructor and getters, and no <code>box-impl</code> or <code>constructor-impl</code>, identical to what <code>value class</code> produces. Not a single warning.</p>
<p>Anyone who reads that as &ldquo;inline now supports multiple properties&rdquo; will be caught out when 2.5.20 changes the behaviour. On 2.5.0, keep <code>inline</code> and <code>value</code> separate and do not mix them.</p>
<h2 id="the-next-few-releases-are-already-queued">The next few releases are already queued</h2>
<p>The announcement lays out the roadmap plainly: 2.5.20 brings better diagnostics for identity, equality and capture, and moves inline value classes to the common <code>inline</code> modifier on all platforms. More ergonomic update syntax, effectively a better <code>copy</code>, is expected in 2.6.0.</p>
<p>Name-based destructuring is already the default in 2.5.0: value classes destructure by property name instead of position. I did not find a trap there, but if you write <code>val (a, b) = p</code> out of habit, note that reordering the properties no longer breaks silently while mismatched names do.</p>
<h2 id="is-it-worth-using-today">Is it worth using today</h2>
<p>If your goal is multi-field immutable values with less boilerplate, value classes work now. The price is an experimental flag, and Alpha means no compatibility promises.</p>
<p>If your goal is performance, two waits apply: on the JVM, JDK 28 plus Valhalla; across platforms, 2.5.20 for the unified <code>inline</code>.</p>
<p>If you only want an API that refuses a bare <code>Double</code>, an inline value class is already enough, and it is more thoroughly erased at call sites than a full one.</p>
<h2 id="one-line">One line</h2>
<p>What 2.5.0 delivered is not faster value classes. It is value classes that can hold more than one field. Performance was left to a future JVM.</p>
<p>The whole project is still on this machine: compiled output, javap dumps, and the raw numbers from all three runs. If you want to try it in your own code, remember the two flags affect both compilation and execution. Do not let CI and your laptop run different JDKs.</p>
<p>Is your JDK at 28 yet? If your environment already runs <code>-Xvalhalla-value-classes</code>, send me the numbers and this article&rsquo;s performance section can be finished.</p>
]]></content:encoded></item></channel></rss>