Skip to content
Permalink
Browse files
No need to preserve BacktraceElement object in Binding.
Saves cloning it for every block and binding we instantiate!
  • Loading branch information
headius committed Jan 9, 2015
1 parent 2f39f3e commit 7e01a8653e59d34c76db7d13b6f9e7d4b37c696f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 28 deletions.
@@ -159,7 +159,9 @@ private void setup(Block procBlock) {
oldBinding.getFrame().duplicate(),
oldBinding.getVisibility(),
oldBinding.getDynamicScope(),
oldBinding.getBacktrace().clone());
oldBinding.getMethod(),
oldBinding.getFile(),
oldBinding.getLine());
block = new Block(procBlock.getBody(), newBinding);

// modify the block with a new backref/lastline-grabbing scope
@@ -47,7 +47,10 @@ public class Binding {
* frame of method which defined this block
*/
private final Frame frame;
private final BacktraceElement backtrace;

public String method;
public String filename;
public int line;

private Visibility visibility;
/**
@@ -75,36 +78,42 @@ public class Binding {
* instances.
*/
private Binding evalScopeBinding = this;

public Binding(IRubyObject self, Frame frame,
Visibility visibility, DynamicScope dynamicScope, BacktraceElement backtrace) {
Visibility visibility, DynamicScope dynamicScope, String method, String filename, int line) {
this.self = self;
this.frame = frame;
this.visibility = visibility;
this.dynamicScope = dynamicScope;
this.backtrace = backtrace;
this.method = method;
this.filename = filename;
this.line = line;
}

private Binding(IRubyObject self, Frame frame,
Visibility visibility, DynamicScope dynamicScope, BacktraceElement backtrace, DynamicScope dummyScope) {
Visibility visibility, DynamicScope dynamicScope, String method, String filename, int line, DynamicScope dummyScope) {
this.self = self;
this.frame = frame;
this.visibility = visibility;
this.dynamicScope = dynamicScope;
this.backtrace = backtrace;
this.method = method;
this.filename = filename;
this.line = line;
this.dummyScope = dummyScope;
}

public Binding(Frame frame, DynamicScope dynamicScope, BacktraceElement backtrace) {
public Binding(Frame frame, DynamicScope dynamicScope, String method, String filename, int line) {
this.self = frame.getSelf();
this.frame = frame;
this.visibility = frame.getVisibility();
this.dynamicScope = dynamicScope;
this.backtrace = backtrace;
this.method = method;
this.filename = filename;
this.line = line;
}

private Binding(Binding other) {
this(other.self, other.frame, other.visibility, other.dynamicScope, other.backtrace, other.dummyScope);
this(other.self, other.frame, other.visibility, other.dynamicScope, other.method, other.filename, other.line, other.dummyScope);
}

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

public BacktraceElement getBacktrace() {
return backtrace;
}

public String getFile() {
return backtrace.filename;
return filename;
}

public void setFile(String file) {
backtrace.filename = file;
public void setFile(String filename) {
this.filename = filename;
}

public int getLine() {
return backtrace.line;
return line;
}

public void setLine(int line) {
backtrace.line = line;
this.line = line;
}

public String getMethod() {
return backtrace.method;
return method;
}

public void setMethod(String method) {
backtrace.method = method;
this.method = method;
}

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

return evalScopeBinding.evalScope;
}

@Deprecated
public Binding(IRubyObject self, Frame frame,
Visibility visibility, DynamicScope dynamicScope, BacktraceElement backtrace) {
this.self = self;
this.frame = frame;
this.visibility = visibility;
this.dynamicScope = dynamicScope;
this.method = backtrace.method;
this.filename = backtrace.filename;
this.line = backtrace.line;
}

@Deprecated
public Binding(Frame frame, DynamicScope dynamicScope, BacktraceElement backtrace) {
this.self = frame.getSelf();
this.frame = frame;
this.visibility = frame.getVisibility();
this.dynamicScope = dynamicScope;
this.method = backtrace.method;
this.filename = backtrace.filename;
this.line = backtrace.line;
}

@Deprecated
public BacktraceElement getBacktrace() {
return new BacktraceElement(method, filename, line);
}
}
@@ -173,7 +173,13 @@ public Block cloneBlock() {
public Block cloneBlockAndFrame() {
Binding oldBinding = binding;
Binding binding = new Binding(
oldBinding.getSelf(), oldBinding.getFrame().duplicate(), oldBinding.getVisibility(), oldBinding.getDynamicScope(), oldBinding.getBacktrace());
oldBinding.getSelf(),
oldBinding.getFrame().duplicate(),
oldBinding.getVisibility(),
oldBinding.getDynamicScope(),
oldBinding.getMethod(),
oldBinding.getFile(),
oldBinding.getLine());

Block newBlock = new Block(body, binding);

@@ -60,7 +60,7 @@ public static Block createMethodBlock(ThreadContext context, IRubyObject self, D
Binding binding = new Binding(
frame,
dynamicScope,
new BacktraceElement(method.getMethodName(), body.getFile(), body.getLine()));
method.getMethodName(), body.getFile(), body.getLine());

return new Block(body, binding);
}
@@ -1004,7 +1004,8 @@ public void setWithinTrace(boolean isWithinTrace) {
*/
public Binding currentBinding() {
Frame frame = getCurrentFrame().capture();
return new Binding(frame, getCurrentScope(), backtrace[backtraceIndex].clone());
BacktraceElement elt = backtrace[backtraceIndex];
return new Binding(frame, getCurrentScope(), elt.getMethod(), elt.getFilename(), elt.getLine());
}

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

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

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

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

/**

0 comments on commit 7e01a86

Please sign in to comment.