Giorgi Chapidze
All projects

Systems

Weft

A Java 25 transactional memory library with snapshot reads, automatic retries, and an optional agent for choosing write-lock timing.

JavaJVM instrumentationMVCC
Weft — concurrent threads woven together.

Choosing when to take the locks

Weft is a transactional memory library for Java 25. It buffers changes to shared transactional values and commits them together after validation, retrying conflicting attempts automatically.

The library explores when a transaction should check for conflicts optimistically or acquire write locks earlier. Read-heavy and write-heavy workloads can favor different answers. Weft combines both protocols behind TVar transactional variables and the Weft.atomic, read, and write API.

From bytecode to a strategy

The core defaults to optimistic execution and runs independently of the agent. An optional Java agent uses the JDK Class-File API to count transactional read and write call sites, resolve counts through nested lambdas, and inject a TransactionHints argument. These are static code counts; branches and loops can make the executed mix differ.

The selector chooses optimistic execution for at least 80% reads, early write locking for at least 60% writes, and optimistic execution otherwise. That choice is made before the retry loop. Conflicts retry the same selected protocol with adaptive backoff.

The agent implementation and transaction entry point show where analysis ends and execution begins.

Architecture

Optimistic transactions use version histories and buffered writes, then acquire write locks during commit. The pessimistic path attempts locks when writes occur. Both validate recorded reads before applying writes. Read-only transactions return after reading a consistent snapshot, without commit validation. Nested atomic calls share the outer transaction’s buffered changes and retry budget.

Logical clocks track identities and commits, ScopedValue carries execution state, and CircularVersionHistory stores version history behind a VersionHistory interface. The core, instrumentation agent, and JMH benchmarks are separate modules.

A Java agent supplies static read and write counts. Those counts select an optimistic or pessimistic strategy once. The transaction runs with that protocol and validates before commit. Conflicts during execution or commit trigger backoff and retry the same selected protocol.
Weft architecture

A small transaction

java
var counter = new TVar<>(0);
Weft.atomic(() -> {
    int current = Weft.read(counter);
    Weft.write(counter, current + 1);
});

The agent can supply metrics at load time; callers can also provide explicit metrics through the overloaded entry point. It resolves directly nested bodies and same-class method references, while leaving explicit hints unchanged. Arbitrary helper calls, references into other classes, Runnable variables, and conditional lambda expressions require explicit hints when automatic analysis cannot resolve them.

Usage boundaries and verification

Transaction bodies can run more than once, so I/O and irreversible effects belong outside them. Values should be immutable or replaced through Weft.write; mutating an object returned by a read bypasses transactional buffering. Nested transactions share the outer attempt and have no savepoints. An attempt belongs to one thread. Cross-thread reads, writes, and nested atomic calls throw IllegalStateException, including from structured subtasks that inherit scoped bindings. Application failures discard buffered writes, and try-with-resources releases owned locks on every exit path.

Retries are bounded: after the initial attempt and 512 retries, a persistent conflict raises MaxRetriesExceededException. Optimistic readers can consult retained history, while pessimistic readers retry when the latest value is newer than their snapshot. History starts at 32 entries and can be resized explicitly; it does not grow automatically.

With JDK 25, ./mvnw clean verify builds all three modules and runs core, agent, and benchmark workload tests, including attachment of the packaged agent in a separate JVM. No preview flags are required. The usage and build guide covers setup and the API's boundaries.

What the benchmarks compare

The JMH suite compares optimistic transactions, pessimistic write locking, and a lock-based implementation. It covers transfers, concurrent reader/writer groups, configurable access patterns, and instrumentation workloads. Untimed checks verify balances, increments, and agent activation.

Recorded throughput

The recorded run used Temurin 25.0.5 and JMH 1.37 with four workers, 64 accounts, two accounts per transaction, and uniform access. Each configuration ran three forks, with five 2-second warmup iterations and eight 2-second measurement iterations per fork. The agent and diagnostic counters were disabled.

Results are millions of operations per second; ± gives the JMH 99.9% confidence-interval half-width. Higher is better.

WorkloadOptimisticPessimisticLocked
Transfer8.823 ± 0.3397.476 ± 0.16944.523 ± 0.748
Reader/writer total21.220 ± 0.94419.526 ± 1.47760.344 ± 0.991
Reads18.521 ± 0.92116.761 ± 1.54947.100 ± 1.034
Writes2.699 ± 0.0902.765 ± 0.17813.244 ± 0.793

The mixed group uses three readers and one transfer writer. Its total combines different operations. The locked baseline stores primitive balances behind one lock; STM also pays for boxing, tracking, history, and retries. That coarse lock wins this small-transfer workload by about 5× over optimistic STM. Optimistic transfers score about 18% above pessimistic transfers; the mixed-group intervals overlap.

These results compare execution strategies with explicit hints, rather than test automatic selection. Machine details and source revisions were not captured, limiting reproducibility. The raw JMH JSON, console log, and benchmark guide provide the measurements, settings, and workload definitions.