Skip to content

perf: ArrayDeque.clear() from O(n) to O(1) with GC-safe nulling - #26288

Merged
SolalPirelli merged 1 commit into
scala:mainfrom
He-Pin:perf/arraydeque-clear
Jun 11, 2026
Merged

SolalPirelli merged 1 commit into
scala:mainfrom
He-Pin:perf/arraydeque-clear

Conversation

@He-Pin

@He-Pin He-Pin commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

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.

@He-Pin
He-Pin marked this pull request as ready for review June 8, 2026 17:32
@He-Pin
He-Pin requested a review from a team as a code owner June 8, 2026 17:32

@SolalPirelli SolalPirelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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
He-Pin force-pushed the perf/arraydeque-clear branch from 3e5a7ab to c193d92 Compare June 9, 2026 07:33
@He-Pin
He-Pin requested a review from SolalPirelli June 9, 2026 07:33
@SolalPirelli
SolalPirelli merged commit f36ea50 into scala:main Jun 11, 2026
45 checks passed
@WojciechMazur WojciechMazur added this to the 3.10.0 milestone Aug 26, 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]
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