Skip to content

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

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

Closed
wants to merge 5 commits into from

Conversation

windelbouwman
Copy link
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
Copy link
Collaborator

@cthulahoops cthulahoops left a comment

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
Collaborator

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

@youknowone
Copy link
Member

@cthulahoops Looking for this one?

Copy link
Member

@coolreader18 coolreader18 left a comment

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.

vm/src/frame.rs Outdated
@@ -85,7 +85,7 @@ pub struct Frame {
stack: RefCell<Vec<PyObjectRef>>, // The main data frame of the stack machine
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
Copy link
Member

@coolreader18 coolreader18 Sep 9, 2019

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
Contributor Author

Choose a reason for hiding this comment

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

Agreed!

vm/src/frame.rs Outdated
@@ -1244,6 +1261,7 @@ impl Frame {

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
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
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
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
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
8 tasks
@coolreader18
Copy link
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