-
Notifications
You must be signed in to change notification settings - Fork 0
Perf #24
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
base: main
Are you sure you want to change the base?
Conversation
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.
Pull Request Overview
This PR implements significant performance optimizations throughout the TernaryTree data structure codebase, with a primary focus on reducing redundant property access and improving cache efficiency. The optimizations target both list and map operations across creation, lookup, and modification operations.
- Caching of frequently accessed properties (tree children, array lengths, elements) to reduce repeated property access overhead
- Replacement of object-based grouping with Map for better performance in numeric key scenarios
- Pre-allocation of arrays with known sizes and optimized iteration patterns
- Enhanced test framework with colored output, timing, and better assertion methods
Reviewed Changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.
Show a summary per file
File | Description |
---|---|
src/test-utils.mts | Complete rewrite of test framework with describe/test structure, colored output, timing, and comprehensive assertion methods |
src/test.mts | Added test framework import and summary output with console logging |
src/test-map.mts | Converted to new test framework structure with describe blocks and improved assertion usage |
src/test-list.mts | Converted to new test framework structure with describe blocks and improved assertion usage |
src/map.mts | Extensive performance optimizations including property caching, Map usage over Record, and pre-allocated arrays |
src/list.mts | Comprehensive performance optimizations with cached property access and size calculations |
src/test-*-perf.mts | New performance testing files for benchmarking optimizations |
package.json | Added build and test scripts for development workflow |
TESTING.md | New comprehensive testing documentation |
README.md | Updated with development commands and testing information |
// Pre-allocate array with known size for better performance | ||
const totalSize = mapLen(tree); | ||
let result: Array<[K, T]> = new Array(totalSize); | ||
let idx: RefInt = { value: 0 }; | ||
|
||
// Use the more efficient collectHashSortedArray instead | ||
collectHashSortedArray(tree, result, idx); |
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 function is using collectHashSortedArray
which expects pre-allocated array positions, but toPairsArray
should maintain the original order-preserving behavior. Using collectHashSortedArray
changes the semantics from the original collectToPairsArray
implementation.
// Pre-allocate array with known size for better performance | |
const totalSize = mapLen(tree); | |
let result: Array<[K, T]> = new Array(totalSize); | |
let idx: RefInt = { value: 0 }; | |
// Use the more efficient collectHashSortedArray instead | |
collectHashSortedArray(tree, result, idx); | |
// Use order-preserving collection | |
let result: Array<[K, T]> = []; | |
collectToPairsArray(result, tree); |
Copilot uses AI. Check for mistakes.
src/test-utils.mts
Outdated
try { | ||
fn(); | ||
throw new Error(message || "Expected function to throw, but it didn't"); | ||
} catch (error) { | ||
// Success - function threw as expected | ||
} |
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 checkThrows
function will incorrectly pass if the function throws an error that matches the expected message. The logic should only throw an error if the function doesn't throw at all, but currently it always throws after the try block.
try { | |
fn(); | |
throw new Error(message || "Expected function to throw, but it didn't"); | |
} catch (error) { | |
// Success - function threw as expected | |
} | |
let threw = false; | |
try { | |
fn(); | |
} catch (error) { | |
threw = true; | |
if (message !== undefined) { | |
// Compare error message | |
const errorMessage = error instanceof Error ? error.message : String(error); | |
if (errorMessage !== message) { | |
throw new Error(`Expected error message "${message}", but got "${errorMessage}"`); | |
} | |
} | |
} | |
if (!threw) { | |
throw new Error(message || "Expected function to throw, but it didn't"); | |
} |
Copilot uses AI. Check for mistakes.
src/test-utils.mts
Outdated
try { | ||
fn(); | ||
throw new Error(message || "Expected function to throw, but it didn't"); | ||
} catch (error) { | ||
// Success - function threw as expected | ||
} |
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 catch block is empty with only a comment, which means any error thrown by the function will be silently caught and the test will pass. This defeats the purpose of testing that a function throws - the function should return normally for the test to fail.
try { | |
fn(); | |
throw new Error(message || "Expected function to throw, but it didn't"); | |
} catch (error) { | |
// Success - function threw as expected | |
} | |
let threw = false; | |
try { | |
fn(); | |
} catch (error) { | |
// Success - function threw as expected | |
threw = true; | |
} | |
if (!threw) { | |
throw new Error(message || "Expected function to throw, but it didn't"); | |
} |
Copilot uses AI. Check for mistakes.
No description provided.