well-typed / cborg

Binary serialisation in the CBOR format
https://hackage.haskell.org/package/cborg
191 stars 86 forks source link

hPutSerialise should use hPutBuilder? #317

Open adamgundry opened 1 year ago

adamgundry commented 1 year ago

While debugging a performance issue I noticed that hPutSerialise ends up calling hPut on toLazyByteString . encode. As the comments on hPutBuilder note:

This function is more efficient than hPut . toLazyByteString because in many cases no buffer allocation has to be done. Moreover, the results of several executions of short Builders are concatenated in the Handles buffer, therefore avoiding unnecessary buffer flushes.

Thus I think it would be better to use hPutBuilder instead of hPut.

At the same time I noticed that writeFileSerialise calls withFile rather than withBinaryFile. I'm not sure what difference this makes, but it seems like the latter might be a better fit?

bgamari commented 1 year ago

Good catch regarding hPutBuilder.

I suspect that withBinaryFile won't make any difference as it (IIRC) only affects functions like System.IO.hPutStr which may need to encode. bytestring's put functions shouldn't encode. Nevertheless, it wouldn't hurt to use withBinaryFile for clarity.