Home
Blog
Favorite Rust Optimization
Updated
Dot Net Perls

Favorite Rust Optimization

I have spent quite a bit of time writing Rust code, and also optimizing that same Rust code. In fact I have doubtless spent more time optimizing code than the time saved by the optimizations. In any case, I have some favorite optimizations.

The programs I use tend to copy data (byte vectors) from one source to another. They process data byte-by-byte. But the optimization is to append ranges of bytes, with extend_from_slice, instead of using push to append each byte individually.

This has some advantages:

Extend_from_slice saves a capacity and length call on each individual byte, and does these checks just once for the entire slice.
Sometimes a call to extend_from_slice can avoid multiple allocations because it only needs to check the capacity once.
Vectorization can be used to copy multiple bytes much faster when done all at once.

You can find more about this Rust optimization in the Rust category on this site—look for the Vec extend_from_slice article.

Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
No updates found for this page.
Home
Changes
© 2007-2025 Sam Allen