A high-performance PHP C++ extension for ASUN (Array-Schema Unified Notation) — SIMD-accelerated (SSE2/AVX2/NEON), zero-copy parsing, direct Zend API with no intermediate layers.
ASUN separates schema from data, eliminating repetitive keys found in JSON:
JSON (100 tokens):
{"users":[{"id":1,"name":"Alice","active":true},{"id":2,"name":"Bob","active":false}]}
ASUN (~35 tokens, 65% saving):
[{id@int, name@str, active@bool}]:(1,Alice,true),(2,Bob,false)
json
Standard JSON repeats every field name in every record. When you send structured data to an LLM, over an API, or across services, that repetition wastes tokens, bytes, and attention:
[
{ "id": 1, "name": "Alice", "active": true },
{ "id": 2, "name": "Bob", "active": false },
{ "id": 3, "name": "Carol", "active": true }
]asun
ASUN declares the schema once and streams data as compact tuples:
[{id, name, active}]:
(1,Alice,true),
(2,Bob,false),
(3,Carol,true)
Fewer tokens. Smaller payloads. Clearer structure, and faster parsing than repeated-object JSON.
@is the field binding marker, for example{id@int,name@str}.- Scalar hints such as
@intand@strare optional; structural bindings such as@{...}and@[...]are required for complex fields. - Header/body separation stays
:, for example{id@int,name@str}:(1,Alice). - Dedicated map syntax has been removed. Key-value collections should be modeled as entry-object lists such as
attrs@[{key@str,value@int}].
| Scenario | json_encode | asun_encode | Speedup |
|---|---|---|---|
| Flat struct × 100 | 3.66 ms | 0.88 ms | 4.16x |
| Flat struct × 500 | 15.02 ms | 4.03 ms | 3.73x |
| Flat struct × 1000 | 31.89 ms | 8.66 ms | 3.68x |
| Flat struct × 5000 | 152.69 ms | 56.63 ms | 2.70x |
| Scenario | json_decode | asun_decode | Speedup |
|---|---|---|---|
| Flat struct × 100 | 7.13 ms | 2.93 ms | 2.43x |
| Flat struct × 500 | 35.35 ms | 16.03 ms | 2.21x |
| Flat struct × 1000 | 69.06 ms | 29.83 ms | 2.32x |
| Flat struct × 5000 | 347.81 ms | 154.45 ms | 2.25x |
| Scenario | json_decode | asun_decode | Speedup |
|---|---|---|---|
| 10 Companies | 30.38 ms | 0.42 ms | 72.34x |
| 50 Companies | 148.92 ms | 1.92 ms | 77.51x |
| 100 Companies | 325.97 ms | 3.88 ms | 83.91x |
| Scenario | JSON | ASUN Text | ASUN Bin | Saving (Text/Bin) |
|---|---|---|---|---|
| Flat struct × 100 | 12,071 B | 5,614 B | 7,446 B | 53% / 38% |
| Flat struct × 5000 | 612,808 B | 287,851 B | 372,250 B | 53% / 39% |
| 100 Companies | 431,612 B | 231,011 B | 251,830 B | 46% / 42% |
- Pure C++ Extension — direct Zend API, no intermediate layers, no FFI overhead
- SIMD-accelerated — SSE2/AVX2 (x86_64) and NEON (ARM64) with scalar fallback
- Zero-copy parsing — string scanning without heap allocation where possible
- PHP 8.4 compatible — built for the latest PHP with proper arginfo
- Binary format — compact binary serialization for maximum throughput
- Pretty formatting — smart indentation with configurable width
- IDE support — PHP stubs included for autocomplete and type checking
cd asun-php
phpize
./configure --enable-asun
make -j$(nproc)
# Optional: install system-wide
sudo make install; php.ini
extension=asun.soOr use per-command:
php -d extension=path/to/modules/asun.so your_script.php| Function | Description |
|---|---|
asun_encode($data) |
Encode to ASUN format |
asun_decode($string) |
Decode ASUN string to PHP value |
asun_encodeBinary($data) |
Encode to ASUN binary format |
asun_decodeBinary($str, $schema) |
Decode binary with type schema |
asun_encodeTyped($data) |
Encode with scalar type hints in schema |
asun_encodePretty($data) |
Encode with pretty formatting |
asun_encodePrettyTyped($data) |
Encode with pretty formatting + scalar type hints |
<?php
// Serialize a struct (associative array)
$user = ['id' => 1, 'name' => 'Alice', 'active' => true];
$asun = asun_encode($user);
// → "{id,name,active}:(1,Alice,true)"
// With scalar type hints
$typed = asun_encodeTyped($user);
// → "{id@int,name@str,active@bool}:(1,Alice,true)"
// Deserialize
$decoded = asun_decode($asun);
// → ['id' => 1, 'name' => 'Alice', 'active' => true]
// Vec of structs — schema written once
$users = [
['id' => 1, 'name' => 'Alice', 'active' => true],
['id' => 2, 'name' => 'Bob', 'active' => false],
];
$vec = asun_encode($users);
// → "[{id,name,active}]:(1,Alice,true),(2,Bob,false)"<?php
$user = ['id' => 42, 'name' => 'Alice', 'active' => true];
// Encode to binary (compact, fast)
$bin = asun_encodeBinary($user);
echo strlen($bin); // 18 bytes vs 36 bytes text
// Decode with type schema
$decoded = asun_decodeBinary($bin, [
'id' => 'int',
'name' => 'str',
'active' => 'bool',
]);<?php
$users = [
['id' => 1, 'name' => 'Alice', 'active' => true],
['id' => 2, 'name' => 'Bob', 'active' => false],
];
echo asun_encodePrettyTyped($users);
// [{id@int, name@str, active@bool}]:
// (1, Alice, true),
// (2, Bob, false)Once the extension is built, you can run the included examples and benchmarks directly from the command line:
# Run basic examples
php -d extension=modules/asun.so examples/basic.php
# Run complex nested structure examples
php -d extension=modules/asun.so examples/complex.php
# Run performance benchmarks (JSON / ASUN / BIN)
php -d extension=modules/asun.so examples/bench.php- Zero key repetition — Schema is written once; data rows carry only values
- Direct Zend API — No intermediate data structures; directly constructs PHP
zvalandHashTable - SIMD string scanning — SSE2/AVX2/NEON vectorized character detection (16 bytes/cycle)
- Fast number formatting — Two-digit lookup table, fast-path float formatting
- Schema-driven parsing — Positional field mapping with O(1) access, no per-row key hashing
- Minimal allocation — Stack-based schema parsing (max 64 fields, zero heap)
Basic test coverage:
- ✅ Struct encode/decode roundtrip
- ✅ Typed schema encode
- ✅ Vec encode/decode roundtrip
- ✅ Pretty format (single struct, vec)
- ✅ Binary encode/decode roundtrip
- ✅ Nested structs
- ✅ Array fields
- ✅ Optional/null fields
- ✅ Special characters (quotes, escapes, newlines, tabs, backslashes)
- ✅ Comments parsing
- ✅ Multiline format
- ✅ Cross-compatibility with C++ format
- ✅ Large dataset (1000+ records)
Copy stubs/asun.php to your project for IDE autocomplete:
cp stubs/asun.php /path/to/your/project/.stubs/Then configure your IDE to include the stubs directory for autocompletion.
MIT