Skip to content
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

change unnecessary heap allocs to stack allocs #24

Merged
merged 1 commit into from
Jul 25, 2015

Conversation

rweichler
Copy link
Contributor

should help with overhead.

@rweichler rweichler changed the title make temporary buffers on the stack instead of the heap change unnecessary heap allocs to stack allocs Jul 11, 2015
@ephemer
Copy link

ephemer commented Jul 24, 2015

@rweichler can you explain what difference this makes? I'm interested in reducing processing overhead as much as possible but coming from the scripting side of things I'm not as up to speed with memory management as I'd like to be.

@rweichler
Copy link
Contributor Author

@ephemer Sure. The basic idea is that in most modern operating systems, there's 2 places to allocate memory. On the stack and on the heap. Stack is temporary stuff and heap is more permanent stuff.

The stack basically works like this:

int x;
if(true) {
    int y;
    if(true) {
        int z;
    }
}
some_function(x);

So what the OS does is this:

<nothing>
x
x, y
x, y, z
x, y
x
x, some_function, x
<run some_function here>
x
<nothing>

Since all of that static scoping stuff can be figured out by the compiler it ends up being way more efficient since you're just pushing and popping stuff from a stack.

On the contrary, with malloc and the like (like all of the Objective-C objects), theres no way for the compiler to tell when the stuff will go away, so it ends up putting the stuff in this heap, which is pretty much a huge chunk of memory with random parts being used and not used. A lot of the time there's fragmentation and stuff which ends up slowing stuff down since it's not all contiguous (kind of like how you have to defrag Windows every once in a while)

So back to my changes, the thing I noticed is that he freed the pointers as soon as the function exited, so there was no need to put it in the heap since they were going to go away as soon as the function ended anyway.

@bartolsthoorn
Copy link
Owner

Thank you for this and sorry for the late reply, I was travelling in Canada
for a while. I expect to merge this one tomorrow when I am back home again.
On Jul 24, 2015 3:03 PM, "Reed Weichler" notifications@github.com wrote:

Sure. The basic idea is that in most modern operating systems, there's 2
places to allocate memory. On the stack and on the heap. Stack is temporary
stuff and heap is more permanent stuff.

The stack basically works like this:

int x;if(true) {
int y;
if(true) {
int z;
}
}some_function(x);

So what the OS does is this:

x x, y x, y, z x, y x x, some_function, x x

Since all of that static scoping stuff can be figured out by the compiler
it ends up being way more efficient since you're just pushing and popping
stuff from a stack.

On the contrary, with malloc and the like (like all of the Objective-C
objects), theres no way for the compiler to tell when the stuff will go
away, so it ends up putting the stuff in this heap, which is pretty much a
huge chunk of memory with random parts being used and not used. A lot of
the time there's fragmentation and stuff which ends up slowing stuff down
since it's not all contiguous (kind of like how you have to defrag Windows
every once in a while)


Reply to this email directly or view it on GitHub
#24 (comment).

@ephemer
Copy link

ephemer commented Jul 25, 2015

@rweichler thanks for the awesome explanation, that makes sense in terms of the function call stack in JavaScript, for example. Awesome stuff

bartolsthoorn added a commit that referenced this pull request Jul 25, 2015
Change unnecessary heap allocs to stack allocs
@bartolsthoorn bartolsthoorn merged commit b074bbd into bartolsthoorn:master Jul 25, 2015
@ephemer
Copy link

ephemer commented Aug 25, 2015

@rweichler @bartolsthoorn Just implemented this commit for a filterbank we made in NVDSP and got about a 15% reduction in CPU usage. Great stuff.

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.

None yet

3 participants