-
Notifications
You must be signed in to change notification settings - Fork 230
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rewrite Stack to be double stack #2138
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a few suggestions from my reading tonight. I'm not done, but I need to quit for the night. If you want to react to these and make further edits, feel free.
src/kOS.Safe/Execution/Stack.cs
Outdated
private int stackPointer = -1; | ||
private readonly object[] stack = new object[MAX_STACK_SIZE]; | ||
private int count = 0; | ||
private readonly object[] aboveStack = new object[MAX_STACK_SIZE]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should stop calling this "above".
If we're changing this into two different stacks, I think it we should also change the terminology.
This was called the "above" stack purely because of where it was located - above the other stack.
This terminology change should also affect all the other places the word "above" is used to refer to this stack, like PopAbove, PushAbove, aboveCount, etc. There may also be mentions of this in comments trying to explain the "above stack", which should change too.
Ideas for new term: scopeStack, or callStack.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: this will be a source of merge conflict for me: On the subject of creating the stack so it stays permanently at MAX_STACK_SIZE all the time - I had a change in my clock cycle branch designed to do this already but it wasn't ready to post it - I was keeping it as a stack (not an array) but telling .net to pre-allocate the stack to the full capacity. I will need to take care when updating that branch to develop because this will conflict for sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah yeah good point. i was just trying to keep things as similar as possible, but some clearer naming here would definitely make this more approachable in the future.
src/kOS.Safe/Execution/Stack.cs
Outdated
// inside it will always be backwardly printed): | ||
for (int index = stack.Count - 1; index >= 0; --index) | ||
// To simulate a single stack with the SP in the middle, we go through the aboveStack bottom up, then | ||
// the normal stack top down |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're changing the underlying implementation, I don't mind exposing that fact to users in the Dump()
output. This is meant to be a dump of what is really going on under the hood, so we should just print out the two stacks as two different stacks, labeled accordingly.
@@ -203,6 +238,28 @@ public string Dump() | |||
} | |||
} | |||
|
|||
private void dumpItem(int index, bool isSP, object item, StringBuilder builder) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like moving this out - it was getting nested a bit too deep.
A checklist of things I'm going to edit in my review - just to make sure I get all of them:
|
Stack operations had to shift everything in the above stack up and down on every push and pop. The above stack is usually small since it is just the context chain, but the extra shuffling adds up. This switches it to be two separate stacks (implemented as fixed-size arrays to avoid re-allocating), changing the
MoveStackPointer
intoPushAbove
andPopAbove
, as it was never used for transferring between the stacks, just for accessing the above stack.This reduces the runtime of my sort script from ~16 seconds to ~14.5 seconds.