Skip to content

Add stack size calculation to code object to pre-allocate value stack - #1327

Closed
windelbouwman wants to merge 5 commits into
mainfrom
stack-size-calculation
Closed

windelbouwman wants to merge 5 commits into
mainfrom
stack-size-calculation

Conversation

@windelbouwman

Copy link
Copy Markdown
Contributor

This is still early work. Comments / ideas are welcome!

Idea is to determine the worst case stack size, and prevent a re-allocation of the value stack. Minor performance win, a lot of work :).

cthulahoops
cthulahoops previously approved these changes Aug 29, 2019

@cthulahoops cthulahoops left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clicked approve in the wrong window, does commenting undo that?

@cthulahoops

Copy link
Copy Markdown
Collaborator

This seems like it's going to add a bunch of code, for a possible speed up. Is it justified?

@youknowone

Copy link
Copy Markdown
Member

@cthulahoops Looking for this one?

@coolreader18 coolreader18 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally like this. CPython does it, it's more efficient, and it's really not all that much more code; it's essentially just a lookup table, both in the source code and the binary.

Comment thread vm/src/frame.rs Outdated
blocks: RefCell<Vec<Block>>, // Block frames, for controlling loops and exceptions
pub scope: Scope, // Variables
pub lasti: RefCell<usize>, // index of last instruction ran
lasti: RefCell<usize>, // index of last instruction ran

@coolreader18 coolreader18 Sep 9, 2019 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're going to touch this, do you want to make it a Cell<usize>? It really doesn't need to be a RefCell.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

Comment thread vm/src/frame.rs Outdated

pub fn push_value(&self, obj: PyObjectRef) {
self.stack.borrow_mut().push(obj);
// println!("Stack size: {}, capa: {}", self.stack.borrow().len(), self.stack.borrow().capacity());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// println!("Stack size: {}, capa: {}", self.stack.borrow().len(), self.stack.borrow().capacity());

self.current_stack_size += effect;
if self.current_stack_size > self.max_stack {
self.max_stack = self.current_stack_size;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could get rid of the properties on CodeObjectStream and just mutate max_stack_size on CodeObject:

let max_stack_size = &mut self.code.max_stack_size;
*max_stack_size = std::cmp::max(*max_stack_size, *max_stack_size + effect);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand. The thing is, I would like to gather all instructions in the stream, and in the end, create the immutable codeobject. This makes the code easier to understand in my opinion. That is the reason why I implemented the code like it is.

@windelbouwman

Copy link
Copy Markdown
Contributor Author

This seems like it's going to add a bunch of code, for a possible speed up. Is it justified?

This change is not justified, but it is a copy of an idea from cpython and micropython.

@windelbouwman windelbouwman changed the title [WIP] Add stack size calculation to code object to pre-allocate value stack Add stack size calculation to code object to pre-allocate value stack Sep 28, 2019
@ep12 ep12 mentioned this pull request Apr 3, 2020
6 of 8 tasks
@coolreader18

Copy link
Copy Markdown
Member

This has been implemented for about a year now iirc, I just never closed this PR.

@coolreader18
coolreader18 deleted the stack-size-calculation branch October 26, 2021 20:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants