Skip to content

Improve coll.flatten stability for large collections#61

Open
shahar-biron wants to merge 11 commits intomainfrom
feature/coll-flatten-stability
Open

Improve coll.flatten stability for large collections#61
shahar-biron wants to merge 11 commits intomainfrom
feature/coll-flatten-stability

Conversation

@shahar-biron
Copy link
Collaborator

@shahar-biron shahar-biron commented Jan 1, 2026

Replace use of push(...item) with indexed loops in coll.flatten to avoid argument explosion and improve stability on large inner arrays.\n\n- Preserve existing one-level flatten semantics and behavior for non-array inputs.\n- All 27 test suites (70 tests) pass against a local FalkorDB instance.\n\nCo-Authored-By: Warp agent@warp.dev

Summary by CodeRabbit

  • New Features

    • Added four new collection utilities: containsAll checks if all candidates exist, containsAny checks if any candidate exists, flatten merges nested arrays, and subtract removes specified elements.
  • Improvements

    • Optimized union function performance.
  • Tests

    • Added comprehensive test coverage for new collection utilities and updated workflow configuration.

✏️ Tip: You can customize this high-level summary in your review settings.

PR Summary by Typo

Overview

This PR improves the stability of coll.flatten for large collections and enhances coll.union for better performance. It also introduces new collection utility functions: containsAll, containsAny, and subtract.

Key Changes

  • Refactored coll.flatten to manually push elements, avoiding potential argument explosion with very large inner arrays.
  • Optimized coll.union to be more allocation- and garbage-collection-friendly by streamlining its internal logic.
  • Added three new collection functions: coll.containsAll (checks if a list contains all specified candidates), coll.containsAny (checks if a list contains any specified candidate), and coll.subtract (returns a list with specified elements removed).
  • Included dedicated test files for all new and modified collection functions.
  • Updated the GitHub Actions workflow for test coverage reporting.

Work Breakdown

Category Lines Changed
New Work 376 (98.7%)
Rework 5 (1.3%)
Total Changes 381
To turn off PR summary, please visit Notification settings.

shahar-biron and others added 5 commits December 25, 2025 11:40
handling toRemove null

Co-authored-by: typo-app[bot] <139475626+typo-app[bot]@users.noreply.github.com>
Co-Authored-By: Warp <agent@warp.dev>
Co-Authored-By: Warp <agent@warp.dev>
Co-Authored-By: Warp <agent@warp.dev>
@typo-app
Copy link

typo-app bot commented Jan 1, 2026

Static Code Review 📊

✅ All quality checks passed!

@coderabbitai
Copy link

coderabbitai bot commented Jan 1, 2026

📝 Walkthrough

Walkthrough

Introduces four new collection utility functions (containsAll, containsAny, flatten, subtract) for array operations with consistent patterns using Set-based lookups and strict equality checks. Refactors the existing union function for improved allocation efficiency. Updates CI/CD workflow configuration for test coverage collection.

Changes

Cohort / File(s) Summary
New Collection Utilities
src/collections/containsAll.js, src/collections/containsAny.js, src/collections/flatten.js, src/collections/subtract.js
Four new array operation functions registered via falkor.register and exported as CommonJS modules. Each implements distinct logic: containsAll verifies all candidates exist in list; containsAny checks for any candidate match; flatten performs one-level array flattening; subtract removes elements. All use Set-based lookups for efficiency and handle null/undefined inputs defensively.
Union Function Refactor
src/collections/union.js
Replaces naive implementation with multi-branch, allocation-conscious logic. Handles non-array inputs, edge cases (empty arrays), and preserves element order while maintaining de-duplication via Set. Updates internal documentation on semantics and performance.
CI/CD Workflow Update
.github/workflows/test.yml
Adds continue-on-error: true to Collect test coverage step. Introduces with block configuring github-token, test-script, annotations, and output parameters for the coverage step.
Collection Integration Tests
tests/collections/containsAll.test.js, tests/collections/containsAny.test.js, tests/collections/flatten.test.js, tests/collections/subtract.test.js
Four parallel test suites validating FLEX graph-based execution and local module behavior. Each initializes FLEX environment, executes Cypher-like queries, and asserts results across typical and edge cases (null inputs, empty arrays, mixed types).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Four new friends hop into our collection,
Set-building springs with strict direction,
Flatten, check, subtract with care—
Array utilities beyond compare! ✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title references coll.flatten stability, but the PR also adds containsAll, containsAny, and subtract utilities, refactors union, and updates CI—the title is partially related but doesn't capture the main breadth of changes.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@typo-app typo-app bot left a comment

Choose a reason for hiding this comment

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

AI Code Review 🤖

Files Reviewed: 10
Comments Added: 1
Lines of Code Analyzed: 386
Critical Issues: 0

PR Health: Good

Give 👍 or 👎 on each review comment to help us improve.

// Seed the Set with `a` and copy `a` to result.
// This preserves the relative order of elements in `a`.
const set = new Set(a);
const result = a.slice();
Copy link

Choose a reason for hiding this comment

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

logic: union may return duplicates from the first array. Initialize result from the Set to ensure uniqueness.

Suggested change
const result = a.slice();
const result = [...set];

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/collections/union.js (1)

23-53: LGTM! Well-optimized allocation-conscious implementation.

The multi-branch approach effectively avoids creating intermediate concatenated arrays, reducing GC pressure. The indexed loop (lines 44-50) is more stable than spread operations for large arrays, and the clear documentation makes the optimization rationale explicit.

Optional: Lines 31-36 could be simplified

The empty-array checks (lines 31-36) could be removed since the main logic (lines 38-50) handles these cases correctly. However, the current approach avoids unnecessary Set operations and allocations for edge cases, so the current implementation is a reasonable performance tradeoff.

-  const lenA = a.length;
-  const lenB = b.length;
-
-  if (lenA === 0 && lenB === 0) return [];
-  if (lenA === 0) return [...new Set(b)];
-  if (lenB === 0) return [...new Set(a)];
-
   // Seed the Set with `a` and copy `a` to result.
src/collections/flatten.js (1)

5-15: Optional: Consider broadening the type annotation.

The @param type is {Array|null}, but the implementation accepts any type and returns [] for non-arrays. Consider using {*} or {any} to better reflect the defensive behavior.

📝 Suggested JSDoc refinement
 /**
  * One-level flatten of a nested list.
  *
  * - If `nested` is not an array, returns an empty list.
  * - For each element:
  *   - If it is an array, its elements are concatenated.
  *   - Otherwise, the element is included as-is.
  *
- * @param {Array|null} nested
+ * @param {*} nested - The value to flatten (expected to be an array)
  * @returns {Array}
  */
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 672e7c1 and 774544a.

📒 Files selected for processing (6)
  • src/collections/containsAll.js
  • src/collections/containsAny.js
  • src/collections/flatten.js
  • src/collections/subtract.js
  • src/collections/union.js
  • tests/collections/subtractContainsFlatten.test.js
🧰 Additional context used
🧬 Code graph analysis (3)
src/collections/containsAll.js (1)
src/collections/containsAny.js (1)
  • set (21-21)
src/collections/containsAny.js (1)
src/collections/containsAll.js (1)
  • set (21-21)
tests/collections/subtractContainsFlatten.test.js (2)
tests/setup.js (1)
  • graph (12-12)
src/collections/flatten.js (1)
  • result (19-19)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test
🔇 Additional comments (6)
src/collections/subtract.js (1)

17-26: LGTM! Clean and efficient implementation.

The function correctly handles all edge cases and uses a Set for efficient O(1) lookups. The shallow copy behavior for non-array toRemove is well-documented and intuitive.

src/collections/containsAny.js (1)

16-27: LGTM! Efficient short-circuit logic.

The implementation correctly uses a Set for O(1) membership checks and short-circuits on the first match. The loose equality check on line 23 appropriately handles both null and undefined as documented.

src/collections/containsAll.js (1)

16-27: LGTM! Correct implementation with proper vacuous truth handling.

The function correctly implements universal quantification semantics, including returning true for empty candidates (vacuous truth). The Set-based implementation ensures efficient lookups.

tests/collections/subtractContainsFlatten.test.js (1)

1-111: Test file is comprehensive and correctly structured.

The test suite appropriately validates both graph query results and local module behavior for all four utilities (subtract, containsAll, containsAny, flatten) with good coverage of edge cases including null inputs and empty arrays. All test expectations align with the corresponding implementations.

src/collections/flatten.js (2)

36-45: Registration and export pattern looks good.

The falkor.register call and conditional CommonJS export follow the project's established pattern for dual-environment support, consistent with the other collection utilities added in this PR.


16-34: LGTM! Solid implementation that achieves the stability goal.

The indexed loop approach (lines 23-28) correctly avoids the argument explosion that would occur with push(...item) on large arrays, addressing the core objective. The logic properly handles both array and non-array elements in the one-level flatten.

Co-Authored-By: Warp <agent@warp.dev>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 774544a and 7e55e05.

📒 Files selected for processing (1)
  • .github/workflows/test.yml


const { initializeFLEX } = require('../setup');

const subtractModule = require('../../src/collections/subtract');
Copy link
Contributor

Choose a reason for hiding this comment

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

Break this test file to multiple ones,
there should be a test file for each Flex function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

sure, 10x

Co-Authored-By: Warp <agent@warp.dev>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
.github/workflows/test.yml (1)

46-49: Configuration looks correct; previous issue resolved.

The inputs are correctly configured per the action's v2 documentation:

  • skip-step: all properly skips install and test execution (tests already ran at line 32)
  • github-token and coverage-file are correctly specified

The previous issue with the invalid skip-step value has been successfully addressed.

Optional: Pin to a more specific version

For improved security and reproducibility, consider pinning to a specific commit SHA or at least a minor version instead of the major version @v2:

-      uses: ArtiomTr/jest-coverage-report-action@v2
+      uses: ArtiomTr/jest-coverage-report-action@v2.x.x  # Replace with latest v2 minor version

Or use a commit SHA:

-      uses: ArtiomTr/jest-coverage-report-action@v2
+      uses: ArtiomTr/jest-coverage-report-action@abc1234...  # Full commit SHA
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7e55e05 and 6b88c1f.

📒 Files selected for processing (1)
  • .github/workflows/test.yml
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test

Co-Authored-By: Warp <agent@warp.dev>
shahar-biron and others added 3 commits January 1, 2026 10:14
Co-Authored-By: Warp <agent@warp.dev>
Co-Authored-By: Warp <agent@warp.dev>
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (4)
tests/collections/containsAny.test.js (1)

37-43: Consider expanding test coverage for edge cases.

The local module tests correctly verify the graph query results and add an extra null check. Consider adding test cases for:

  • containsAny([], []) → false (both empty)
  • containsAny([1], [1]) → true (exact match)
  • containsAny([1,2], [1,2,3]) → true (partial overlap)

These would strengthen confidence in boundary behavior.

tests/collections/flatten.test.js (1)

37-42: Consider adding tests for deeply nested arrays.

The local module tests correctly verify one-level flattening. To make the "one-level" behavior more explicit, consider adding:

  • flatten([[[1,2]]])[[1,2]] (demonstrates only one level is flattened)
  • flatten([])[] (empty input)

This would clarify the expected depth behavior and prevent future confusion.

tests/collections/containsAll.test.js (1)

37-43: Consider expanding edge case coverage.

The local module tests correctly mirror the graph query and add symmetric null handling. Consider adding:

  • containsAll([], [1]) → false (empty list doesn't contain elements)
  • containsAll([1,2], [1,2]) → true (exact match)
  • containsAll([1,1,2], [1]) → true (duplicates in list)

These would provide more comprehensive edge case coverage.

tests/collections/subtract.test.js (1)

35-41: LGTM! Consider adding more edge cases.

The local module tests correctly verify the graph query results and add graceful null handling. The comment on line 39 is helpful for understanding the design decision.

Consider adding tests for:

  • subtract([1,2,2,3], [2])[1,3] (verify all occurrences removed)
  • subtract([1,2,3], [4,5])[1,2,3] (no matching elements)
  • subtract([1,2,3], [1,2,3])[] (remove all)
📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 59a14a5 and 70ee435.

📒 Files selected for processing (5)
  • .github/workflows/test.yml
  • tests/collections/containsAll.test.js
  • tests/collections/containsAny.test.js
  • tests/collections/flatten.test.js
  • tests/collections/subtract.test.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • .github/workflows/test.yml
🧰 Additional context used
🧬 Code graph analysis (2)
tests/collections/containsAll.test.js (4)
tests/collections/containsAny.test.js (2)
  • require (5-5)
  • db (9-9)
tests/collections/flatten.test.js (2)
  • require (5-5)
  • db (9-9)
tests/collections/subtract.test.js (2)
  • require (5-5)
  • db (9-9)
tests/setup.js (1)
  • graph (12-12)
tests/collections/containsAny.test.js (4)
tests/collections/containsAll.test.js (2)
  • require (5-5)
  • db (9-9)
tests/collections/flatten.test.js (2)
  • require (5-5)
  • db (9-9)
tests/collections/subtract.test.js (2)
  • require (5-5)
  • db (9-9)
tests/setup.js (1)
  • graph (12-12)
🔇 Additional comments (8)
tests/collections/containsAny.test.js (2)

1-21: LGTM!

The test setup and teardown are correctly structured with async lifecycle hooks and proper cleanup.


23-36: LGTM!

The graph query test cases cover the essential scenarios: intersection detection, no intersection, empty candidate list, and null input handling.

tests/collections/flatten.test.js (2)

1-21: LGTM!

The test structure follows the same pattern as other collection tests with proper async lifecycle management.


23-35: LGTM!

The test cases effectively demonstrate one-level flatten behavior with nested arrays, mixed types, empty arrays, and null input handling.

tests/collections/containsAll.test.js (2)

1-21: LGTM!

The test structure is consistent with other collection tests and properly manages the database lifecycle.


23-35: LGTM!

The test cases correctly cover subset checking, partial match failure, empty candidate list (vacuous truth), and null input handling.

tests/collections/subtract.test.js (2)

1-21: LGTM!

The test structure follows the established pattern with proper async lifecycle management.


23-33: LGTM!

The test cases effectively verify subtract behavior: removing all occurrences, handling empty removal lists, and null input handling.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants