{
  "id": 3480227,
  "title": "Value Classes Still Need Compiler Sympathy",
  "url": "https://urgent.news/2026/08/26/value-classes-still-need-compiler-sympathy",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-08-26T08:59:15.000Z",
  "source": {
    "name": "Hacker News",
    "slug": "hacker-news",
    "url": "https://johan-sjolen.github.io/post/compiler-sympathy/compiler-sympathy/"
  },
  "original_language": "en",
  "account": "The article discusses the preview feature in JDK 28 that integrates Valhalla, a major milestone for Java. Value classes, which increase the ability to communicate program semantics to others and optimization opportunities for the JVM, are highlighted. However, some perceive a belief that ordinary classes provide a performance floor, whereas value classes raise performance but never lower it. This is incorrect, as a well-intentioned program may lead the JVM into a situation where flattened and reference representations are faster in different methods, forcing conversion between them. The article aims to provide insight into the JVM's capabilities and limitations with value classes, encouraging reasoned code writing.\n\nThe main optimization advantage of value classes is the relinquishment of identity, giving the JVM flexibility in choosing a suitable representation. This enables flattening values (preventing pointer chasing), scalarization by representing components in registers or on the stack, and trivial escape analysis due to no identity to escape. Three examples are presented: a large final value stored flat, a direct value transformation compiled without allocation, and a generic virtual call requiring materialization.\n\nJEP 539, Strict Field Initialization in the JVM, allows the JVM to rely on a final field being initialized before its enclosing object becomes observable, without risking torn assignments. Mutable fields, however, must maintain tear-free assignment. If a mutable field contains a value too large for an atomic flattened update, the JVM must use a reference layout. Strict-initialization grants many optimization possibilities. FourLongs, with 32 bytes of payload, is too large for an atomic flattened update but can be flattened when used as a record component due to strict initialization.\n\nThe JVM's capability is demonstrated in a data race scenario where flattened fields require writing individual components, risking torn values. The Java Memory Model forbids such tearing, so the current JVM uses reference layout for immutable classes. In the case of a small function that changes a component in a loop, C2, an optimizing compiler, recognizes the scalarized representation and performs optimization. However, when the value class becomes an identity record, C2 cannot prove that its identity has no impact on the computation. Providing the compiler with stronger semantic guarantees may yield significant performance improvements. The article concludes by mentioning an unexpected slowdown in a parsing library after converting records to value classes, which has sparked an active investigation by C2 team researchers.",
  "summary": null,
  "key_points": [],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}