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

Optimise Imgix::Path#to_url by not generating the escaped query string twice #103

Closed
wants to merge 2 commits into from

Conversation

stevehodgkiss
Copy link
Contributor

Description

In profiling an application that renders a lot of Imgix images, I noticed a significant amount of time spent escaping the query string in Imgix::Path#query.

To generate a URL, Imgix::Path#query is called twice - once for the URL to be rendered, and again to generate the signature.

The change in this PR avoids generating the escaped query string twice, and almost doubles the performance of to_url on my machine.

Before:

❯ bundle exec ruby script/bench_to_url.rb
Warming up --------------------------------------
  Imgix::Path#to_url     1.683k i/100ms
Calculating -------------------------------------
  Imgix::Path#to_url     16.886k (± 0.6%) i/s -     85.833k in   5.083431s

After:

❯ bundle exec ruby script/bench_to_url.rb
Warming up --------------------------------------
  Imgix::Path#to_url     2.731k i/100ms
Calculating -------------------------------------
  Imgix::Path#to_url     27.278k (± 0.7%) i/s -    136.550k in   5.006213s

Output:

```
❯ bundle exec ruby script/bench_to_url.rb
Warming up --------------------------------------
  Imgix::Path#to_url     1.683k i/100ms
Calculating -------------------------------------
  Imgix::Path#to_url     16.886k (± 0.6%) i/s -     85.833k in   5.083431s
```
@stevehodgkiss stevehodgkiss requested a review from a team as a code owner March 30, 2021 11:33
@commit-lint
Copy link

commit-lint bot commented Mar 30, 2021

Tests

  • add benchmark script for Imgix::Path#to_url (e4cbc37)

Performance Improvements

  • avoid calculating path_and_params twice (4d0891f)

Contributors

stevehodgkiss

Commit-Lint commands

You can trigger Commit-Lint actions by commenting on this PR:

  • @Commit-Lint merge patch will merge dependabot PR on "patch" versions (X.X.Y - Y change)
  • @Commit-Lint merge minor will merge dependabot PR on "minor" versions (X.Y.Y - Y change)
  • @Commit-Lint merge major will merge dependabot PR on "major" versions (Y.Y.Y - Y change)
  • @Commit-Lint merge disable will desactivate merge dependabot PR
  • @Commit-Lint review will approve dependabot PR
  • @Commit-Lint stop review will stop approve dependabot PR

Benchmark output:

```
❯ bundle exec ruby script/bench_to_url.rb
Warming up --------------------------------------
  Imgix::Path#to_url     2.731k i/100ms
Calculating -------------------------------------
  Imgix::Path#to_url     27.278k (± 0.7%) i/s -    136.550k in   5.006213s
```
@frederickfogerty
Copy link
Contributor

Hey there @stevehodgkiss thanks for the PR! I've pinged someone on our team with more extensive knowledge of this library than me to take a look at this, and hopefully we can get this merged in soon!

@ericdeansanchez
Copy link
Contributor

@stevehodgkiss Nice! At a glance, I like this a lot. I'm currently working on performance improvements in our codebase and deferring computation until it's needed is a technique I use often (it often helps to de-duplicate computation).

I will review this more thoroughly later today/tomorrow. Thanks for this!

Copy link
Contributor

@sherwinski sherwinski left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @stevehodgkiss, this seems like a straightforward boost to me 👍

@sherwinski
Copy link
Contributor

This branch is out of sync with main so I'm closing it in favor of #109 in order to unblock #108

@sherwinski sherwinski closed this Apr 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants