mirror of
https://github.com/librespot-org/librespot.git
synced 2026-04-27 08:15:50 +03:00
[PR #459] [CLOSED] Refactor SpotifyId #937
Labels
No labels
A-Alsa
SpotifyAPI
Tokio 1.0
audio
bug
can't reproduce
compilation
dependencies
duplicate
enhancement
good first issue
help wanted
high priority
imported
imported
invalid
new api
pull-request
question
reverse engineering
wiki
wontfix
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/librespot#937
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?
📋 Pull Request Information
Original PR: https://github.com/librespot-org/librespot/pull/459
Author: @alcore
Created: 4/10/2020
Status: ❌ Closed
Base:
dev← Head:refactor-id📝 Commits (1)
8d46305Refactor SpotifyId📊 Changes
1 file changed (+362 additions, -58 deletions)
View changed files
📝
core/src/spotify_id.rs(+362 -58)📄 Description
This PR includes a refactoring of
SpotifyId.In my particular use case I am dealing with a relatively large amount of track IDs (GBs) as playback history - those need to be base62 encoded onto the wire in batches of up to 100 and the current encoding algo was at my QPS prohibitively expensive as it performs 128-bit division + modulo, which compiles down to an unoptimized runtime call.
It now instead uses 64-bit arithmetic (which could relatively trivially be optimized for 32-bit arches behind a build condition) with an algo in use in the cryptocurrency ecosystem for their base58 encoding, further optimized for our case.
On
rustc 1.44.0-nightly (b543afca9 2020-04-05), Windows 10, i7 4770k @ 4.4GHz (windows/x86-64)the result is as follows:I then went ahead to pick some further low hanging fruit in the other methods, without resorting to LUTs nor SIMD intrinsics. I.e. the change to the base62 encoding algo is the only non-trivial piece of code contained.
from_uri()had an overhead of ~400ns/op on top of itsfrom_base62()call, which is now reduced to 7ns, with some trivial inline parsing based on our knowledge of the inputs. FileId gain was primarily due to it needlessly allocating 20 Strings - one for each byte.I included simple tests and rudimentary docs, which felt less welcome than the code - given there are none of either in the entire codebase ;-)
Rationale
I realize librespot itself in a player scenario barely uses those codepaths and that the PR introduces a bit of additional complexity. I would still like to be able to use this as a library and reuse the types in my project, given they do what I want -- just not efficiently enough.
Additional
to_uri()method. However, it moves part of the logic to theSpotifyAudioTypeenum (and is 100ns faster, albeit less idiomatic) where I feel it should belong;SpotifyObjectstruct to encapsulate the ID and object type instead.This would involve:
SpotifyAudioTypefor anSpotifyObjectTypeenum and filling it with other known types (including non-playable ones), keeping it unitary (with a placeholderUnknowntag for unrecognized types);SpotifyIdtoSpotifyObject. For consistency I'd love to rename ID toSpotifyObjectIdin that case as well;SpotifyContextto encapsulate aSpotifyObjectTypein a given context (including the URI);SpotifyIdshould convert to/from an URI or the base62 encoding. That is, if bumping MSRV is a go, as those need to be TryInto/TryFrom;A 'go' for this would be welcome as I'm eager to get coding.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.