lz4_flex 0.7.2 reaches parity with cpp reference implementation on speed and ratio
Speed Comparison:
https://github.com/PSeitz/lz4_flex#results-v072-18-01-2021-safe-decode-and-safe-encode-off
Changes Improving Compression Speed:
Improve Compression Ratio:
- Add Backtracking of Duplicates With this change the compression ratio is even slightly better than in the cpp implementation.
24 comments · 96% upvoted · other discussions
Following this change
count_same_bytesis unsound - it offsets the pointer by the value ofcandidatewithout any bounds checks, which may result in out-of-bounds access.At present this function should be marked an
unsafe fn, since it's up to the caller to validate that the offset passed to this function is in bounds. For more info on the contracts a function not markedunsafe fnhas to uphold, see the Rustonomicon.vlmutolo 7 points · Jan 2021
How do you go about finding all these bugs
Shnatsel 4 points · Jan 2021
I tend to
rg unsafeand go from there, or even typeunsafein github search box.In this case I looked up the commit message because I was curious how they sped that up, and realized they don't actually validate the value before adding it to the pointer.
https://doc.rust-lang.org/nomicon/ is a good resource for understanding what kind of unsafe code is OK and what isn't.
Fuzzing is also helpful for larger or more complex codebases.
Pascalius [OP] 3 points · Jan 2021
In this case
candidateis a pointer into input and can never be out-of-bounds.The issue currently is, if I mark the function as unsafe, the caller needs to wrap the call with unsafe. There is a normalization layer between safe code and unsafe code, where the call looks the same. So instead:
I would need:
Which seems pretty verbose. In that case it probably would make sense to split the unsafe and safe version in two seperate files. As I understand it this is an api convention to signal the programmer unsafe code and does not cause UB itself, by missing it.
[deleted] 23 points · Jan 2021
For private APIs, practically yes. However, it is still considered good style to mark even those functions as
unsafeif they rely on a safety invariant that has to be upheld by the caller.Lej77 9 points · Jan 2021
I'm not saying you should but you could have a macro like:
that would allow you to write it as:
Note that currently rustfmt will format code inside macros that are invoked with parenthesis
((not brackets[or{), so formatting should still work.Plasma_000 7 points · Jan 2021
I think it boils down to “should private unsafe functions be marked as such even if they can’t be used unsafy by the public API” - which I don’t think there is a consensus on...
I would err on the side of marking unsafe just because a future dev might work on the project and use the function in an unsound way.
Shnatsel 5 points · Jan 2021
The Nomicon explicitly states:
It does not make a distinction between public and private.
backtickbot -4 points · Jan 2021
Fixed formatting.
Hello, Pascalius: code blocks using triple backticks (```) don't work on all versions of Reddit!
Some users see this / this instead.
To fix this, indent every line with 4 spaces instead.
FAQ
You can opt out by replying with backtickopt6 to this comment.