Implement BufRead/Write for en/decoders alongside Read/Write #385
-
Hello. Have been using deflate for encoding/decoding various moderatly sized data (under 5KB), where I need to parse some encoded heading variabely-sized metadata first. Have been wondering, if there is any strong reason as to why encoders and decoders do not implement BufRead/Write, if internal implementations certainly use buffered IO. Even though files in question are small, they can quickly bloat in numbers, and the neccessity of repeatedly either allocating in-memory raw buffers on heap or wrapping decoder/incoder in BufReaders/Writers (which, while enabling more "lazy", "stream-fashioned" approach, still do those same allocations internally) seems concerning perfromance-wise. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Thanks for sharing! I hope you don't mind me turning this into a Q&A due to its format. However, I also pinned it for greater visibility. That way those in the know might be able to shed some light on this - usually there are very good reasons for the API as is even though these are sometimes not easily discovered. |
Beta Was this translation helpful? Give feedback.
-
It is is not a good idea to have a single type implement both
We could provide two types that implement each trait (similar to how Note, neither trait requires repeated allocations. |
Beta Was this translation helpful? Give feedback.
Having spent more time on looking into the code, I've found out, that only unprocessed bytes from underlying stream are buffed. Output proccessed bytes are decoded on demand in quantities requested (size of output buffer) at most. Thus, wrapping
inflate
structs inBufRead
doesn't do any processed bytes unneccessary copies, if used with minimal common sense (that being e.g. not using buffering, if the end use-case is callingread_to_end
).