Optimize SimpleTextBKDWriter.rotateToTree tree-layout computation to O(1) per node#16354
Open
rajat315315 wants to merge 3 commits into
Open
Optimize SimpleTextBKDWriter.rotateToTree tree-layout computation to O(1) per node#16354rajat315315 wants to merge 3 commits into
rajat315315 wants to merge 3 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes: #16353
Description
This PR optimizes the
rotateToTreemethod inSimpleTextBKDWriterto avoid iterative loop overhead when calculating binary tree partitions.rotateToTreeconverts a sorted, flat list of leaf block split/start values into a flat array structure representing a balanced complete binary search tree.The Problem / Bottleneck
To partition nodes for the left subtree (
leftHalf) at each level of recursion, the method previously used an iterativewhile(true)loop to calculate the level-by-level boundaries. This loop ran in O(log(count)) time at each step of the recursion. Since recursion occurs for every node in the tree, the overall mathematical partition computation overhead was O(N log N) for N inner nodes.The Optimization
while(true)loop with a bitwise constant-time O(1) calculation to determine the exact number of nodes in the left subtree of a complete binary tree:This leverages the fact that complete binary trees have a mathematically rigid structure that can be derived in constant time using bit-shifts.
Unified Array Copying: We consolidated the System.arraycopy code to handle both leaf and internal nodes under a single, unified execution path, reducing code duplication and branch prediction overhead.
Dead Code Cleanup: Removed legacy debug output and outdated comments.