[Repo Assist] perf: use bottom-up construction in Heap.ofSeq, extract mergeData helper#242
Draft
github-actions[bot] wants to merge 2 commits intomasterfrom
Draft
Conversation
The previous ofSeq implementation built the heap by sequential left-to-right merges. For sorted input (ascending for a min-heap), every new element was appended as a direct child of the root, producing a root with O(n) children after n insertions. The first Tail() call then had to merge all those children, costing O(n) instead of the amortised O(log n) expected by users (see #166). New approach: build n singleton heaps from the input array and repeatedly merge adjacent pairs (bottom-up construction). Each pass halves the number of heaps; log₂(n) passes produce a balanced heap whose height is O(log n). The first Tail() on such a heap costs O(log n) regardless of the input ordering. Also refactors the inline mergeData function (previously duplicated inside Tail()) into a private static helper Heap.mergeData, used by both Tail() and the new ofSeq, and simplifies the static merge wrapper. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
22 tasks
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.
🤖 This PR was created by Repo Assist, an automated AI assistant.
Addresses the performance issue described in #166 ("Heap.Tail slow").
Root cause
The previous
ofSeqbuilt the heap by a sequential left-to-right fold, treating each new element as a merge with the current root. For sorted ascending input on a min-heap, every new element is larger than the root, so it is prepended as a direct child of the root. After inserting n elements this creates:Format check (
dotnet fantomas --check) passes with no changes needed.Closes #166