Repository navigation
perf: ArrayDeque.clear() from O(n) to O(1) with GC-safe nulling - #26288
Merged
Merged
Conversation
He-Pin
marked this pull request as ready for review
June 8, 2026 17:32
SolalPirelli
requested changes
Jun 9, 2026
SolalPirelli
left a comment
Contributor
There was a problem hiding this comment.
I think this is reasonable for an array-backed collection, just let's not have needless intermediate variables
Motivation: ArrayDeque.clear() iterated through all elements calling removeHeadAssumingNonEmpty() one by one, which is O(n) in time due to per-element null-out and pointer advancement. Modification: Replace the while loop with direct Arrays.fill calls to null out the occupied range (handling the circular buffer wrap-around), then reset start and end to 0. This is O(n) in the nulling (necessary for GC) but O(1) in pointer manipulation, and avoids the per-element method call overhead of removeHeadAssumingNonEmpty. Result: ArrayDeque.clear() now uses bulk Arrays.fill for nulling and direct pointer reset, significantly faster for large deques.
He-Pin
force-pushed
the
perf/arraydeque-clear
branch
from
June 9, 2026 07:33
3e5a7ab to
c193d92
Compare
SolalPirelli
approved these changes
Jun 9, 2026
tgodzik
pushed a commit
to scala/scala3-lts
that referenced
this pull request
Sep 30, 2026
…a#26288) ## Description Optimize ArrayDeque.clear() from O(n) to O(1) with GC-safe nulling. ## Problem ArrayDeque.clear() iterated through all elements calling removeHeadAssumingNonEmpty() one by one, which is O(n) in time due to per-element null-out and pointer advancement. ## Solution Replace the while loop with direct Arrays.fill calls to null out the occupied range (handling the circular buffer wrap-around), then reset start and end to 0. This is O(n) in the nulling (necessary for GC) but O(1) in pointer manipulation, and avoids the per-element method call overhead of removeHeadAssumingNonEmpty. ## Result ArrayDeque.clear() now uses bulk Arrays.fill for nulling and direct pointer reset, significantly faster for large deques. ## LLM Policy LLM-based tools were used extensively in this contribution. The code was reviewed and tested locally before submission. [Cherry-picked f36ea50]
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.
Description
Optimize ArrayDeque.clear() from O(n) to O(1) with GC-safe nulling.
Problem
ArrayDeque.clear() iterated through all elements calling removeHeadAssumingNonEmpty() one by one, which is O(n) in time due to per-element null-out and pointer advancement.
Solution
Replace the while loop with direct Arrays.fill calls to null out the occupied range (handling the circular buffer wrap-around), then reset start and end to 0. This is O(n) in the nulling (necessary for GC) but O(1) in pointer manipulation, and avoids the per-element method call overhead of removeHeadAssumingNonEmpty.
Result
ArrayDeque.clear() now uses bulk Arrays.fill for nulling and direct pointer reset, significantly faster for large deques.
LLM Policy
LLM-based tools were used extensively in this contribution. The code was reviewed and tested locally before submission.