jhawthorn / vernier

📏 next generation CRuby profiler
https://vernier.prof/
MIT License
718 stars 15 forks source link

Expose StackTable to Ruby #62

Closed jhawthorn closed 2 months ago

jhawthorn commented 3 months ago

StackTable exposes Vernier's (previously internal) method of collecting stacks and frames without making Ruby allocations and returns a single integer representing a unique stack.

stack_table = Vernier::StackTable.new
stack_table.current_stack #=> 12
stack_table.current_stack #=> 13

This can later be decoded

stack_table.backtrace(12)
#=> ["test.rb:5:in 'foo'",
#    "test.rb:9:in 'bar'"]

The first use of this can be collecting stack indexes which are compatible with the output of Vernier's collectors (ex. to add them to markers). The second is performance.

This is 2-3x faster than using caller_locations, however the biggest gains would be when additional processing is done on the stack in Ruby (ex. filtering or reformatting) which can be avoided because stacks and frames are de-duplicated.

ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-linux]
Warming up --------------------------------------
              caller    28.485k i/100ms
    caller_locations   110.542k i/100ms
stack_table.current_stack
                       303.853k i/100ms
Calculating -------------------------------------
              caller    283.979k (± 0.3%) i/s -      1.424M in   5.015382s
    caller_locations      1.128M (± 0.2%) i/s -      5.748M in   5.094051s
stack_table.current_stack
                          3.064M (± 0.2%) i/s -     15.497M in   5.057551s