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

Improve DynamicBVH code to make it clearer how the stack/heap works #86059

Merged
merged 1 commit into from
Dec 18, 2023

Conversation

alesliehughes
Copy link
Contributor

...
if (aux_stack.is_empty())
memcpy(aux_stack.ptr(), stack, ALLOCA_STACK_SIZE * sizeof(const Node *));
...
stack = aux_stack.ptr();

There might be a possibility that "aux_stack.ptr()" and "stack" will overlap. Using memmove will ensure in this case it's handled correctly.

@alesliehughes alesliehughes requested a review from a team as a code owner December 12, 2023 02:12
@RandomShaper
Copy link
Member

Looks to me like once stack = aux_stack.ptr(); happens the first time, the positive branch of the if won't be entered again. In other words, the positive branch is where the code switches from the "stack stack" to the heap stack, and from that point onwards the code won't copy again. (Resizing the LocalVector will implicitly copy to a new memory block if needed, which by the way is the reason the stack pointer has to be reassigned.)

If I'm right, a change that would communicate all that more clrearly would be to keep the original stack pointer as alloca_stack, then assign stack to it and replace the memcpy with the following:

memcpy(aux_stack.ptr(), alloca_stack, ...);
alloca_stack = nullptr;

@lawnjelly
Copy link
Member

There might be a possibility that "aux_stack.ptr()" and "stack" will overlap. Using memmove will ensure in this case it's handled correctly.

Agree with @RandomShaper you seem to be misunderstanding how the code works. The alloca stack the OS stack, and the aux stack is actually the heap.

@alesliehughes
Copy link
Contributor Author

There might be a possibility that "aux_stack.ptr()" and "stack" will overlap. Using memmove will ensure in this case it's handled correctly.

Agree with @RandomShaper you seem to be misunderstanding how the code works. The alloca stack the OS stack, and the aux stack is actually the heap.

This is true. I'm still new to the godot code base. I'll adjust the patch as suggested.

I'm just trying to fix Coverity issues that make sense in the short term.

@RandomShaper
Copy link
Member

This is true. I'm still new to the godot code base. I'll adjust the patch as suggested.

I'm just trying to fix Coverity issues that make sense in the short term.

No worries. Besides, I'd advise you to get more familiar with the codebase until the point where you start feeling comfortable catching and fixing issues yourself. Automated tools only get you so far (for now!) and can even hinder your self-training process.

Inspired by a Coverity issue about possible memcpy usage and overlapping memory.
@alesliehughes
Copy link
Contributor Author

Updated and tested to the best I can.

Advise on downloadable projects that would make sure I didn't break anything?

Copy link
Member

@RandomShaper RandomShaper left a comment

Choose a reason for hiding this comment

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

LGTM. I think this makes the intent of the code more explicit. Aside, I'm pretty sure the compiler will be good at removing the superfluous (to the machine) stuff.

@akien-mga akien-mga requested a review from lawnjelly December 13, 2023 09:22
@akien-mga akien-mga modified the milestones: 4.x, 4.3 Dec 13, 2023
@lawnjelly
Copy link
Member

Looks fine as far as I can see for functionality.

Whether it improves readability I can't say, but then I'm quite used to this pattern (stack then heap) so am not an unbiased observer.

@YuriSizov YuriSizov changed the title Use memmove in DynamicBVH where memory might overlap Improve DynamicBVH code to make it clearer how the stack/heap works Dec 18, 2023
@YuriSizov YuriSizov merged commit 639f452 into godotengine:master Dec 18, 2023
15 checks passed
@YuriSizov
Copy link
Contributor

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants