Skip to content

Use Java 9 stack walking to reduce the cost of #caller and friends#5500

Merged
headius merged 2 commits intojruby:masterfrom
headius:lazy_stack_trace
Dec 8, 2018
Merged

Use Java 9 stack walking to reduce the cost of #caller and friends#5500
headius merged 2 commits intojruby:masterfrom
headius:lazy_stack_trace

Conversation

@headius
Copy link
Member

@headius headius commented Dec 6, 2018

This PR modifies our logic for building stack traces to be streamable and to use the Java 9 StackWalker API to reduce the load of partial walks.

This is a work in progress.

@headius headius added this to the JRuby 9.2.6.0 milestone Dec 6, 2018
In preparation for using the StackWalker API on Java 9+ this patch
streamifies both the native Java stack elements and the frames
from JRuby's interpreter. The former case will allow us to use
the StackWalker API to produce those frames lazily, drastically
reducing the cost of short stack walks on large stacks. The latter
eliminates the copying of the interpreter stack frames and just
streams them directly from ThreadContext.
This improves the performance of limited-depth Kernel#caller by
a substantial amount, dependent on the depth of the stack. For the
following benchmark with a 500-deep stack, the improvement is well
over 10x.

bench_caller_single.rb
```ruby
def foo(depth)
  if depth > 0
    foo(depth-1)
  else
    caller(1, 1)
  end
end

loop {
  t = Time.now
  i = 0
  while i < 10000
    i+=1
    foo(500)
  end
  puts Time.now - t
}
```

Results on 8 and 9:
```
[] ~/projects/jruby $ pickjdk 5 ; jruby blah.rb
New JDK: jdk1.8.0_181.jdk
6.725401
6.5270790000000005
6.555323
^C
[] ~/projects/jruby $ pickjdk 4 ; jruby blah.rb
New JDK: jdk-9.0.4.jdk
0.765762
0.5783969999999999
0.444722
0.442188
0.439658
0.446652
^C
```
@headius headius merged commit 2a26d30 into jruby:master Dec 8, 2018
@headius headius deleted the lazy_stack_trace branch December 8, 2018 02:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant