Closed technicalpickles closed 1 year ago
Snuck another small improvement: Use SHA256 with is marginally faster than SHA1
Benchmark:
require 'benchmark/ips'
require 'digest'
def refactored
query_caller = caller
location_key = Digest::SHA1.hexdigest(query_caller.join)
query_caller
end
def refactored_sha2
query_caller = caller
location_key = Digest::SHA256.hexdigest(query_caller.join)
query_caller
end
Benchmark.ips do |x|
x.config warmup: 0, times: 100
x.report("refactored") { refactored }
x.report("refactored_sha2") { refactored_sha2 }
x.compare!
end
Results:
Calculating -------------------------------------
refactored 272.508k (±16.2%) i/s - 1.157M in 4.794913s
refactored_sha2 279.210k (±16.6%) i/s - 1.184M in 4.793971s
Comparison:
refactored_sha2: 279210.1 i/s
refactored: 272507.7 i/s - same-ish: difference falls within error
Nice perf improvements @technicalpickles, thanks!
I was memory profiling a particularly slow system spec, and these two lines popped up on my radar. It's calling
caller
twice, and then duplicating one of them.caller
is pretty well understood as being pretty slow, so calling it only once is an Improve.The second call was using
dup
. This should not be necessary, ascaller
returns a new object anyways. Even when callingcaller
once, we don't do any manipulation of it, so it's safe to remove thedup
In my specific spec, I saw an improvement from 2m18s to 1m58s. I also did a micro benchmark showing the difference:
Which gives us: