Skip to content

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Perf #24

wants to merge 5 commits into from

Conversation

tiye
Copy link
Member

@tiye tiye commented Aug 2, 2025

No description provided.

@tiye tiye requested review from a team and Copilot August 2, 2025 08:03
Copy link

@Copilot Copilot AI left a 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

Comment on lines +1127 to +1133
// 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);
Copy link
Preview

Copilot AI Aug 2, 2025

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.

Suggested change
// 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.

Comment on lines +111 to +116
try {
fn();
throw new Error(message || "Expected function to throw, but it didn't");
} catch (error) {
// Success - function threw as expected
}
Copy link
Preview

Copilot AI Aug 2, 2025

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.

Suggested change
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.

Comment on lines +111 to +116
try {
fn();
throw new Error(message || "Expected function to throw, but it didn't");
} catch (error) {
// Success - function threw as expected
}
Copy link
Preview

Copilot AI Aug 2, 2025

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.

Suggested change
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.

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.

1 participant