3 ms·
> (I'm sure people will shout that this isn't the right way to parse optional flags) Since that sounds like a challenge, just wanted to make sure that people a
by kibwen 6y ago
> (I'm sure people will shout that this isn't the right way to parse optional flags)
Since that sounds like a challenge, just wanted to make sure that people are aware of Iterator::nth (https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.nth https://doc.rust-lang.org/std/iter/trait.Iterator.html#metho...), which means you can entirely replace lines 8 and 9 with `let cartridge_path = env::args().nth(1);` :) In combination with Iterator::any to replace line 10, you can avoid a whopping five whole heap allocations this way!! :P
- Waterluvian 6y agoOoooh thanks for sharing! I am a beginner to Rust (this emulator being my second project). I was not aware of that. I feel like when I'm done I need to find a bit of cash and get a veteran to code review the whole thing. I'm learning a ton by suffering through it myself. But also probably missing a lot of great shortcuts and tips. I assume the "five heap allocations" is kinda tongue in cheek? My understanding is that while it is good to avoid heap allocations, given this is a called-once piece of code, it's not really a big deal?
- kibwen 6y ago> I assume the "five heap allocations" is kinda tongue in cheek? My understanding is that while it is good to avoid heap allocations, given this is a called-once piece of code, it's not really a big deal? Indeed. :) I can't envision a circumstance where avoiding exactly five small allocations outside of any loop would be observable in either time or memory usage. As a Rust programmer it simply provides a warm and fuzzy feeling to avoid allocations in places where it not only doesn't obscure the code, but actually makes it shorter. At best, this could be a small step towards running your code on embedded hardware (which isn't unreasonable for an emulator like this), but only if you were to also eliminate all other allocations within your program.
- Waterluvian 6y agoGiven some ROMs are multiple megabytes I'm not sure how this would work. Maybe I'm wrong but isn't the stack only a few MB in max size? Appreciate your earlier comments. Thanks.