Skip to content

Pread extra bounds checks #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
m4b opened this issue Jun 7, 2017 · 0 comments
Closed

Pread extra bounds checks #9

m4b opened this issue Jun 7, 2017 · 0 comments

Comments

@m4b
Copy link
Owner

m4b commented Jun 7, 2017

from #7, we bounds check twice in Pread, once in the TryFromCtx, and again in FromCtx (technically, it's everytime we access the byte array).

Consequently, there's room for some optimization here.

To be clear:

  1. I'm definitely for optimization at this point, as the crate is becoming mature and I'd like to hit 1.0 soon, and well optimized numeric readers would be great
  2. I'd like to see some basic benchmarks as to whether these bounds checks are actually worth optimizing.
  3. It might be better to clean up TryFromCtx (break backwards compat, since we're below 1.0), remove offset logic from the trait, and move into Pread itself, and make fully generic with Index trait bounds, and reduce it to the bounds check + assert, which we might be ok to live with, again, needs measuring.

Anyway, as @goandylok suggests, some possible avenues:

  1. Mark FromCtx and cread‘s members unsafe to give away the responsibility of bound check to call site.
  2. Make FromCtx to be a wrapper of TryFromCtx by calling expect() on TryFromCtx's result.

Honestly, 1. is a "nuclear option" for me; I'm really hesitant to mark it unsafe, and would really require evidence (specifically benchmarks) to do so. Once it's unsafe, it's hard to put that genie back in the bottle; history has shown people are very unlikely to check bounds, even when its in their best interest, so this is definitely going to require some serious consideration. I might even prefer a UnsafeFromCtx, and have this isolated to just the Cread trait, but that might be too much.

I also don't like the divergence from the std trait; why is FromCtx unsafe? It just shouldn't fail, similar to From trait in std, which isn't unsafe.

Anyway, those are some thoughts.

@m4b m4b closed this as completed Dec 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant