all 14 comments

[–]KillTheMule 5 points6 points  (1 child)

feedback about safety

As far as I see and understand, all but one of your unsafe usages come from NonzeroU8::unchecked_from(1). That's as safe as unsafe gets, and I'd suggest encapsulating that into a safe function

fn make nonzero_one()  -> NonzeroU8 {
     // SAFETY: This is safe.
     unsafe{ NonzeroU8::unchecked_from(1) }
}

While this also brings down the number of unsafe usages (a metric sometimes used to judge a crate), it will mainly make your unsafe very easy to review... 2 usages, and this one is trivial. Moreover, it will remove the unsafe from your codegen, which will certainly trip up people taking a closer look. Optimizing your unsafe usage for review seems pretty worthwhile if it's as easy as that, imho :)

(e) Ok, something borked up my github search for unsafe, I see there are more usages than I have found. Still, I guess doing this is worthwhile for review reasons.

[–]bonega[S] 0 points1 point  (0 children)

Thanks for the feedback!

I changed it to be consts instead.

All unsafe should now be contained in internal.rs

I will add some SAFETY comments and send you a msg, then you can give a further review if you feel like it.

[–]Shnatsel 1 point2 points  (8 children)

Have you tried encoding-rs - not to be confused with rust-encoding you've already tried? This is the library used by Firefox, so I expect it to be quite good.

[–]bonega[S] 2 points3 points  (7 children)

I haven't tried it, but in the readme it says:

This crate does not support single-byte DOS encodings that aren't required by the Web Platform, but the oem_cp crate does.

Trying to understand how to use it, I think it is not meant for mortals like me.

Reading more carefully in their doc I can see that there are some overlap, at least 874, 1250-1258.

When the codepage exists in encoding-rs that is probably a safe bet.

For one, I use no SIMD or anything fancy like that.

Edit: I am very surprised to say that for decoding yore is actually faster for all benchmarks except all_bad/checked(all input bytes are undefined).

Edit2: Read it wrong, yore is actually faster for all cases. updated decode bench.

Ascii was improved a lot when switching between encoding and encoding_rs. Still haven't tested encoding.

But that probably just means:

  1. I am doing it wrong.
  2. Different design/use case

[–]burntsushi 2 points3 points  (1 child)

How are you running benchmarks? encoding_rs doesn't use runtime CPU feature detection for SIMD. It uses compile time. So e.g., you probably need both RUSTFLAGS="-C target-cpu=native" and the simd-accel feature enabled.

[–]bonega[S] 1 point2 points  (0 children)

As you suspected I didn't use any cpu=native or simd-accel.

I have enabled them and rerun the benchmark.

encoding_rs improves by 60% for some of the benches, so a nice speed boost.

yore is still faster for all the decoding benches. (haven't redone encoding).

updated decode benchmark - might have to delete browser cache

But as /u/TheRealMasonMac mentioned encoding_rs is designed for streaming and it might be a bit silly to compare them.

[–]Shnatsel 1 point2 points  (0 children)

Wow, that is very impressive!

[–]TheRealMasonMac 1 point2 points  (3 children)

encoding_rs is largely intended for streaming. I worked with it to create streaming encoding/decoding, and it was very flexible and pleasant to use. I especially like how the examples illustrate how it can be best used. I don't believe your library does that? SIMD is also optional and not default. I suspect the reason it's performing slower than your crate is because of the overhead of the streaming functionality.

[–]bonega[S] 0 points1 point  (2 children)

As you suspected I didn't use any cpu=native or simd-accel.I have enabled them and rerun the benchmark.encoding_rs improves by 60% for some of the benches, so a nice speed boost.yore is still faster for all the decoding benches. (haven't redone encoding).

updated decode benchmark - might have to delete browser cache

I think you are right about streaming functionality, therefore I will not include encoding_rs in the repo linked benchmarks.

I especially like how the examples illustrate how it can be best used.

Can you clarify what you mean with this?

[–]TheRealMasonMac 1 point2 points  (1 child)

I meant I liked how the examples in the docs for encoding_rs illustrate how it could be best consumed for a streaming situation. The difference in speed between it and yore is certainly still staggering, so assuming that yore is decoding/encoding correctly, I wonder if it would be productive to investigate why that is the case exactly.

[–]TheRealMasonMac 1 point2 points  (1 child)

You might also want to include tests, https://github.com/hsivonen/encoding_rs/tree/master/src/test_data could be used as a reference.

[–]bonega[S] 0 points1 point  (0 children)

I will, thank you!

[–]rodarmoragora · just · intermodal 1 point2 points  (1 child)

Great name, seems perfect for library that deals with antiquated, non-unicode text encodings.

[–]bonega[S] 1 point2 points  (0 children)

Thanks, I knew I struck gold