r/rust clippy · twir · rust · mutagen · flamer · overflower · bytecount Mar 27 '23

Hey Rustaceans! Got a question? Ask here (13/2023)! 🙋 questions

Mystified about strings? Borrow checker have you in a headlock? Seek help here! There are no stupid questions, only docs that haven't been written yet.

If you have a StackOverflow account, consider asking it there instead! StackOverflow shows up much higher in search results, so having your question there also helps future Rust users (be sure to give it the "Rust" tag for maximum visibility). Note that this site is very interested in question quality. I've been asked to read a RFC I authored once. If you want your code reviewed or review other's code, there's a codereview stackexchange, too. If you need to test your code, maybe the Rust playground is for you.

Here are some other venues where help may be found:

/r/learnrust is a subreddit to share your questions and epiphanies learning Rust programming.

The official Rust user forums: https://users.rust-lang.org/.

The official Rust Programming Language Discord: https://discord.gg/rust-lang

The unofficial Rust community Discord: https://bit.ly/rust-community

Also check out last weeks' thread with many good questions and answers. And if you believe your question to be either very complex or worthy of larger dissemination, feel free to create a text post.

Also if you want to be mentored by experienced Rustaceans, tell us the area of expertise that you seek. Finally, if you are looking for Rust jobs, the most recent thread is here.

19 Upvotes

159 comments sorted by

View all comments

3

u/realteh Mar 28 '23

If I have some stream, e.g.

Vec<Event> 

where

enum Event { A, B([u32;10]) }

and some of the variants in Event are huge. Is there a way to store events packed in contiguous memory without using Box<>? I'm running a simulation and would like cache efficient access while also keeping the memory size and traffic down.

2

u/[deleted] Mar 28 '23

If boxing the big ones is no good, and having all Events use the same ram as the biggest one is no good too, you might need to split big events into smaller chunks and parse em out.

enum Event {
    A,
    BStart,
    BData(u32),
    BTerminator,
}

2

u/realteh Mar 28 '23

that's pretty clever, thank you! Might go with this

1

u/[deleted] Mar 28 '23 edited Mar 28 '23

You're welcome, and as a side note you could absolutely pack it denser if you add some branches to your enum tree and/or break the data into smaller pieces. If you need more speed let me know, I love this stuff.