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

fix: Small perf improvement to generate_r #1522

Merged
merged 1 commit into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if you are aware but offset was added in 3.1, and IIRC otel is supposed to be compatible with Ruby 3.0?

But since the offset is static here, you can use the @ syntax:

>> "abcedfghsdfdsfhdsfjsdkjfdsjfkdlsfjdsfjksdfk".unpack1('Q>', offset: 8)
=> 8314883393651632228
>> "abcedfghsdfdsfhdsfjsdkjfdsjfkdlsfjdsfjksdfk".unpack1('@8Q>')
=> 8314883393651632228

offset was added to avoid allocating a new pattern string every time when the offset is dynamic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if you are aware but offset was added in 3.1, and IIRC otel is supposed to be compatible with Ruby 3.0?

But since the offset is static here, you can use the @ syntax:

>> "abcedfghsdfdsfhdsfjsdkjfdsjfkdlsfjdsfjksdfk".unpack1('Q>', offset: 8)
=> 8314883393651632228
>> "abcedfghsdfdsfhdsfjsdkjfdsjfkdlsfjdsfjksdfk".unpack1('@8Q>')
=> 8314883393651632228

offset was added to avoid allocating a new pattern string every time when the offset is dynamic.

64 - x.bit_length
end
end
Expand Down
Loading