Skip to content

feat(linter): no accumulating spread - #1607

Merged
Boshen merged 11 commits into
mainfrom
don/linter/feat/no-reduce-spread
Dec 5, 2023
Merged

Boshen merged 11 commits into
mainfrom
don/linter/feat/no-reduce-spread

Conversation

@DonIsaac

@DonIsaac DonIsaac commented Dec 2, 2023 •

Copy link
Copy Markdown
Contributor

Creates a new nursery-level rule, no-reduce-spread, that prevents spreading accumulator objects/arrays in Array.prototype.reduce.

The Tl;Dr of why this should be avoided is because it drastically increases time/memory complexity in reductions. In general, it turns potentially O(1)/O(n) memory operations into O(n)/O(n^2), and O(n) time into O(n^2). For more details, refer to this helpful blog post.

Note that this rule doesn't exist in ESLint's default rule set or any other ESLint plugin, although it looks like there's been some interest in it in the past. Since there is no reference implementation to compare against, and thus this could potentially have bugs, I've decided to make this a nursery-level rule until we're sure it's stable and useful.


Edit:

@DonIsaac DonIsaac added C-enhancement A-linter Area - Linter (oxlint): rules, config, diagnostics, fixes. Add L-* labels for details labels Dec 2, 2023
@DonIsaac DonIsaac self-assigned this Dec 2, 2023
@codspeed

codspeed Bot commented Dec 2, 2023 •

Copy link
Copy Markdown
Contributor

CodSpeed Performance Report

Merging #1607 will not alter performance

Comparing don/linter/feat/no-reduce-spread (245a3df) with main (b5f8a65)

Summary

✅ 20 untouched benchmarks

@Boshen

Boshen commented Dec 2, 2023

Copy link
Copy Markdown
Member

Love this! Let me test this on our internal repos before landing, if all goes well I may mark this as correctness (or perhaps introduce a new performance category?).

@DonIsaac

DonIsaac commented Dec 2, 2023

Copy link
Copy Markdown
Contributor Author

My vote is a new performance category; I'm planning on migrating eslint-plugin-react-perf too

@DonIsaac
DonIsaac requested a review from Boshen December 2, 2023 20:38
@DonIsaac

DonIsaac commented Dec 3, 2023

Copy link
Copy Markdown
Contributor Author

Love this! Let me test this on our internal repos before landing, if all goes well I may mark this as correctness (or perhaps introduce a new performance category?).

any updates? Can I merge this?

@Boshen

Boshen commented Dec 4, 2023

Copy link
Copy Markdown
Member

I've been afk, will get to this soon.

Boshen added a commit that referenced this pull request Dec 4, 2023
Comment thread crates/oxc_linter/src/rules.rs Resolved
Boshen added a commit that referenced this pull request Dec 4, 2023
@Boshen

Boshen commented Dec 4, 2023

Copy link
Copy Markdown
Member

Testing in the vscode repo:

False positives:

  ⚠ perf(no-reduce-spread): Do not spread accumulators in Array.prototype.reduce()
      ╭─[src/vs/workbench/contrib/extensions/browser/extensionEditor.ts:1300:1]
 1300 │             const viewContainersForLocation: IViewContainer[] = contrib[location];
 1301 │             result.push(...viewContainersForLocation.map(viewContainer => ({ ...viewContainer, location })));
      ·                                                                              ────────────────
 1302 │             return result;
      ╰────
  help: Prefer property assignment or Array.prototype.push()

@Boshen Boshen self-assigned this Dec 5, 2023
@Boshen

Boshen commented Dec 5, 2023

Copy link
Copy Markdown
Member

@camc314 Do you have the time to clean up this rule (also sync its name and tests with Biome)? https://biomejs.dev/linter/rules/no-accumulating-spread

@camc314

camc314 commented Dec 5, 2023

Copy link
Copy Markdown
Contributor

@camc314 Do you have the time to clean up this rule (also sync its name and tests with Biome)? https://biomejs.dev/linter/rules/no-accumulating-spread

yes, will try to do today.

i've fixed the naming ect, will work on the falst positive - i think if we cross a function boundary, we can just break out of that loop where we check for reduce calls and we should be fine

@camc314 camc314 changed the title feat(linter): add perf/no-reduce-spread rule feat(linter): cxc: no accumulating spread Dec 5, 2023
@camc314
camc314 requested a review from Boshen December 5, 2023 08:47

@Boshen Boshen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@camc314
camc314 requested a review from Boshen December 5, 2023 09:10
@Boshen
Boshen merged commit 795db7c into main Dec 5, 2023
@Boshen
Boshen deleted the don/linter/feat/no-reduce-spread branch December 5, 2023 09:25
@Boshen Boshen changed the title feat(linter): cxc: no accumulating spread feat(linter): no accumulating spread Dec 5, 2023
@Boshen

Boshen commented Dec 5, 2023

Copy link
Copy Markdown
Member

Great work team @DonIsaac and @camc314 !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-linter Area - Linter (oxlint): rules, config, diagnostics, fixes. Add L-* labels for details

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants