alt-art / genius-rs

Rust library that allows interact with Genius API
https://crates.io/crates/genius-rs
MIT License
7 stars 3 forks source link

Optimizing `get_lyrics` #7

Closed vrobweis closed 2 years ago

vrobweis commented 2 years ago

This PR targets the get_lyrics method of the Genius client, taking aim at some snippets that could lead to excessive memory allocations, or at least rely on the insecure premise that rustc will optimize those allocations away. It likely would, but we don't want to rely on that.

Most changes are documented inline, but the biggest change is that instead of pushing to a String instance as lyric sections are iterated over without specifying a size, these changes lazily find the underlying &strs and then collect them into the target Vec<String>. This can leverage size_hint and reduce necessary allocations, especially if the rustc optimization strategy ever varies or wanes.

More importantly, it will make functional proglang hipsters happy by avoiding the need for a mutable binding. /s

alt-art commented 2 years ago

Please use cargo fmt before the commits.

vrobweis commented 2 years ago

Acknowledged. Remaining draft PRs will include format passes before being marked for review.