Skip to content

Read the caches without a lock on JRuby - #95

Merged
czj merged 2 commits into
mainfrom
claude/magical-noether-qcvl8n
Nov 30, 2026
Merged

czj merged 2 commits into
mainfrom
claude/magical-noether-qcvl8n

Conversation

@czj

@czj czj commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

Since #90, every cache read takes a Mutex off CRuby, so on JRuby a process answering user agents it has already seen stays at one thread's throughput whatever the number of threads (#93).

JRuby's cache tables are now a java.util.concurrent.ConcurrentHashMap, whose reads take no lock. Bot's keyword patterns, the last table read under a lock, become a frozen table that a write replaces whole under KEYWORDS_LOCK, on every Ruby. CRuby's tables are unchanged, and TruffleRuby keeps the locked tables.

  • Keys stay Ruby Strings: JRuby would convert each key to a Java String, which merges keys that differ only in encoding (a Latin-1 user agent read as binary got the cached answer of its UTF-8 twin) and raises for encodings Java lacks (ISO-8859-14). Keys go in as RubyString, compared like Hash keys, and a new test checks these cases on every engine.
  • Patterns copied on write: a new keyword list copies at most 63 entries under the lock, a write that happens a handful of times per process.
  • Same answers: CRuby's suite passes unchanged, and on JRuby the unit tests give the same results as on main.
Measurements (JRuby 10.0.2, 4 cores, shared machine)

The script from #93: 20 user agents asked over and over, so every lookup is a cache hit, 10 s of JIT warmup, median of 3 measures of 2 s, two runs each, alternated. Throughput relative to one thread:

Threads main, run 1 main, run 2 this branch, run 1 this branch, run 2
1 1.00× 1.00× 1.00× 1.00×
2 0.86× 0.81× 0.97× 1.52×
4 0.81× 1.02× 1.54× 2.86×
8 0.81× 0.83× 1.60× 2.91×
16 0.85× 0.89× 1.53× 2.97×

The machine's load varies between runs, but main never scales and this branch does on every run.


Generated by Claude Code

czj added 2 commits November 30, 2026 22:35
Since #90 every cache read took a Mutex off CRuby, so on JRuby throughput
stayed at one thread's pace whatever the number of threads (#93). JRuby's
tables are now a ConcurrentHashMap whose keys stay Ruby Strings, and Bot's
keyword patterns a frozen table replaced whole under its lock.

Closes #93

Assisted-by: Claude [Claude Code]
@czj
czj merged commit 6991862 into main Nov 30, 2026
19 checks passed
@czj
czj deleted the claude/magical-noether-qcvl8n branch November 30, 2026 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant