$ show scala-native/scala-native
Scala Native is an optimizing ahead-of-time compiler and lightweight managed runtime for Scala, enabling compilation of Scala code to native executables. The repository implements the full toolchain: a Scala compiler plugin (nscplugin) that emits an intermediate representation (NIR), a linker and optimizer (tools) that perform reachability analysis, inlining, and code generation targeting LLVM, and a native runtime (nativelib) with multiple garbage collectors (immix, commix, Boehm), memory management (zones, unsafe pointers), and platform abstractions. It also provides extensive reimplementations of the Java standard library (javalib), POSIX and Windows bindings (posixlib, windowslib), and a JUnit test framework adapted for native execution. The project is highly modular, with over 3500 files spanning compiler internals, runtime C code, and library ports. It is actively maintained with frequent commits and a large test suite covering JDK compatibility, concurrency, and platform-specific behavior. Notable technical depth includes a custom NIR serialization format, graph-coloring for trait dispatch, polymorphic inline caching, and concurrent garbage collection algorithms.
The LLM identified 6 files worth highlighting for reuse:
Provides a generic Scope trait that manages a stack of resources (using an AtomicReference) and ensures deterministic cleanup via close(). This pattern is reusable for any scoped resource management in concurrent or single-threaded contexts, independent of Scala Native internals.
Constructs a control flow graph from NIR instructions, identifying blocks, edges, and dead blocks. The algorithm is generic and can be adapted to any linear IR for dead code elimination or optimization passes.
Implements a striped concurrent accumulator (base for LongAdder/DoubleAdder) using a table of cells to reduce contention. This is a well-known pattern from JSR-166, implemented cleanly with volatile fields and CAS operations, reusable in any JVM-like environment.
Port of the Ryu algorithm for fast and accurate double-to-string conversion. This is a self-contained, pure algorithm with no Scala Native dependencies, directly reusable in any Scala or Java project for high-performance floating-point formatting.
Implements polymorphic inline caching for the interflow optimizer: caches getClass results per receiver and generates type-switches for sealed/final classes. The pattern is general for any language runtime that wants to inline virtual calls based on receiver type.
Provides implicit enrichment of Java collections (Iterable, Iterator, Enumeration) with Scala-like functional operations (foreach, exists, foldLeft, mkString). This is a standalone utility that can be extracted to bridge Java and Scala collections without Scala 2.13+ dependencies.