diff options
author | Shawn O. Pearce <sop@google.com> | 2011-06-01 16:52:54 -0700 |
---|---|---|
committer | Shawn O. Pearce <sop@google.com> | 2011-06-02 07:48:01 -0700 |
commit | 132745252cb4507317d25ea81a371f69a293ed56 (patch) | |
tree | 3a9186ddfc91e9b9be83f292a94436a721d1832c | |
parent | 7a577d848527ddd30f6d7d6681871e8c54b2de39 (diff) | |
download | prolog-cafe-132745252cb4507317d25ea81a371f69a293ed56.tar.gz |
Micro-optimize CPFStack access
Avoid doing multiple array index operations to access the top of
the stack element. Instead hold onto the top of the stack in a
local variable and access its fields directly as required.
-rw-r--r-- | src/lang/CPFStack.java | 18 | ||||
-rw-r--r-- | src/lang/Prolog.java | 31 |
2 files changed, 18 insertions, 31 deletions
diff --git a/src/lang/CPFStack.java b/src/lang/CPFStack.java index a5a7b95..d2f4e94 100644 --- a/src/lang/CPFStack.java +++ b/src/lang/CPFStack.java @@ -315,28 +315,12 @@ public class CPFStack implements Serializable { */ public int max() { return maxContents; } + CPFEntry topEntry() { return buffer[top]; } void restore() { buffer[top].restore(engine); } /** Returns the <em>time stamp</em> of current choice point frame. */ public long getTimeStamp() { return buffer[top].timeStamp; } - /** Sets the <em>time stamp</em> of current choice point frame. */ - public void setTimeStamp(long t) { buffer[top].timeStamp = t; } - /** Returns the <em>next clause</em> of current choice point frame. */ - public Operation getBP() { return buffer[top].bp; } - /** Sets the <em>next clause</em> of current choice point frame. */ - public void setBP(Operation p) { buffer[top].bp = p; } - - /** Returns the <em>trail pointer</em> of current choice point frame. */ - public int getTR() { return buffer[top].tr; } - /** Sets the <em>trail pointer</em> of current choice point frame. */ - public void setTR(int i) { buffer[top].tr = i; } - - /** Returns the <em>cut point</em> of current choice point frame. */ - public int getB0() { return buffer[top].b0; } - /** Sets the <em>cut point</em> of current choice point frame. */ - public void setB0(int i) { buffer[top].b0 = i; } - /** Shows the contents of this <code>CPFStack</code>. */ public void show() { if (empty()) { diff --git a/src/lang/Prolog.java b/src/lang/Prolog.java index d84faad..5a6f35a 100644 --- a/src/lang/Prolog.java +++ b/src/lang/Prolog.java @@ -168,11 +168,12 @@ public class Prolog implements Serializable { CPFTimeStamp = Long.MIN_VALUE; // Creates an initial choice point frame. - stack.push(CPFEntry.S0(null)); - stack.setTR(trail.top()); - stack.setTimeStamp(++CPFTimeStamp); - stack.setBP(Failure.FAILURE); - stack.setB0(B0); + CPFEntry initialFrame = CPFEntry.S0(null); + initialFrame.b0 = B0; + initialFrame.bp = Failure.FAILURE; + initialFrame.tr = trail.top(); + initialFrame.timeStamp = ++CPFTimeStamp; + stack.push(initialFrame); exceptionRaised = 0; @@ -256,8 +257,9 @@ public class Prolog implements Serializable { * and returns the backtrak point in current choice point. */ public Operation fail() { - B0 = stack.getB0(); // restore B0 - return stack.getBP(); // execute next clause + CPFEntry top = stack.topEntry(); + B0 = top.b0; // restore B0 + return top.bp; // execute next clause } /** @@ -345,11 +347,11 @@ public class Prolog implements Serializable { } private Operation finishjtry(Operation p, Operation next, CPFEntry entry) { + entry.b0 = B0; + entry.bp = next; + entry.tr = trail.top(); + entry.timeStamp = ++CPFTimeStamp; stack.push(entry); - stack.setTR(trail.top()); - stack.setTimeStamp(++CPFTimeStamp); - stack.setBP(next); - stack.setB0(B0); return p; } @@ -360,8 +362,9 @@ public class Prolog implements Serializable { */ public Operation retry(Operation p, Operation next) { restore(); - trail.unwind(stack.getTR()); - stack.setBP(next); + CPFEntry top = stack.topEntry(); + trail.unwind(top.tr); + top.bp = next; return p; } @@ -371,7 +374,7 @@ public class Prolog implements Serializable { */ public Operation trust(Operation p) { restore(); - trail.unwind(stack.getTR()); + trail.unwind(stack.topEntry().tr); stack.delete(); return p; } |