-
-
Notifications
You must be signed in to change notification settings - Fork 709
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
allocate_vec returns uninitialised memory #139
Comments
Interesting... A bit of background there. Rust's Vec big initialization is dead slow in debug mode. I started using the unsafe
This is unsafe because it is by definition "undefined behavior". In my experience, it can be on the contrary more dangerous to zero out vectors that you use for instance as a memory arena. In C++ it is one of the common cause for code to fail in optimized mode and seem to work perfectly (until when though?) in debug mode. I also see how it can be a security issue (possibly exposing within the same process)... @kaedroho and @BurntSushi, I am very interested if you could educate me on the subject. My opinion at the moment is really "In practise, I don't care." and "zero-initialization actually have hidden some bug from me in the past, I would have prefer things to fail fast". BTW tantivy has a lot of much more unsafe code in its indexer, for performance reasons. If you have time for it, I'd be glad to discuss with you if you can help me figure out alternatives. |
@fulmicoton To clear the air: Consider this example: https://is.gd/ipfyEu --- If you run it, you'll silently get nonsense data. This is because your
You might consider turning on optimizations for running tests. It will increase compile times, of course. Popping up a level, I will say this: I think justifying
Can you point to the files? I see I will say these things also:
|
Well... This is not a public API. Surely having it as a public API would be an ever greater sin. :) @BurntSushi thanks for the explanation. I'll see how I can improve that. I will -at the very least- mark the function as unsafe, and
I guess I am not very patient as a person.
The role of the memory arena is both about performance (bump allocation, scratch for unalloc) and functionality. Another usage is in the I use a I have no elegant solution to the first part of the problem, and the solution to the second problem is probably somewhere in the
This sounds like great practise. I'll try to do the same but I don't think I'll ever reach your degree of discipline / QofCode. |
If you want to go down a safer route, it might be possible to refactor all the places that use |
@kaedroho @BurntSushi I found out very recently #136 two horrible design choices that were extremely expensive in term of memory (the ez to explain one, is that there is currently one Once this is fixed, the minimal memory usage in the unit test should be much smaller, and it should be fine to do buffer allocation the "safe way". @kaedroho Thanks for linking |
@fulmicoton Thanks for being receptive. :-) It's crucial that awesome projects like this don't abuse |
https://github.com/tantivy-search/tantivy/blob/0dad02791c74e572ec005abb1f75e3601bd25db5/src/common/mod.rs#L31-L41
This could lead to a number of problems if not used carefully but most rust developers would not expect this behaviour from a "safe" function.
I really think this should be replaced with something safer, or at least marked as
unsafe
The text was updated successfully, but these errors were encountered: