Skip to content

Lookup tables or the JDK

Andrey Shindarev edited this page Oct 18, 2026 · 1 revision

Since 2.1.0 a model of CRC-32/ISO-HDLC and its aliases, of CRC-32/JAMCRC and of CRC-32C can be calculated by the classes java.util.zip.CRC32 and java.util.zip.CRC32C of the JDK, which the JVM runs on the CRC instructions of the CPU (see CRC-32 and CRC-32C of the JDK in the README). The default is the lookup tables of the library. This note says which way to choose; the numbers are those of the README and of Speed of 2.1.0 (JMH, Intel Core i9-9980HK).

In short

  • CRC-32C on Java 9+: the JDK. It is faster from 8 bytes on (on JDK 25: 16 bytes 13.8 → 5.5 ns, 256 bytes 145 → 13 ns, 4 KB 2.14 → 0.13 µs), and its speed does not depend on the address of the data. Java 8 has no CRC32C: there the lookup tables calculate CRC-32C either way.
  • CRC-32 of about 256 bytes and more: the JDK (on JDK 25: 256 bytes 141 → 62 ns, 4 KB 2.24 → 0.20 µs, 1 MB 515 → 42 µs). Below 64 bytes (128 on Java 8) the lookup tables calculate it anyway, and at 16 bytes the class of the JDK only adds the check of the length (13.8 → 16.0 ns).
  • Every other CRC: the lookup tables, whatever the way: the classes of the JDK know these three algorithms only.
  • Choose per model rather than for the whole JVM when the application calculates other CRCs as well: CrcModel.of("CRC-32C", true) for the models that calculate much data, the default for the others.

Why the default stays the lookup tables

  • Three algorithms of 112. For the other 109 the way changes nothing, and they can only lose by the JDK of other models (the next point).
  • The calls of the calculation stay of one kind. As long as no model of the JDK is chosen, the class of the calculation with the JDK is not even loaded. When models of both ways run in one JVM, the JIT compiler compiles the calls of the calculation for both, as for CRCs of other widths: a model of the lookup tables after a model of CRC32 in the same JVM got 6–16% slower for CRC-64 on JDK 8, 6–8% slower for CRC-16 and CRC-32 of 1 KB and 9–10% faster of 256 bytes on JDK 25. The system property makes every CRC-32 a model of the JDK, so an application that calculates other CRCs too mixes both ways.
  • Short data gains nothing. The class of the JDK calculates from 64 bytes (128 on Java 8) for CRC-32 and from 8 bytes for CRC-32C; shorter data stays with the lookup tables, plus the check of the length.
  • CRC32 depends on the address of the data. CRC-32 of 64 bytes, 256 bytes and 1 KB takes 15–17, 21–24 and 48–50 ns when the array starts at an address divisible by 16, and 36–41, 49–57 and 76–86 ns at any other address (about 15% at 4 KB, nothing at 1 MB). The JVM chooses the address, and the garbage collector may move the array. The lookup tables and CRC32C do not depend on it.
  • A change of the default changes the speed of every application that upgrades: it belongs to a minor release with its own measurements, not to a patch.

How to choose

CrcModel crc32c = CrcModel.of("CRC-32C", true);            // CRC32C on Java 9+, the lookup tables on Java 8
CrcModel crc32 = StandardCrc.CRC_32_ISO_HDLC.model(true);  // CRC32 from 64 bytes, 128 on Java 8
CrcModel same = CrcModel.of("CRC-32C").withJdk(true);      // the model crc32c
boolean byJdk = crc32c.isJdk();                            // true on Java 9+

For the whole JVM: -Dcom.github.sftwnd.crayfish.common.crc.jdk=true, or CrcDescription.setJdk(true) before the models are chosen; a model keeps the way it was chosen with. The CRC values do not depend on the way: the CRCs of both ways are equal and can be combined, so the parts of an application may use different ways.

Numbers

model.value(data) with the lookup tables → with the class of the JDK, the average time of a call:

Model, JDK 16 B 256 B 4 KB 1 MB
CRC-32, JDK 25 13.8 → 16.0 ns 141 → 62 ns 2.24 → 0.20 µs 515 → 42 µs
CRC-32C, JDK 25 13.8 → 5.5 ns 145 → 13 ns 2.14 → 0.13 µs 513 → 41 µs
CRC-32, JDK 8 18.8 → 19.6 ns 141 → 59 ns 2.41 → 0.20 µs 615 → 39 µs

The CRC-32 rows are those of data at an address not divisible by 16. The medians of every length on JDK 8, 21 and 25 are in The classes of the JDK of the page of the speed. The thresholds of 64, 128 and 8 bytes and all the numbers were measured on x86; on ARM the JVM uses other instructions, which are not measured yet.

Clone this wiki locally