Open jasperpotts opened 2 months ago
After this is fixed, update PbjBytesHasher
to use it.
this is currently implemented with existing code:
private final MessageDigest eventDigest = DigestType.SHA_384.buildDigest();
private final WritableSequentialData eventStream = new WritableStreamingData(new HashingOutputStream(eventDigest));
EventCore.PROTOBUF.write(event.getEventCore(), eventStream);
Do you think there is any benefit in adding this new method? Other than some convenience?
Probably fine, don't think there is a performance impact from first look other than extra object created. Adding the PBJ method would make code cleaner and give single point to performance test and improve over time. We know that PBJ serialization is expensive and if there is a more efficient way to hash a PBJ object in the future then that would be nice if there was only a couple APIs where we know it is done and they are used everywhere.
Problem
Today we have to serialize to a byte array before we can hash a PBJ protobuf object. This is wasteful in time and more importantly heap object garbage. For example we do:
EventCore.PROTOBUF.toBytes(event.getEventCore()).writeTo(eventDigest)
Solution
Add an overloaded
write(MessageDigest digest)
method to Codec interface and generated implementations. It can call the normalwrite(T o, WritableSequentialData out)
use a custom implementation ofWritableSequentialData
that wraps ajava.security.MessageDigest
mapping thewrite(byte)
andwrite(byte[])
methods toupdate(byte)
andupdate(byte[])
methods on MessageDigest. This will avoid the creation of temporary byte arrays and ByteArrayOutputStreams.Alternatives
No response