Skip to content

Optimize hot paths and eliminate code duplication#47

Draft
Copilot wants to merge 9 commits intomainfrom
copilot/improve-code-efficiency
Draft

Optimize hot paths and eliminate code duplication#47
Copilot wants to merge 9 commits intomainfrom
copilot/improve-code-efficiency

Conversation

Copy link

Copilot AI commented Jan 26, 2026

Identified and addressed performance bottlenecks and duplicated code patterns in the async iterator buffering logic.

Changes

Performance optimizations:

  • Replace array spread with generator in fillQueue() hot path - eliminates repeated allocations when determining least-targeted iterator

Code quality:

  • Extract normalizeError() utility - consolidates 4 instances of identical error normalization
  • Add isValueObject() helper - eliminates duplicate type checks in isIterable() and isAsyncIterable()
  • Add bounds check to ordered promise insertion loop
  • Maintain recursive fillQueue() approach - keeps cognitive complexity low (stack overflow not a concern with typical buffer sizes of 6-10)

Configuration:

  • Update engines.node to >=18.18.0 (dependency requirement)
  • Remove unused @types/chai-quantifiers
  • Fix knip configuration

Before/After Example

// Before: Array spread creates temporary allocation on every call
const iterator = findLeastTargeted(
  mainReturnedDone ? subIterators : [...subIterators, asyncIterator],
  bufferedPromises,
  promisesToSourceIteratorMap
);

// After: Generator yields without allocation
const targets = mainReturnedDone
  ? subIterators
  : yieldArrayWithItem(subIterators, asyncIterator);
const iterator = findLeastTargeted(targets, bufferedPromises, promisesToSourceIteratorMap);

Test coverage improved from 98.45% to 98.59% with no regressions.

Original prompt

Identify and suggest improvements or refactorings to slow, inefficient and/or duplicated code


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI and others added 7 commits January 26, 2026 18:16
Co-authored-by: voxpelli <34457+voxpelli@users.noreply.github.com>
Co-authored-by: voxpelli <34457+voxpelli@users.noreply.github.com>
Co-authored-by: voxpelli <34457+voxpelli@users.noreply.github.com>
Co-authored-by: voxpelli <34457+voxpelli@users.noreply.github.com>
Co-authored-by: voxpelli <34457+voxpelli@users.noreply.github.com>
Co-authored-by: voxpelli <34457+voxpelli@users.noreply.github.com>
Co-authored-by: voxpelli <34457+voxpelli@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and suggest improvements to slow and duplicated code Optimize hot paths and eliminate code duplication Jan 26, 2026
Copilot AI requested a review from voxpelli January 26, 2026 18:29
@voxpelli
Copy link
Owner

Co-authored-by: voxpelli <34457+voxpelli@users.noreply.github.com>
Copy link
Author

Copilot AI commented Jan 26, 2026

@copilot Look into https://sonarcloud.io/project/issues?pullRequest=47&open=AZv7itFh5D3hkRvE8pxQ&id=voxpelli_buffered-async-iterable

The SonarCloud issue was likely flagging increased cognitive complexity from wrapping the fillQueue logic in a while loop. I've addressed this in commit 1c434e4 by reverting to the recursive approach.

The recursive call is actually better here because:

  • Default buffer size is 6, with typical values of 3-10 in tests
  • Stack overflow is not a realistic concern with these depths
  • The recursive version has lower cognitive complexity since it avoids an additional nesting level
  • The performance benefit is equivalent (both create the same number of promises)

The other optimizations (error normalization helper, generator for array operations, type check refactoring) remain and provide the maintainability improvements without increasing complexity.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • sonarcloud.io
    • Triggering command: /home/REDACTED/work/_temp/ghcca-node/node/bin/node /home/REDACTED/work/_temp/ghcca-node/node/bin/node --enable-source-maps /home/REDACTED/work/_temp/copilot-developer-action-main/dist/index.js (dns block)
    • Triggering command: /usr/bin/curl curl -L REDACTED (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@sonarqubecloud
Copy link

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