Skip to content

Inspect directly into a RubyString and avoid StringBuilder/char[].#4128

Merged
enebo merged 2 commits intojruby:masterfrom
headius:lighter_inspect
Oct 12, 2016
Merged

Inspect directly into a RubyString and avoid StringBuilder/char[].#4128
enebo merged 2 commits intojruby:masterfrom
headius:lighter_inspect

Conversation

@headius
Copy link
Member

@headius headius commented Sep 1, 2016

This should reduce the overhead of inspecting by avoiding the
intermediate StringBuilder/char[] and subsequent conversion to
byte[]. There are a few extra objects created along the way
(java.lang.String.getBytes, RubyString for class name, etc) but
overall memory use should reduce around 3x: a US-ASCII
string would need 2x byte size in the StringBuilder, and the
builder is still alive when we create the eventual byte[] for a
total of 3x max memory use during inspect.

Relates to #4127, but does not constitute a fix for memory issues
there.

@headius
Copy link
Member Author

headius commented Sep 1, 2016

Inspect perf comparison.

Script:

obj = top = Object.new
100.times do
  obj2 = Object.new
  obj.instance_variable_set :@obj, obj2
  obj = obj2
end
loop do
  t = Time.now
  1000.times { top.inspect }
  puts Time.now - t
end

Before:

1.306
0.918
0.95
0.883
0.915
0.956
0.903
0.876
0.899
0.89

After:

0.339
0.18
0.184
0.205
0.148
0.155
0.195
0.153
0.149
0.198
0.153

@enebo enebo modified the milestones: JRuby 9.1.5.0, JRuby 9.1.6.0 Sep 7, 2016
This should reduce the overhead of inspecting by avoiding the
intermediate StringBuilder/char[] and subsequent conversion to
byte[]. There are a few extra objects created along the way
(java.lang.String.getBytes, RubyString for class name, etc) but
overall memory use should reduce around 3x: a US-ASCII
string would need 2x byte size in the StringBuilder, and the
builder is still alive when we create the eventual byte[] for a
total of 3x max memory use during inspect.

Relates to jruby#4127, but does not constitute a fix for memory issues
there.
@enebo enebo merged commit 256042b into jruby:master Oct 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants