Skip to content

Commit

Permalink
perf: avoid calculating path_and_params twice
Browse files Browse the repository at this point in the history
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
```
  • Loading branch information
stevehodgkiss authored and sherwinski committed Apr 8, 2021
1 parent 4b00c4b commit 923618c
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions lib/imgix/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ def to_url(opts = {})
prev_options = @options.dup
@options.merge!(opts)

url = @prefix + path_and_params
current_path_and_params = path_and_params
url = @prefix + current_path_and_params

if @secure_url_token
url += (has_query? ? "&" : "?") + "s=#{signature}"
url += (has_query? ? "&" : "?") + "s=#{signature(current_path_and_params)}"
end

@options = prev_options
Expand Down Expand Up @@ -129,8 +130,8 @@ def method_missing(method, *args, &block)

private

def signature
Digest::MD5.hexdigest(@secure_url_token + path_and_params)
def signature(current_path_and_params)
Digest::MD5.hexdigest(@secure_url_token + current_path_and_params)
end

def path_and_params
Expand Down

0 comments on commit 923618c

Please sign in to comment.