Use u64 to track file offsets instead of usize - #2573
Conversation
This replaces usize in a lot of places. Besides being a technically breaking change, this should not impact functionality. Solves a TODO left long ago in the entry code of parse.
45e1141 to
2a6f9db
Compare
|
A lot of consumers seem to hold the entire wasm input in memory, hence these need some ergonomics to convert their offsets back to |
0f2d5de to
018d1e9
Compare
|
I have not removed |
alexcrichton
left a comment
There was a problem hiding this comment.
Personally I still feel that InMemData and MemOffset aren't worth it. This is already a very large change due to how many places it has to touch, and in my mind it's a consequence of the change at all that as casts are going to be needed when in-memory data is indexed with the offsets coming out of wasmparser. While I've no doubt that certain abstractions could cut down on the number of resulting as casts I'm not sure if the benefit of such abstractions outweighs their costs (e.g. new primitives to learn).
I can see that for
One oddity for example is that since In any case, conversion was much easier with |
|
In the course of adding docs to |
alexcrichton
left a comment
There was a problem hiding this comment.
After reading this in full again, personally I'd still subjectively disagree. I understand taht InMemData is used a fair bit, but it's a pretty nontrivial abstraction. I find it much easier to reason about possibly-slightly-more-verbose code using try_into conversions or such rather than seeing an InMemData abstraction and immediately having to go look up what it's doing and what it looks like under the hood.
At most I think it'd be ok to have a small set of helpers in wasmparser::* to do something with Range<u64> and get usize-like things out, or maybe indexing, but I'd want that to be constrained to maybe at most 2-3 functions if that were done. Otherwise I feel that InMemData is just a bit too foundational in the wrong place and it's not carrying its weight for being a foundational abstraction
Not sure I follow the "foundational" aspect. To me, the wrapper struct is as much utility as the proposed "2-3 functions". It doesn't cross into parser code, or even other crate boundaries. It's certainly not meant to show up in any public API except as an optional utility to ease the conversion to u64 ranges. A consuming crate can choose to wrap the byte slice it passes to the parsing functions so that it itself can index into that slice with the ranges coming back the parser. The fact that a crate did that wrapping should be a transparent implementation detail, and the wrapper behaves as best it can as that byte slice, except for the additional indexing access. But alas, I will push the indexing into the consuming crates and duplicate the small bits of code tomorrow. |
This replaces usize in a lot of places. Besides being a breaking change, this should not impact functionality for most users. Tracking file offset with
u64is the more principled choice.Fixes #838
Solves a TODO left long ago in the entry code of parse. Most of the places used
usizeto pass it along to other functions, sinking into Error code for nice error messages, and are thus not sensitive to the data type used. I found two cases that usedusize::MAXas a dummy index in a path that calledexpect()on the eventual error.There is a small abstraction in
offsets.rsexposed asInMemDatathat allows conversion fromRange<u64>to byte slices in case the whole data input fits in memory.