Make AESFastEngine actually faster than AESEngine by timw · Pull Request #72 · bcgit/bc-java

AESFastEngine is slower than AESEngine on JDK 1.7/1.8 (1.6 is about even) despite pre-computing more, due to the additional array range checking incurred by having 4 table lookup arrays (vs only one in AESEngine).

This change compacts all of the T0/T1/T2/T3 tables into a single table, trading off an offset addition on each lookup for removing the range checking.

Registerisation of the state variables on encrypt is also done for an additional speed bump.
Registerisation is a bit sensitive - AESEngine and AESFastEngine.decryptBlock incur a penalty if state is registerised further, probably due to register spilling on x64.

Speed is up on JDK 1.7/1.8, slightly down on JDK 1.6 (which at least with AggressiveOpts is strangely faster than JDK 1.7).

Representative throughput changes (encrypt and decrypt are comparable):
(i7 Macbook Pro, 2.3GHz)

Java 6

  • AESEngine: 143 MB/s
  • AESFastEngine before: 134 MB/s
  • AESFastEngine after: 129 MB/s

Java 6 + AggressiveOpts

  • AESEngine: 148 MB/s
  • AESFastEngine before: 148 MB/s
  • AESFastEngine after: 145 MB/s (-2%, 2% slower than AESEngine)

Java 7

  • AESEngine: 138 MB/s
  • AESFastEngine before: 126 MB/s
  • AESFastEngine after: 164 MB/s (+30%, 19% faster than AESEngine)

Java 8

  • AESEngine: 153 MB/s
  • AESFastEngine before: 134 MB/s
  • AESFastEngine after: 163 MB/s (+22%, 7% faster than AESEngine)