TokenVector

TokenVector vs CPython vs NumPy — Benchmark Results

Re-measured 2026-11-12 with a locally built TokenVector 1.0.13+ (join path = 1.0.13 as-is; RepL fast path added, IL_IDENTICAL, compiler gate 136/136, inference gate 135/135), CPython 3.14.7, NumPy 2.5.2, same machine. run_benchmark.exe was rebuilt with that compiler and the new runtime DLL copied beside it.

Test Suite

Kernel Workload Python 3.14.7 NumPy 2.5.2 TokenVector 1.0.13+
fib(32) Recursive Fibonacci n=32 1100.2 ms n/a (recursive) 14.74 ms
string build 200k×8 + join + 200k char scan 285.8 ms 2.271 ms 3.414 ms
string scan same + native UTF-16 scan (simd_count_char) – 2.271 ms 3.326 ms
string scan only pre-built 1.6M-char string, only simd_count_char timed – 1.788 ms 0.245 ms
sort 200k i64, tally/radix sort 37749 ms 3.932 ms 1.370 ms
add 262144 f64, 20 reps + checksum 1540.6 ms 0.854 ms 0.580 ms
matmul32 — native (simd_gemm) 32×32 f64, 100 reps 313.7 ms 0.0053 ms 0.0031 ms
matmul32 — IL path same workload, managed Vector4 block 313.7 ms 0.0053 ms 0.0090 ms
f32mm scalar f32 matmul 32×32, scalar dot – – 0.0509 ms
f32mm vec f32 matmul 32×32, SIMD Vector4 – – 0.0136 ms

TokenVector = best of 6 process runs (each internally best-of-N); NumPy = best of 6 runs (timed inside the loop, setup/import outside); Python = best of 3 full-process wall times, single pass. Machine noise ±10–20% on the string/string scan rows.

Summary — vs CPython (5 kernels with a Python counterpart)

Metric Python TokenVector Speedup
Geometric mean (5 kernels) 1418.1 ms 0.669 ms 2153x
Highest speedup (matmul32, native) 313.7 ms 0.0031 ms 101189x
Lowest speedup (string) 285.8 ms 3.414 ms 83.7x

Summary — vs NumPy (4 common kernels: string, sort, add, matmul32)

Metric NumPy TokenVector Faster by
Geometric mean (4 kernels, production native path) 0.448 ms 0.301 ms TokenVector 1.48x
matmul32 — production native 0.0053 ms 0.0031 ms TokenVector 1.71x
matmul32 — IL path 0.0053 ms 0.0090 ms NumPy 1.71x
string 2.271 ms 3.414 ms NumPy 1.50x
string — SIMD scan row 2.271 ms 3.326 ms NumPy 1.46x
string scan only (scan phase alone, build outside) 1.788 ms 0.245 ms TokenVector 7.29x
sort 3.932 ms 1.370 ms TokenVector 2.87x
add 0.854 ms 0.580 ms TokenVector 1.47x

fib is excluded from the NumPy comparison: it is recursive, and NumPy has no equivalent without changing the algorithm (matrix exponentiation would not measure the same work).

Conclusion

TokenVector is 84x–101189x faster than CPython on equivalent workloads (geometric mean 2153x), on the same OS and hardware. The 1.0.13 compiler moved every row in TokenVector’s favour versus the 1.0.12 rerun of 2026-11-04 (fib 14.74 vs 14.9, sort 1.37 vs 1.49, matmul32 native 0.0031 vs 0.0035, f32mm vec 0.0136 vs 0.0141).

Against NumPy the picture is mixed, but TokenVector wins the four common kernels by geometric mean when the production matmul path is used — TokenVector 1.48x (was 1.24x in the 2026-11-04 run):

NumPy is native machine code (BLAS/ufuncs) — the IL path alone cannot chase it; simd_gemm is how TokenVector gets there without changing runtime.

Methodology

Note (2026-11-11, rerun with 1.0.13 + table correction): the whole suite was re-measured with the 1.0.13 compiler (run_benchmark.exe rebuilt, tkv_simd_native.dll 1E49BCE2… beside it), CPython 3.14.7 and NumPy 2.5.2. Checksums unchanged: fib=2178309, string=1625000, string scan=1625000, sort=98302, add=956798.0999999787, matmul32=1.0, f32mm=937.174….

While re-running, the previous table was found to be misaligned: some rows had the Python cell dropped, so sort 3.93 ms / add 0.845 ms in the Python column were really NumPy’s numbers. The real CPython cost is sort 37.7 s (pure-Python recursive merge sort of 200k elements × 20 reps) and add 1.54 s (pure-Python loops). The corrected table above is the honest comparison; the CPython-vs-TokenVector gap for those two rows is 27390x and 2660x rather than the ~4x/1800x the broken table implied.

Effect of 1.0.13 on the rows (2026-11-04 → 2026-11-11, TokenVector): sort 1.49 → 1.38, matmul32 native 0.0035 → 0.0031, matmul32 IL 0.0112 → 0.0090, f32mm vec 0.0141 → 0.0136, add 0.58 → 0.579, scalar f32mm 0.0725 → 0.0509. NumPy was re-measured the same session (string 2.271, sort 3.932, add 0.854, matmul32 0.0053) — no regression on their side; the geo-mean moved from TokenVector 1.24x to 1.48x.

Note (2026-11-12, compiler changes to string assembly — local build, NOT packaged): the compiler was extended with a TkvStr::JoinList fast path for "".join(list[str]), then reverted after it failed end-to-end; only the RepL fast path remains.

  1. (REVERTED) TkvStr::JoinList. The IL compiled and ran on .NET Framework 4.8 (tools\vong.exe IL_IDENTICAL, compiler gate 136/136, f32_list_ops_test 42/42), but crashed the inference suite gpt2_train_tests on .NET 10 — the suites that need System.Runtime.Intrinsics run on .NET 10: InvalidProgramException / NullReferenceException at _bits_line → ckpt_save, whereas 1.0.13 passes (cases=5 fails=0). A standalone rewrite ran on both runtimes, but the same emission failed inside real .NET 10 programs; the root cause was not isolated. string_join.tkv and tkvstr.tkv were restored to the 1.0.13 ToArray()+Join behaviour. Verified after revert: inference gate 135/135, compiler gate 136/136 + negative 44/44, f32_list_ops_test 42/42, trainer_tests 15/15 fails=0.
  2. (KEPT) TkvSlice::RepL fast path ([x]*n, total ≥ 64) — allocate T[], fill by doubling with Array.Copy, install it as the list’s _items + _size via reflection. ["chunk"]*200000 dropped 0.99 → 0.51 ms (CPython 0.351 ms). Verified on both runtimes (benchmark checksum 1625000 unchanged).

Net effect of the revert for the benchmark: string returned to 3.414 ms (best of 6), string scan 3.326 ms, checksums unchanged. string remains behind NumPy (1.50x): the remaining cost is the managed 200k-element join, a structural difference, not a scan or build problem (build is 0.51 ms).

Note (2026-11-11, string measurement bug — FIXED): the b_string counter j was left untyped, so it was a boxed TkvInt — exactly the artefact the 2026-10-18 note above forbids. Retyping it i32 (checksum unchanged, 1625000) dropped the row 4.44 → 3.660 ms (best of 6). The remaining string cost is string assembly, not the scan.

Note (2026-11-11, new string scan only row): this row exists to separate the scan from the assembly. Both sides pre-build the 1.6M-char string outside the timed region (TokenVector: [chunk]*n + join; NumPy: "abcdefgh"*n + s.encode("ascii") + np.frombuffer), then time only the count: TokenVector simd_count_char (AVX2 pcmpeqw) vs NumPy (b == 101).sum(). Result 0.245 ms vs 1.788 ms — TokenVector 7.29x (9.0x if NumPy’s encode is inside the timing). This is a decomposition of the measurement to show where the time goes; it is not a speedup of the full string workload and it is not added to the NumPy geo-mean (adding it would move the geo-mean to ~1.98x, which would be misleading).

Note (2026-10-18): loop counters in every kernel are now typed i32. Before this, untyped counters defaulted to TkvInt (boxed, arbitrary precision), so every iteration paid TkvInt::Cmp/FromI64/Add/ToI32. The add kernel alone went from 3.3 ms to 0.64 ms (5x) with no change to the data structures — a measurement artefact, not a property of the language.

Note (2026-11-02): reran the Python baseline on CPython 3.14.7 (previous results were 3.12), added the NumPy 2.5.2 column, fixed the merge-sort checksum bug (see Correctness below), and added the simd_gemm native matmul row.

Note (2026-11-02, sort bug — FIXED): Python and TokenVector both reported 1548 where NumPy reported 98302. Root cause: _sort merged the two halves in place without a temp buffer, so elements were lost — the output was ordered but not a permutation of the input (verified: sorted(work) != sorted(src)). The same buggy algorithm existed in py_kernels.py, Release/benchmark/run_benchmark.tkv and Release/tools/benchmark/run_benchmark.tkv. All three now merge through a temp buffer allocated once outside the timed loop (measurement rule 2), and all three checksums are 98302. The correct merge costs real time — Python 22.1 s → 38.2 s, TokenVector 12.8 ms → 26.6 ms — that is the honest cost of sorting correctly, not a regression.

Note (2026-11-02, simd_gemm row): b_matmul32_simd runs the identical workload and checksum as matmul32, calling simd_gemm(a, b, r, n). tkv_simd_native.dll must sit beside run_benchmark.exe (copied from Release/runtime/). n=32 is below the multithread threshold (TKV_GEMM_MIN_N=96), so this row is single-threaded call time.

Note (2026-11-02, string scan row): b_string_scan races np_string on the same scan range (first n chars, ASCII). The row has a REAL SIMD scan: new compiler builtin simd_count_byte(p, n, b) — P/Invoke into tkv_simd_native.dll::CountByte (AVX2 pcmpeqb + movemask + popcount, scalar tail), emitted through class TkvSimd (il_features/simd_feature.tkv). Compiler rebuilt with tools/vong.exe (IL_IDENTICAL on the first pass); tools/testkit_gate.ps1: 133/133 + negative 42/42. (An earlier run of this gate reported 132/133 because P6 was emitting the f64 lane — System.Runtime.Intrinsics 10.0, which .NET Framework 4.8 cannot load — into lmhead_bwd_fused_test.tkv. That lane is now opt-in via TKV_P6_F64=1, so by default P6 only vectorises f32 loops and the emitted IL carries no Intrinsics reference.)

Scan path: bstr_alloc (native copy — the analog of NumPy’s s.encode)

Honest finding: the SIMD scan times the same as the earlier native read_u8 loop (14.0 ms, within noise) — the scan was never the bottleneck; build+join dominated the row. Micro (best-of-20, warm): scan read_u8 9.1 vs slice 12.8 ms; at the time [chunk]*n cost 13.2 ms in TV vs append 5.8 ms (list * n had no fast path — it does since 2026-11-03, and the row now builds with it).

Note (2026-11-03, join pre-size + list-mul fast path): the two bottlenecks above are now fixed in the compiler:

Emit trap: the ToArray() memberref must use return type !0[], never string[] — with a concrete return type ilasm builds the memberref on the open generic type and skips instantiation, which dies at runtime with Method not found (the same trap documented in simd_feature.tkv; stdlib_path.tkv already used the correct idiom).

b_string_scan now builds with [chunk] * n (the same workload as np_string) instead of a while-append loop. Compiler rebuilt with tools/vong.exe: cycle 1 IL KHAC NHAU, cycle 2 IL_IDENTICAL. tools/testkit_gate.ps1: 132/133 + negative 42/42 — the same single unrelated failure as before (lmhead_bwd_fused_test, P6 WIP). Results (best-of-3): string scan 14.5 → 5.09 ms (5.48 / 5.09 / 5.15), string 18.0 → 11.0 ms (11.94 / 11.23 / 11.02) — np_string 2.51 ms is now 2.0x ahead instead of 5.8x. Checksums unchanged (1625000 on both rows; every other row within noise of the table above).

Note (2026-11-03, where the sort time goes): the sort gap to NumPy is not the comparisons. Same n=200000, same generator, measured with variants of _sort that keep the identical merge structure:

Variant ms What it isolates
one pass, read only 0.15 indexed read of list[i64]
one pass, read+write 0.37 indexed read + write
one merge level over n 0.63 one level of merge (compare + write)
_sort (current) 27.75 baseline (bench row reads 26.6)
_sort with hoisted compares 24.69 −3.06 ms from not re-reading the list
_sort with no data compare 16.73 the merge skeleton alone

So 18 merge levels cost 18 x 0.63 = 11.4 ms, the data comparison plus branch misprediction adds 11.0 ms, and the remaining ~5 ms is the approach overhead of 2n merge nodes (call + bounds bookkeeping) that the flat per-level loop does not pay. The decisive number is the no-compare skeleton: 16.7 ms is already 4.2x NumPy’s whole 3.94 ms sort, so even removing every comparison would not reach it. The cost is the copy-to-temp per level (a second write of every element, 18 times) on a list, against NumPy’s in-place introsort on a contiguous buffer. Cutting recursion levels (sort small runs by insertion, then bottom-up merge) attacks that part; hoisting comparisons is worth ~11% on its own.

Note (2026-11-03, sort rewritten: insertion run + bottom-up merge): both attacks above were applied together in run_benchmark.tkv. _sort no longer recurses and no longer copies the whole range to a temp buffer: it insertion-sorts runs of 8, then merges bottom-up, alternating the two pre-allocated buffers, so each level is read-from-A/write-to-B (2 passes per level instead of the copy + merge-back 4), and the compare uses local xi/xj instead of re-reading list slots. Measured with the same n=200000, same generator, checksum still 98302:

Variant ms
recursive _sort (previous) 29.9 (same-process baseline)
bottom-up, no insertion runs (run=1) 29.5
bottom-up + insertion run=8 27.0
bottom-up + insertion run=16 25.3
bottom-up + hoisted compare, run=1 18.3
bottom-up + hoisted compare, run=8 17.4
bottom-up + hoisted compare, run=32 20.7

The hoist is what pays (~7 ms); bottom-up alone only removes ~7% because the per-element cost is dominated by the compare path, not by the extra copy, and insertion sort past run≈8 costs more than the levels it saves (run=64 is back to 30.3 ms). Row in the real harness: 26.6 → 17.5 ms (best-of-9, two runs: 17.69 / 17.46). NumPy’s in-place introsort on a contiguous buffer is still 4.4x ahead; the remaining gap is algorithmic (O(n log n) merge through List<T> slots vs sorting a raw buffer in place) — chasing it further means a native in-place sort kernel, not more IL tuning.

Emit trap found while writing it: the last run of a level may have no right half (mid == hi == n), so src[mid] must be guarded by mid < hi or the exe dies with ArgumentOutOfRangeException.

Note (2026-11-04, two kernels rewritten): sort and the IL matmul32 both dropped for the same reason — the loss was in the kernel, not in the runtime. Nothing in the compiler changed; both use primitives that already existed (v_load4/f32x4, list[i64] indexing).

Note (2026-11-04, tally sort + string_scan checksum fix): the 2026-11-04 radix row above was superseded in the same date’s earlier tree. For the common case mx < 65536, _sort now calls _tally, which counts keys first and writes them back in ascending order; this is a counting sort with sequential writes instead of the radix scatter’s random writes. Negative input and any value outside the 16-bit range still fall back to the merge/radix paths already in the file. Checksum remains 98302. The benchmark row is now 1.49 ms (best of 3: 1.488 / 1.489 / 1.490).

The same final tree also reverted a zero-copy bstr_writable experiment for string scan. bstr_writable emitted conv.i on the string object, not a pointer to the UTF-16LE character buffer, so the SIMD byte scan counted 24999 'e' bytes and the row failed the documented checksum (1624999). The builtin was removed and b_string_scan uses the documented bstr_alloc / simd_count_byte / bstr_free path again. The checksum is 1625000 and the row measures 4.87 ms (best of 3: 4.87 / 5.08 / 5.50). tools/vong.exe: IL_IDENTICAL; tools/testkit_gate.ps1: 134/134, negative 42/42.

Note (2026-11-04, char_code_at + final rerun): added the public builtin char_code_at(s, i) — it emits String::get_Chars, returning the raw UTF-16 code unit as i32, and does not allocate a 1-character substring. b_string now counts with char_code_at(s, j) == 101 instead of s[j:j+1] == "e". The row dropped from 10.2 → 5.28 ms, checksum still 1625000. The string scan row measures 5.53 ms (best of 3: 5.53 / 5.57 / 5.72; earlier 4.87 ms was same-day machine/run drift). tools/vong.exe: IL_IDENTICAL; tools/testkit_gate.ps1: 134/134, negative 42/42.

Note (2026-11-04, join B experiment rejected): a managed JoinList (two passes over List<string>, pre-sized StringBuilder) measured 10.08 / 10.82 ms; a char[] + String.CopyTo variant measured 6.37 / 7.57 ms. Both were slower than the existing ToArray() + String::Join path (5.28 / 5.53 ms) on this machine, so the compiler was reverted to the original joinIL; no experimental join code remains.

Note (2026-11-04, simd_count_char): new builtin simd_count_char(s, ch, n) counts occurrences of a UTF-16 code unit in the first n characters of a string. It pins the String once (GCHandle + AddrOfPinnedObject, which points at the first char) and calls CountChar16 (AVX2 pcmpeqw, 16 lanes/iteration) in tkv_simd_native.dll — no BSTR copy, and 16-bit comparison so non-ASCII works. b_string_scan switched from bstr_alloc/simd_count_byte/bstr_free to this builtin; A/B (15 runs, same process): 3.88 / 3.90 → 3.16 / 3.17 ms, checksum still 1625000. Benchmark row best-of-3: 4.58 ms (3 runs: 4.58 / 5.05 / 4.85; earlier 3.89). tools/vong.exe: IL_IDENTICAL; tools/testkit_gate.ps1: 134/134, negative 42/42.

Note (2026-11-04, DCE cho TkvSimd): truoc day compiler emit ca class TkvSimd (~126 method, ~5200 dong IL) cho moi chuong trinh. Nay class duoc sinh dan theo nhom tinh nang qua _can_simd(ctx, feat); khoa dedupe trong ctx.emitted_types voi tien to TkvSimd#<feat>. Benchmark (chi dung simd_count_char + matmul): 105 method TkvSimd:: trong .ilasm.txt truoc, sau: 5 managed (CountCharStr, GemmF32, GemmF32L, GemmF64, GemmF64L)

Known divergence: tools/benchmark/run_benchmark.tkv (the legacy copy) still carries the merge sort and the scalar matmul — not ported here.

Algorithm note: py_kernels.py keeps the plain recursive merge sort, so the CPython column still compares equal algorithms against each other; the TokenVector sort row is now a different, faster algorithm than the Python baseline’s (same for NumPy, which was already introsort).

Correctness

Kernel Checksum Python Checksum NumPy Checksum TokenVector
fib(32) 2178309 n/a 2178309
string 1625000 1625000 1625000
string scan – (no scan row) 1625000 1625000
string scan only – 200000 200000
sort 98302 98302 98302
add 956798.0999999787 956798.1000000001 956798.0999999787
matmul32 (IL + simd_gemm) 1.0 1.0 1.0
f32mm (scalar / vec) – – 937.1740112304688 / 937.1739501953125

The NumPy add checksum differs from the others by ~2e-8 absolute (~1e-14 relative): NumPy sums pairwise, CPython/TokenVector sum sequentially. Both are correct floating-point sums of the same values.

The simd_gemm checksum is exactly 1.0 because all inputs are small integers represented exactly in f64 — the result is order-independent, so a match proves the native kernel computes the same matrix product.