Skip to content

Commit 7e01a86

Browse files
committed
No need to preserve BacktraceElement object in Binding.
Saves cloning it for every block and binding we instantiate!
1 parent 2f39f3e commit 7e01a86

File tree

5 files changed

+74
-28
lines changed

5 files changed

+74
-28
lines changed

core/src/main/java/org/jruby/RubyProc.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,9 @@ private void setup(Block procBlock) {
159159
oldBinding.getFrame().duplicate(),
160160
oldBinding.getVisibility(),
161161
oldBinding.getDynamicScope(),
162-
oldBinding.getBacktrace().clone());
162+
oldBinding.getMethod(),
163+
oldBinding.getFile(),
164+
oldBinding.getLine());
163165
block = new Block(procBlock.getBody(), newBinding);
164166

165167
// modify the block with a new backref/lastline-grabbing scope

core/src/main/java/org/jruby/runtime/Binding.java

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public class Binding {
4747
* frame of method which defined this block
4848
*/
4949
private final Frame frame;
50-
private final BacktraceElement backtrace;
50+
51+
public String method;
52+
public String filename;
53+
public int line;
5154

5255
private Visibility visibility;
5356
/**
@@ -75,36 +78,42 @@ public class Binding {
7578
* instances.
7679
*/
7780
private Binding evalScopeBinding = this;
78-
81+
7982
public Binding(IRubyObject self, Frame frame,
80-
Visibility visibility, DynamicScope dynamicScope, BacktraceElement backtrace) {
83+
Visibility visibility, DynamicScope dynamicScope, String method, String filename, int line) {
8184
this.self = self;
8285
this.frame = frame;
8386
this.visibility = visibility;
8487
this.dynamicScope = dynamicScope;
85-
this.backtrace = backtrace;
88+
this.method = method;
89+
this.filename = filename;
90+
this.line = line;
8691
}
8792

8893
private Binding(IRubyObject self, Frame frame,
89-
Visibility visibility, DynamicScope dynamicScope, BacktraceElement backtrace, DynamicScope dummyScope) {
94+
Visibility visibility, DynamicScope dynamicScope, String method, String filename, int line, DynamicScope dummyScope) {
9095
this.self = self;
9196
this.frame = frame;
9297
this.visibility = visibility;
9398
this.dynamicScope = dynamicScope;
94-
this.backtrace = backtrace;
99+
this.method = method;
100+
this.filename = filename;
101+
this.line = line;
95102
this.dummyScope = dummyScope;
96103
}
97104

98-
public Binding(Frame frame, DynamicScope dynamicScope, BacktraceElement backtrace) {
105+
public Binding(Frame frame, DynamicScope dynamicScope, String method, String filename, int line) {
99106
this.self = frame.getSelf();
100107
this.frame = frame;
101108
this.visibility = frame.getVisibility();
102109
this.dynamicScope = dynamicScope;
103-
this.backtrace = backtrace;
110+
this.method = method;
111+
this.filename = filename;
112+
this.line = line;
104113
}
105114

106115
private Binding(Binding other) {
107-
this(other.self, other.frame, other.visibility, other.dynamicScope, other.backtrace, other.dummyScope);
116+
this(other.self, other.frame, other.visibility, other.dynamicScope, other.method, other.filename, other.line, other.dummyScope);
108117
}
109118

110119
/**
@@ -173,32 +182,28 @@ public Frame getFrame() {
173182
return frame;
174183
}
175184

176-
public BacktraceElement getBacktrace() {
177-
return backtrace;
178-
}
179-
180185
public String getFile() {
181-
return backtrace.filename;
186+
return filename;
182187
}
183188

184-
public void setFile(String file) {
185-
backtrace.filename = file;
189+
public void setFile(String filename) {
190+
this.filename = filename;
186191
}
187192

188193
public int getLine() {
189-
return backtrace.line;
194+
return line;
190195
}
191196

192197
public void setLine(int line) {
193-
backtrace.line = line;
198+
this.line = line;
194199
}
195200

196201
public String getMethod() {
197-
return backtrace.method;
202+
return method;
198203
}
199204

200205
public void setMethod(String method) {
201-
backtrace.method = method;
206+
this.method = method;
202207
}
203208

204209
public boolean equals(Object other) {
@@ -235,4 +240,32 @@ public final DynamicScope getEvalScope(Ruby runtime) {
235240

236241
return evalScopeBinding.evalScope;
237242
}
243+
244+
@Deprecated
245+
public Binding(IRubyObject self, Frame frame,
246+
Visibility visibility, DynamicScope dynamicScope, BacktraceElement backtrace) {
247+
this.self = self;
248+
this.frame = frame;
249+
this.visibility = visibility;
250+
this.dynamicScope = dynamicScope;
251+
this.method = backtrace.method;
252+
this.filename = backtrace.filename;
253+
this.line = backtrace.line;
254+
}
255+
256+
@Deprecated
257+
public Binding(Frame frame, DynamicScope dynamicScope, BacktraceElement backtrace) {
258+
this.self = frame.getSelf();
259+
this.frame = frame;
260+
this.visibility = frame.getVisibility();
261+
this.dynamicScope = dynamicScope;
262+
this.method = backtrace.method;
263+
this.filename = backtrace.filename;
264+
this.line = backtrace.line;
265+
}
266+
267+
@Deprecated
268+
public BacktraceElement getBacktrace() {
269+
return new BacktraceElement(method, filename, line);
270+
}
238271
}

core/src/main/java/org/jruby/runtime/Block.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,13 @@ public Block cloneBlock() {
173173
public Block cloneBlockAndFrame() {
174174
Binding oldBinding = binding;
175175
Binding binding = new Binding(
176-
oldBinding.getSelf(), oldBinding.getFrame().duplicate(), oldBinding.getVisibility(), oldBinding.getDynamicScope(), oldBinding.getBacktrace());
176+
oldBinding.getSelf(),
177+
oldBinding.getFrame().duplicate(),
178+
oldBinding.getVisibility(),
179+
oldBinding.getDynamicScope(),
180+
oldBinding.getMethod(),
181+
oldBinding.getFile(),
182+
oldBinding.getLine());
177183

178184
Block newBlock = new Block(body, binding);
179185

core/src/main/java/org/jruby/runtime/MethodBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static Block createMethodBlock(ThreadContext context, IRubyObject self, D
6060
Binding binding = new Binding(
6161
frame,
6262
dynamicScope,
63-
new BacktraceElement(method.getMethodName(), body.getFile(), body.getLine()));
63+
method.getMethodName(), body.getFile(), body.getLine());
6464

6565
return new Block(body, binding);
6666
}

core/src/main/java/org/jruby/runtime/ThreadContext.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,7 +1004,8 @@ public void setWithinTrace(boolean isWithinTrace) {
10041004
*/
10051005
public Binding currentBinding() {
10061006
Frame frame = getCurrentFrame().capture();
1007-
return new Binding(frame, getCurrentScope(), backtrace[backtraceIndex].clone());
1007+
BacktraceElement elt = backtrace[backtraceIndex];
1008+
return new Binding(frame, getCurrentScope(), elt.getMethod(), elt.getFilename(), elt.getLine());
10081009
}
10091010

10101011
/**
@@ -1014,7 +1015,8 @@ public Binding currentBinding() {
10141015
*/
10151016
public Binding currentBinding(IRubyObject self) {
10161017
Frame frame = getCurrentFrame().capture();
1017-
return new Binding(self, frame, frame.getVisibility(), getCurrentScope(), backtrace[backtraceIndex].clone());
1018+
BacktraceElement elt = backtrace[backtraceIndex];
1019+
return new Binding(self, frame, frame.getVisibility(), getCurrentScope(), elt.getMethod(), elt.getFilename(), elt.getLine());
10181020
}
10191021

10201022
/**
@@ -1026,7 +1028,8 @@ public Binding currentBinding(IRubyObject self) {
10261028
*/
10271029
public Binding currentBinding(IRubyObject self, Visibility visibility) {
10281030
Frame frame = getCurrentFrame().capture();
1029-
return new Binding(self, frame, visibility, getCurrentScope(), backtrace[backtraceIndex].clone());
1031+
BacktraceElement elt = backtrace[backtraceIndex];
1032+
return new Binding(self, frame, visibility, getCurrentScope(), elt.getMethod(), elt.getFilename(), elt.getLine());
10301033
}
10311034

10321035
/**
@@ -1038,7 +1041,8 @@ public Binding currentBinding(IRubyObject self, Visibility visibility) {
10381041
*/
10391042
public Binding currentBinding(IRubyObject self, DynamicScope scope) {
10401043
Frame frame = getCurrentFrame().capture();
1041-
return new Binding(self, frame, frame.getVisibility(), scope, backtrace[backtraceIndex].clone());
1044+
BacktraceElement elt = backtrace[backtraceIndex];
1045+
return new Binding(self, frame, frame.getVisibility(), scope, elt.getMethod(), elt.getFilename(), elt.getLine());
10421046
}
10431047

10441048
/**
@@ -1053,7 +1057,8 @@ public Binding currentBinding(IRubyObject self, DynamicScope scope) {
10531057
*/
10541058
public Binding currentBinding(IRubyObject self, Visibility visibility, DynamicScope scope) {
10551059
Frame frame = getCurrentFrame().capture();
1056-
return new Binding(self, frame, visibility, scope, backtrace[backtraceIndex].clone());
1060+
BacktraceElement elt = backtrace[backtraceIndex];
1061+
return new Binding(self, frame, visibility, scope, elt.getMethod(), elt.getFilename(), elt.getLine());
10571062
}
10581063

10591064
/**

0 commit comments

Comments
 (0)