Ghost Serialization Engine — KSP Lab

👻 Ghost Compiler Lab

A guided tour through the 4 optimization pillars that make Ghost 3× faster and 10× more RAM efficient than Moshi, Gson, and kotlinx.serialization.

⚙️Pillar 1 — Perfect Hash O(1): The Zero-Lookup Dispatcher
Why does this matter?
Traditional libraries like Gson use a HashMap<String, Field> to find which field matches each JSON key — this requires computing the hash of every String, searching the table, and comparing with equals(). Ghost eliminates all of that: the KSP Compiler pre-calculates a collision-free perfect hash at compile time. At runtime, finding a field is a direct mathematical O(1) operation on raw bytes.
Your Kotlin Data Class
@GhostSerialization
Terminal — KSP Outputready
> Press "Run KSP Compiler" to start...
Step 1 of 3 — Byte Packing (4 bytes → Int32)

Current field:

Int32 (key)0
Byte 3 (bits 24-31)Byte 2 (bits 16-23)Byte 1 (bits 8-15)Byte 0 (bits 0-7)
0
0
0
0
Real formula (PerfectHashFinder.kt):
hash = ((key × 31 + len) >> 0) & 1023
Byte Packing Walkthrough:
1. Nombre del campo: "" (longitud = 0)
2. Conversión ASCII a Int32:
3. Valor final empaquetado (Int32): 0
ℹ️ En Kotlin, el compilador KSP asocia este número único al campo:
val H_ = 0