TailCache · Part 4 of 6

How do you give two Java caches the same work?

I gave both caches the same list of records to read and update. One shortcut in the test code could still have made Caffeine look cheaper to run.

I thought “95% reads and 5% writes” was a fairly precise description of a test. Try giving it to two people. One might update the same five records all afternoon. The other might eventually update every record in the cache. Both followed the instructions.

For TailCache, I needed to hand Caffeine and Chronicle Map the same afternoon. I made a list of record IDs to visit and a separate list saying whether to read or replace each record. Each list came from a repeatable sequence. Its starting number is called a seed. Use the same seed and you get the same choices again. I could now change the share of writes without quietly changing the order of the IDs as well.

Some tests gave every record an equal chance of being picked. Others made a few records especially popular. When I added workers to test simultaneous access, they all used one cache and started at different places in the list. Sixteen workers with sixteen private caches would tell me very little about what happens when two of them want the same record.

The shortcut that would have favored Caffeine

A write needs new data. The easy way to supply it was to prepare one array of bytes and use it for every replacement. Suppose I update records 42 and 43 using that array. Caffeine can make both entries point to the very same object. Chronicle still stores a set of bytes at 42 and another at 43. Repeat that often enough and I’ve given one cache a memory saving that comes entirely from how I wrote the test.

We prepared a separate replacement for each record. Then another cost became visible. At 2,048 records and 4,096 bytes per replacement, the test program itself held 8,388,608 bytes, or 8 mebibytes (MiB), of data on the Java heap. It held those arrays even when Chronicle was keeping the actual cache off-heap. This version could compare access patterns. I wouldn’t use its heap readings to tell someone how much memory they could save.

What did reopening the file prove?

For the version of Chronicle that saves data to a file, we wrote the records, closed the store, reopened it and checked the contents. The records survived. Whether the next read would be quick was a separate question. The operating system might still have to bring part of the file into memory or write earlier changes back to disk. I kept that result separate and took the in-memory comparison forward. I already had enough ways to give the wrong thing credit.