There is an oversight in the optimising compiler which makes it slower than it should be. I have filed an issue to fix it.
The issue is that AOT compiler emits some unnecessary speculative code when inlining ByteData
methods for writing 8, 16 and 32-bit integers. If it later can't prove that its speculation holds it ends up generating much worse code. In some sense this is something left over from pre-Dart 2 times.
We are going to fix it in the compiler. However you can also work around it by doing explicit masking on arguments of these methods. For example if you rewrite the core of the benchmark like this
buffer.setInt32(offset, intContent & 0xFFFFFFFF); offset += 4; intContent++;
buffer.setInt32(offset, intContent & 0xFFFFFFFF); offset += 4; intContent++;
buffer.setInt16(offset, intContent & 0xFFFF); offset += 2; intContent++;
buffer.setInt16(offset, intContent & 0xFFFF); offset += 2; intContent++;
buffer.setInt8(offset, intContent & 0xFF); offset += 1; intContent++;
buffer.setInt8(offset, intContent & 0xFF); offset += 1; intContent++;
It should significantly improve the performance of the benchmark. Though there would still be some difference between release and debug mode (2-3x) because there are still optimisation opportunities in the generated AOT code.