Skip to content

Commit

Permalink
fix: Small perf improvement to generate_r (#1522)
Browse files Browse the repository at this point in the history
  • Loading branch information
fbogsany authored Sep 14, 2023
1 parent cf8a18b commit 37e225a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
17 changes: 17 additions & 0 deletions api/benchmarks/id_generation_bench.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ def generate_span_id_while
id
end

def generate_r(trace_id)
x = trace_id[8, 8].unpack1('Q>') | 0x3
64 - x.bit_length
end

def generate_r_in_place(trace_id)
x = trace_id.unpack1('Q>', offset: 8) | 0x3
64 - x.bit_length
end

Benchmark.ipsa do |x|
x.report('generate_trace_id') { generate_trace_id }
x.report('generate_trace_id_while') { generate_trace_id_while }
Expand All @@ -46,3 +56,10 @@ def generate_span_id_while
x.report('generate_span_id_while') { generate_span_id_while }
x.compare!
end

Benchmark.ipsa do |x|
trace_id = generate_trace_id
x.report('generate_r') { generate_r(trace_id) }
x.report('generate_r_in_place') { generate_r_in_place(trace_id) }
x.compare!
end
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def decimal(str)
end

def generate_r(trace_id)
x = trace_id[8, 8].unpack1('Q>') | 0x3
x = trace_id.unpack1('Q>', offset: 8) | 0x3
64 - x.bit_length
end
end
Expand Down

0 comments on commit 37e225a

Please sign in to comment.