-
Notifications
You must be signed in to change notification settings - Fork 160
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
Will memory alignment be supported? #360
Comments
Very unlikely, because that could cause portability issues: type alignment is architecture dependent. I would also be surprised if the performance difference was significant. In most cases you can use functions like |
I was thinking more about a bigger data-structure like roaring bitmaps. In C they developed a "frozen format" that can be directly reinterpreted and is much faster than allocating and copying entire containers. |
I see. Supporting external libraries that require alignment isn't one that I had considered. Has anyone tried porting roaring bitmaps to Rust and removing the alignment requirements? All the benchmarks I've seen show that there's basically no different between aligned and unaligned access on modern CPUs. |
Thank you for reopening this issue.
There indeed is a pure Rust port of the I was thinking about using the power of memory mapping to keep things where they are and avoid duplicating them elsewhere to be able to read them when this would be unnecessary if the memory were correctly aligned. We talked a little bit about this format with Daniel Lemire in this issue. It can be hard to read, so I advise you only to read the last message:
|
Ah yes, I can see why that's expensive. Is it possible to instead borrow the I took that approach with my |
It seems like a good idea, but I am not sure it will be easy to implement instead of just porting the frozen format C implementation to Rust. However, performance cost can be significant on some arch like the ARMv7 (Apple Mac M1 is an ARMv8) where an unaligned instruction generates four instructions instead of two when the bytes are unaligned. I am just thinking about the implementation detail on your side. Wouldn't it be some padding bytes in front of the value to make it correctly aligned on the page? I understand that you must keep the length of the padding to be able to skip it when reading 🤔 |
Well, additional instructions doesn't necessarily translate into a difference in performance. Except for very tight loops, instructions should be practically free compared with the loads from memory/disk that redb has to do. Oh yes, the implementation would be pretty straightforward. The thing that I'm worried about is portability between architectures. The alignment of the primitive types is not specified in Rust, and is platform dependent. I have not review every architecture that's supported to see if they have the same alignment, but I could imagine a 64bit platform on which say |
For using rkyv in redb, memory alignment would also be very interesting. I'd think it's sufficiently useful to make it available even if portability can't be ensured when using it. |
Fyi trying to actually use |
I was thinking that the simplest way to support alignment may be:
In practice, this might look like a type like:
Which can be used as a redb key or value, and whose value is a byte slice for which |
@casey that sounds very reasonable, and would make it straightforward to integrate with tools like |
@casey yep, that's how I'm thinking of implementing it to start. I'm going to try and make it flexible enough that if I end up making |
I think this is probably blocked on rust-lang/rust#76560 To ensure that the alignment is checked at compile time, I need code like this to work: |
#490 adds alignment to the file format, so that it can be supported in the future without requiring a file format upgrade |
I did some work on this in the alignment branch. The next step from that branch is changing However, I'm not sure this is going to be possible in the near future with the Rust type system. Basically, I need code like this to work: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=00c17b86bc4d44cec05ffab93da10cc0 and it looks like that's been ruled out for the moment due to it not being const well-formed. I don't entirely understand const wf, but it looks like rust-lang/rust#76560 won't be sufficient to make that branch work. Anyone have ideas for making this work? |
If you make the size a generic parameter it could work, not sure this fits in the rest of your design: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a239c92b3f0225c0d472bf635deb32e7 |
ya, I looked into that solution briefly, and good to see that it seems to work. However, I think it's probably not going to be a good fit for the rest of the redb API, because then those alignment generic parameters are going to pollute the whole API since nearly everything references |
@dignifiedquire I tried that out, and unfortunately it's not going to work well. The const generic parameter would leak out into the whole API, requiring things like I think the best workaround is going to be to store the values that require alignment in a separate structure, such as a separate mmap'ed file, and then store the offset & length in redb to allow retrieving the value. Or if you know anyone who works on the const generic type system, maybe you can pitch them on adding support for this case :) |
I added an example showing one way to workaround this. Please re-open if you find a way around the Rust associated const generic issue though! I do think this would be a nice feature to have in the core API, but don't want to add it if it requires adding const generic parameters to everything. |
This feature is not implemented, and there is no clear path to implementing it (see #360). However, if it becomes possible to implement it in the future, this design should be backward compatible
This feature is not implemented, and there is no clear path to implementing it (see #360). However, if it becomes possible to implement it in the future, this design should be backward compatible
This feature is not implemented, and there is no clear path to implementing it (see #360). However, if it becomes possible to implement it in the future, this design should be backward compatible
Would it be possible to move alignment concerns out of the type system? I haven't studied redb's internals yet, but it seems like adding/skip padding could be dynamic, with redb's API guaranteeing that the argument to |
Ya, that's definitely possible, but I don't want to implement it that way because it would be too easy to introduce bugs |
Nothing an |
Hey,
I was looking at redb and thought about one feature that could be very interesting and that is supported only by sanakirja right now. Memory alignment could bexcitingng when it comes to speed, for example, avoid having to copy memory could bring more performance.
The text was updated successfully, but these errors were encountered: