Skip to content

Conversation

@lucabart97
Copy link
Contributor

@lucabart97 lucabart97 commented Oct 23, 2025

Instead of separately confirming if both children are nullptr, compare the child pointers' values directly. This technique is valid because, in a properly structured search tree, the two child pointers will only be equal if both are nullptr.

This change reduces the operation from three comparisons to just one.

@jlblancoc
Copy link
Owner

@lucabart97 wow, that's smart.
Probably just ripping a nanosecond per iteration... Or maybe not even that because at the microarchitecture level all CPUs have a special op to check for a register to be zero, so it should be cheaper than a A==B comparison.

Anyways, I'm triggering the CI tests, if they all pass, I'm glad to merge. Before, please add a small comment to those lines to make clear to future readers that it's actually checking for nullptr.

Thanks! 🙏

Instead of separately confirming if both children are nullptr,
compare the child pointers' values directly. This technique is valid
because, in a properly structured search tree, the two child
pointers will only be equal if both are nullptr.

This change reduces the operation from three comparisons to just one.

Signed-off-by: Luca Bartoli <[email protected]>
@lucabart97 lucabart97 force-pushed the nanoflann_faster_searchLevel branch from 425fc75 to 2e27210 Compare October 26, 2025 13:31
@lucabart97
Copy link
Contributor Author

Ok, done✈️

@jlblancoc jlblancoc merged commit 81cd02b into jlblancoc:master Oct 26, 2025
6 checks passed
@qq422216549
Copy link

case 1:child1==nullptr child2==nullptr
case 2:child1==nullptr child2 != nullptr
case 3:child1 != nullptr child2==nullptr
case 4:child1 != nullptr child2 != nullptr
Actually case2 and case3 Will not appear
so just compare child1 is Enough

if(node->child1==nullptr)
{
......
}

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.

3 participants