-
-
Notifications
You must be signed in to change notification settings - Fork 11
perf: optimize CSVRecordAssembler with single-loop array processing #571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| --- | ||
| "web-csv-toolbox": patch | ||
| --- | ||
|
|
||
| Optimize CSVRecordAssembler with single-loop array processing | ||
|
|
||
| **Performance Optimization:** | ||
| - Replaced chained array methods (`.map().filter().map()`) with single for-loop | ||
| - Reduces array iterations from 3 passes to 1 pass | ||
| - Applied to 3 critical code paths: | ||
| - Processing non-empty records (RecordDelimiter handler) | ||
| - Processing empty lines (skipEmptyLines=false case) | ||
| - Flushing remaining buffered data | ||
|
|
||
| **Implementation Details:** | ||
| - Changed from functional chaining to imperative loop for better performance | ||
| - Direct array construction eliminates intermediate array allocations | ||
| - Maintains identical output behavior - all 460 tests pass | ||
|
|
||
| **Impact:** | ||
| - Most beneficial for CSVs with many columns | ||
| - Reduces CPU cycles during record assembly phase | ||
| - Complements the CSVLexer buffer pointer optimization |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -110,19 +110,26 @@ export class CSVRecordAssembler<Header extends ReadonlyArray<string>> { | |
| this.#setHeader(this.#row as unknown as Header); | ||
| } else { | ||
| if (this.#dirty) { | ||
| yield Object.fromEntries( | ||
| this.#header | ||
| .map((header, index) => [header, index] as const) | ||
| .filter(([header]) => header) | ||
| .map(([header, index]) => [header, this.#row.at(index)]), | ||
| ) as unknown as CSVRecord<Header>; | ||
| // Optimize: single loop instead of map().filter().map() | ||
| const entries: [string, string | undefined][] = []; | ||
| for (let i = 0; i < this.#header.length; i++) { | ||
| const header = this.#header[i]; | ||
| if (header) { | ||
| entries.push([header, this.#row[i]]); | ||
| } | ||
| } | ||
| yield Object.fromEntries(entries) as unknown as CSVRecord<Header>; | ||
| } else { | ||
| if (!this.#skipEmptyLines) { | ||
| yield Object.fromEntries( | ||
| this.#header | ||
| .filter((header) => header) | ||
| .map((header) => [header, ""]), | ||
| ) as CSVRecord<Header>; | ||
| // Optimize: single loop instead of filter().map() | ||
| const entries: [string, string][] = []; | ||
| for (let i = 0; i < this.#header.length; i++) { | ||
| const header = this.#header[i]; | ||
| if (header) { | ||
| entries.push([header, ""]); | ||
| } | ||
| } | ||
| yield Object.fromEntries(entries) as CSVRecord<Header>; | ||
|
Comment on lines
+124
to
+132
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| } | ||
| } | ||
|
|
@@ -144,12 +151,15 @@ export class CSVRecordAssembler<Header extends ReadonlyArray<string>> { | |
| *#flush(): IterableIterator<CSVRecord<Header>> { | ||
| if (this.#header !== undefined) { | ||
| if (this.#dirty) { | ||
| yield Object.fromEntries( | ||
| this.#header | ||
| .map((header, index) => [header, index] as const) | ||
| .filter(([header]) => header) | ||
| .map(([header, index]) => [header, this.#row.at(index)]), | ||
| ) as unknown as CSVRecord<Header>; | ||
| // Optimize: single loop instead of map().filter().map() | ||
| const entries: [string, string | undefined][] = []; | ||
| for (let i = 0; i < this.#header.length; i++) { | ||
| const header = this.#header[i]; | ||
| if (header) { | ||
| entries.push([header, this.#row[i]]); | ||
| } | ||
| } | ||
| yield Object.fromEntries(entries) as unknown as CSVRecord<Header>; | ||
|
Comment on lines
+154
to
+162
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| } | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The optimization from chained
map().filter().map()to a singleforloop is a significant improvement for performance, especially when dealing with large datasets. This change reduces array iterations and avoids intermediate array allocations, which aligns with the PR's goal of optimizing CPU cycles.