mirror of
https://github.com/ramsayleung/rspotify.git
synced 2026-04-26 07:55:55 +03:00
[GH-ISSUE #160] There are a lot of unnecessary allocations in the library #56
Labels
No labels
Stale
bug
discussion
enhancement
good first issue
good first issue
help wanted
pull-request
question
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/rspotify#56
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @kstep on GitHub (Nov 24, 2020).
Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/160
Is your feature request related to a problem? Please describe.
There are a lot of places with unnecessary clones/vector constructions/other allocations.
E.g.
get_id()function does things like this:This leads to unnecessary allocation of a piece of memory, immediately thrown away.
It probably should be
if _type.as_str() != fields[len -2].Also, there are a lot of places with
something_ids.into_iter().collect::<Vec<_>>().join(","), which allocates a vector just to throw it away. It can be replaced with justsomething_ids.into_iter().join(",")usingitertoolscrate, which is very efficient, trying hard to allocate as little memory as possible and joins elements in one pass instead of two.Another example is
&format!("me/player/repeat?state={}", state.to_string()), why allocate a transient string here? It should be something like&format!("me/player/repeat?state={}", state.as_str()).Describe the solution you'd like
Use
&'static strto get a string representation of enums. Use slices and iterators without transient allocations withcollect()wherever possible. Use enums to compare things instead of strings. Avoid.to_string()unless totally necessary.Describe alternatives you've considered
Leave things as they are. But I think using unnecessary allocations in languages like Rust is a sin, as doing so is losing the point of such a low-level language at all.
@marioortizmanero commented on GitHub (Nov 24, 2020):
I completely agree, I would like to reduce the number of allocations in this crate as well. But IMO that's currently very low priority, we're under some refactoring right now and lots of stuff could change. For instance I wanted to rewrite
get_idcompletely because the function itself is quite messy. Maybe after we release the next version we can focus on things like this.For simple things you can always open a PR and I'll gladly review it, but I wouldn't want to lose too much time on this yet.
@marioortizmanero commented on GitHub (Dec 29, 2022):
This is now too old to be relevant. If anyone still thinks there are too many allocations (only if removing them doesn't hurt usability), please open a new issue with examples from the codebase. Thanks!