Research question
When SQLite runs as LLVM bitcode under GraalVM Sulong, a serialized connection crosses the Truffle native function interface (NFI) to call glibc for each mutex operation. The mutex may be uncontended while preparing the call still allocates argument buffers. We ask how much a guarded fixed-signature downcall changes the CPU time of a repeated point-lookup workload, and whether its behavior matches the original native call in targeted tests.
The result is relevant to tools that select and invoke database-backed runtimes. A cost model for such a tool can account for process startup and compilation separately from repeated requests to a warmed process. In this study Panama had a lower median warmed wall time than libffi and a higher whole-process median. These measurements characterize a tool runtime component. Agent policies and task outcomes are outside the experiment.
Guarded downcall and behavioral contract
The JVM overlay specializes calls after Sulong resolves the native symbol and converts the pointer. It requires a nonvariadic NFI signature of (POINTER):SINT32, an already converted numeric pointer, and an address matching pthread_mutex_lock or pthread_mutex_unlock in the loaded glibc. Failed guards take the existing libffi path. Connection serialization, the underlying mutex operations, the JNI thread transition, and the return type are preserved.
For the admitted Linux x86-64 ABI, the normal serializer would place the same pointer bits $p$ in the argument slot. The bridge transfers the guest thread's error state to native errno, calls the resolved function $g$ on $p$, and captures the new errno $e'$ and signed 32-bit return $r$. It returns a 64-bit word
$$w = 2^{32}\operatorname{u32}(e') + \operatorname{u32}(r).$$
The Java adapter decodes the two halves and restores guest errno. This argument establishes equality of the pointer bits, function target, input errno, returned bits, and resulting errno at the guarded ABI boundary. The tests exercise error-checking mutexes, wrong-owner and duplicate operations, errno sentinels, native-to-guest callbacks, and an alternate provider with the same signature. Each instrumented interpreter and forced Tier 2 run observed 4,000 direct libc bridge calls and zero for the alternate provider. The captured sqlite3_mutex_enter graph shows 3 to 0 argument-preparation array allocations. Its installed code shrank from 1,229 to 722 bytes.
For the comparison with Panama, the pinned Sulong build uses an explicit backend prefix for the same two guarded direct symbols and delegates to the published GraalVM Panama provider. All three JVM profiles load the same overlay JARs and use the same coalesced SQLite LLVM module. Each profile selects its own path through runtime settings.
Experimental design
All measurements used one Linux x86-64 host with an AMD EPYC 9V74 CPU and glibc 2.41. The main versions were SQLite 3.54.1, GraalVM Community JDK 24.0.2+11.1, Sulong 24.2.2, and Clang 18.1.3. The native controls use Clang -O2 on stock SQLite C and on the exact LLVM module run by the JVM. Every lookup profile uses the same serialized connection mode, inputs and deterministic checksums.
Six blocks each ran the five profiles in a seeded pseudorandom order, using one fresh process for each profile per block. The workload contains 2,048 rows, 12,000 warmup lookups, and ten windows of 150,000 lookups. The last five windows contribute one median per process. The verifier checks that the lookup driver and region had Tier 2 code at the start of each retained JVM window and that the trace contains no logged guest compiler events in that window. It checks lookup values and window checksums against native. The complete run contains 30 processes and 150 retained windows. Warm thread CPU, warm wall time, and whole-process wall time are distinct measurements.
Results
| Profile | Warm thread CPU per 150,000 lookups | Warm wall per 150,000 lookups | Whole-process wall | | --- | ---: | ---: | ---: | | Stock SQLite C, Clang -O2 | 46.69 ms | 46.88 ms | 0.489 s | | Matched guest LLVM, Clang -O2 | 59.34 ms | 59.34 ms | 0.629 s | | Graal JVM libffi | 183.71 ms | 186.55 ms | 50.875 s | | Graal JVM Panama | 111.27 ms | 113.35 ms | 78.659 s | | Graal JVM typed JNI | 68.74 ms | 69.47 ms | 50.686 s |
Typed JNI used less CPU than both JVM alternatives in every warmed block. The median of the paired JNI/libffi CPU ratios was 0.365, a 63.5% reduction. Its exact 96.875% sign interval was [0.352, 0.407]. The JNI/Panama ratio was 0.618 [0.469, 0.648]. JNI/stock C was 1.469 [1.368, 1.563], and JNI/matched LLVM was 1.162 [0.984, 1.229]. The intervals are conditional on independent process blocks and a continuous median on this host. JNI preceded libffi in five of the six blocks. The raw data retains the first block's high cold samples.
In the separate six-block mutex loop, medians per lock/unlock pair were 7.73 ns for native C, 147.37 ns for libffi, 35.92 ns for Panama, and 18.23 ns for typed JNI. In an additional six-block check, the pristine JVM and the JVM with both switches disabled used 140.59 ns and 142.33 ns per pair, respectively. The median paired ratio was 1.006 [0.817, 1.022]. A speedtest1 panel on main, cte, and orm repeated each native and JVM profile three times. All 45 processes matched native result counts, hashes, and case order. Whole-process performance improved on some testsets and regressed on others.
Compatibility and limits
The traced stock-timeout four-worker threadtest5 mismatch accounted for all 27 missing rows. Six writes returned SQLITE_BUSY and the worker did not execute 21 expected inserts from an already-claimed task after another busy response. Raising the test's busy timeout to 30 s produced matching 1,230-row tables in the AOT interpreter, AOT normal-compilation, and JVM interpreter profiles. The instrumented JVM JIT runs reached the outer limit. A separate forced-lock probe reproduced the committed-claim, failed-insert and successful-retry sequence in native and guest runs. This intervention changes the test harness's waiting policy.
The production JNI bridge passed an isolated Native Image contract for glibc mutexes, EDEADLK, errno transfer, and signed result packing. A bounded full stock SQLite Native Image build timed out during static analysis. Full AOT SQLite performance, complete public-suite equivalence, and replication on a second physical host remain open. This case study supports the tested warm serialized workload. Its largest warm improvement did not materially change whole-process median time compared with libffi.
Artifact and disclosure
The attached reproduction files contain fixed inputs, the overlay source, process reports, timing and verification logs, build commands, and summary scripts. Start with REPRODUCE.md. The compact attachments also document the pinned dependencies and which Native Image outcomes can be replayed from logs or rebuilt. A coding agent assisted with implementation, benchmark execution, diagnosis, analysis, and drafting.
References
- GraalVM LLVM Runtime
- Truffle Native Function Interface
- SQLite threading modes
- Java SE 24 Foreign Function and Memory API
- Kalibera and Jones. Rigorous Benchmarking in Reasonable Time.
- Bolz, Kurilova, and Tratt. Making an Embedded DBMS JIT-friendly.
- Kowalski. Compiling SQLite Queries to Native Code with LLVM.