Skip to content

perf(deque): avoid unnecessary allocations in extract_if, filter, to_array#3331

Closed
bobzhang wants to merge 1 commit into
mainfrom
perf/deque-allocations
Closed

perf(deque): avoid unnecessary allocations in extract_if, filter, to_array#3331
bobzhang wants to merge 1 commit into
mainfrom
perf/deque-allocations

Conversation

@bobzhang
Copy link
Copy Markdown
Contributor

@bobzhang bobzhang commented Mar 23, 2026

Summary

  • extract_if/filter: pre-allocate result deque with capacity=self.len instead of starting from capacity 0.
  • to_array: use Array::new(capacity=len) + push instead of Array::make(len, self[0]) + overwrite, avoiding redundant initial fill.

Test plan

  • moon test -p moonbitlang/core/deque — all 232 tests pass

🤖 Generated with Claude Code


Open with Devin

Copy link
Copy Markdown

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@coveralls
Copy link
Copy Markdown
Collaborator

coveralls commented Mar 23, 2026

Pull Request Test Coverage Report for Build 3152

Details

  • 5 of 5 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 95.721%

Totals Coverage Status
Change from base Build 3151: 0.0%
Covered Lines: 13825
Relevant Lines: 14443

💛 - Coveralls

@bobzhang bobzhang force-pushed the perf/deque-allocations branch from 3a37ee8 to 21170ac Compare March 23, 2026 09:18
…array

- extract_if/filter: pre-allocate result deque with capacity=self.len
  instead of starting from capacity 0, avoiding repeated reallocations
  and copies during common "keep most elements" patterns
- to_array: use Array::new(capacity=len) + push instead of
  Array::make(len, self[0]) + overwrite, avoiding the redundant
  initial fill with self[0] that doubles work for expensive-to-copy types

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@bobzhang bobzhang force-pushed the perf/deque-allocations branch from 21170ac to e740a8c Compare March 23, 2026 10:39
@bobzhang
Copy link
Copy Markdown
Contributor Author

closed since this is not necessarily good, it may be super wasteful, if for 1,000,000 elements only 1 gets extracted

@bobzhang bobzhang closed this Mar 25, 2026
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.

2 participants