ozankasikci / rust-music-theory

A music theory guide written in Rust.
MIT License
626 stars 28 forks source link

Use lazy_static to avoid unnecessary creation of static Regexes #10

Closed Kestrer closed 4 years ago

Kestrer commented 4 years ago

Currently many of the from_regex functions recompile the regex each time they are called, which can be quite slow. This PR uses the lazy_static crate to avoid this, by having the regexes become lazy-initialized and global.

ozankasikci commented 4 years ago

This should indeed improve the performance. Although i'm not a fan of having unwraps in the codebase, i don't think it's possible to handle errors properly if we use the lazy_static crate. Still should be ok because i don't expect to see undefined behaviour from a Regex::new call.

Thanks for the PR!

XBagon commented 4 years ago

unwraps are a very legit way to just express that the Result/Option are expected to contain Ok/Some. If they don't something very bad went wrong (Memory corruption?), so you kinda want it to crash then. Also when having constant parameters, you can expect unwrap to never panic under normal circumstances, when it compiled initially.