Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of placeholder images #1325

Merged
merged 4 commits into from
Sep 7, 2021
Merged

Improve performance of placeholder images #1325

merged 4 commits into from
Sep 7, 2021

Conversation

mill1000
Copy link
Contributor

@mill1000 mill1000 commented Sep 4, 2021

This PR improves performance for placeholder images:

  • Use a common URL for placeholder images allowing browsers to cache the image
  • Applies requested size to the placeholder image reducing transfer size

I'm leaving this as a draft for now as I don't know if there are negative impacts from removing the updatedAt field from the getCoverArt requests for placeholders.

Original behavior
Original behavior has to make a request for each placeholder image at original size. My system/internet has lousy upload performance resulting in noticeable delay in fetching placeholders.
original requests

New behavior
With the following changes only a single request for the place holder is performed, and at a reduced size.
new requests

@mill1000
Copy link
Contributor Author

mill1000 commented Sep 6, 2021

It appears the API does not make use of the "updatedAt" field so I believe it's removal should have no negative side effects.

func (c *MediaRetrievalController) GetCoverArt(w http.ResponseWriter, r *http.Request) (*responses.Subsonic, error) {
id := utils.ParamStringDefault(r, "id", "non-existent")
size := utils.ParamInt(r, "size", 0)

@mill1000 mill1000 marked this pull request as ready for review September 6, 2021 23:38
core/artwork.go Outdated
@@ -127,6 +128,12 @@ func (a *artwork) getArtwork(ctx context.Context, id string, path string, size i
if err != nil {
log.Warn(ctx, "Error extracting image", "path", path, "size", size, err)
reader, err = resources.FS.Open(consts.PlaceholderAlbumArt)

if size != 0 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also check for err == nil here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean like so?

if (size != 0 && err == nil)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. if it returns error, you should not call resizeImage with the streamer (it may even be nil)

}

return
}

func resizeImage(reader io.Reader, size int) (io.ReadCloser, error) {
func resizeImage(reader io.Reader, size int, usePng bool) (io.ReadCloser, error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why keep to placeholder png?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the transparency. It threw off the visual style when it suddenly got a white background.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hummm, makes sense.

@@ -49,7 +49,9 @@ const getCoverArtUrl = (record, size) => {
...(record.updatedAt && { _: record.updatedAt }),
...(size && { size }),
}
return baseUrl(url('getCoverArt', record.coverArtId || 'not_found', options))
if (record.coverArtId)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a bit of nitpick: Can you put braces {} around the statements in the if and else blocks? Or rewrite it with a ternary operator:

  return record.coverArtId
    ? baseUrl(url('getCoverArt', record.coverArtId, options))
    : baseUrl(url('getCoverArt', 'not_found', size && { size }))

I strive to avoid if blocks without braces throughout the code. This arguably makes code easier to maintain, reducing the possibility of wanting to add a new statement in the if block and forgetting to add the braces. This is similar to what Go forces you to do: https://golang.org/doc/effective_go#if

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. Opt'd for braces in this case. The ternary seems a bit long and unweildy

@deluan deluan merged commit fb11080 into navidrome:master Sep 7, 2021
@mill1000 mill1000 deleted the issues/placeholder_cache branch September 7, 2021 02:42
@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants