Skip to content

perf: Add max depth limits and optimize circular reference detection#277

Merged
nev21 merged 1 commit intomainfrom
nev21/perf
Feb 17, 2026
Merged

perf: Add max depth limits and optimize circular reference detection#277
nev21 merged 1 commit intomainfrom
nev21/perf

Conversation

@nev21
Copy link
Contributor

@nev21 nev21 commented Feb 16, 2026

  • Add configurable depth limits to prevent stack overflow and O(n²) degradation:

    • maxFormatDepth (default: 50): Limits formatting recursion depth
    • maxCompareDepth (default: 100): Limits deep equality comparison depth
    • maxCompareCheckDepth (default: 50): Limits circular reference check iterations
  • Refactor _deepEqual to require context parameter (never optional):

    • Ensures configuration flows through deep equality checks properly
  • Optimize circular reference detection for better performance:

    • Search backwards through visited list (better cache locality)
    • Respect maxCompareCheckDepth to prevent O(n²) behavior in deep structures
  • Update _formatValue to respect maxFormatDepth limit:

    • Prevents pathological formatting of extremely nested objects
    • Treats excessive nesting as circular references for safety
  • Add comprehensive tests for depth limit configuration and behavior

  • Update copilot-instructions.md with refined @SInCE tag guidance

Critical fixes:

  • Fixes regression where _deepEqual created null context
  • Prevents stack overflow with deeply nested structures
  • Maintains backward search optimization (better performance than forward search)

- Add configurable depth limits to prevent stack overflow and O(n²) degradation:
  - maxFormatDepth (default: 50): Limits formatting recursion depth
  - maxCompareDepth (default: 100): Limits deep equality comparison depth
  - maxCompareCheckDepth (default: 50): Limits circular reference check iterations

- Refactor _deepEqual to require context parameter (never optional):
  - Ensures configuration flows through deep equality checks properly

- Optimize circular reference detection for better performance:
  - Search backwards through visited list (better cache locality)
  - Respect maxCompareCheckDepth to prevent O(n²) behavior in deep structures

- Update _formatValue to respect maxFormatDepth limit:
  - Prevents pathological formatting of extremely nested objects
  - Treats excessive nesting as circular references for safety

- Add comprehensive tests for depth limit configuration and behavior

- Update copilot-instructions.md with refined @SInCE tag guidance

**Critical fixes:**
- Fixes regression where _deepEqual created null context
- Prevents stack overflow with deeply nested structures
- Maintains backward search optimization (better performance than forward search)
@nev21 nev21 added this to the 0.1.8 milestone Feb 16, 2026
Copilot AI review requested due to automatic review settings February 16, 2026 23:28
@nev21 nev21 requested review from a team as code owners February 16, 2026 23:28
Copy link
Contributor

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 introduces configurable recursion/depth limits to harden tripwire’s formatting and deep-equality logic against stack overflows and pathological performance in deeply nested structures, while also refactoring internal deep-equality helpers to always receive configuration context.

Changes:

  • Add maxFormatDepth, maxCompareDepth, and maxCompareCheckDepth configuration options with defaults.
  • Optimize circular reference detection (backward scan + bounded scan window) and enforce comparison depth limits.
  • Add tests validating default values and basic configurability of the new depth settings.

Reviewed changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
core/test/src/config/maxDepth.test.ts Adds test coverage for default depth settings and configurability.
core/src/internal/_formatValue.ts Adds max formatting depth guard and backward circular-reference scan.
core/src/interface/IConfig.ts Documents and exposes new depth-related configuration options.
core/src/config/defaultConfig.ts Sets default values for the new depth-related config options.
core/src/assert/ops/includeOp.ts Updates deep-include operations to pass context into _deepEqual.
core/src/assert/funcs/nested.ts Updates nested assertions to pass context into _deepEqual.
core/src/assert/funcs/members.ts Improves deep-members matching and passes context into _deepEqual.
core/src/assert/funcs/keysFunc.ts Updates deep-keys comparisons to pass context into _deepEqual.
core/src/assert/funcs/equal.ts Enforces compare depth limits, optimizes circular detection, and makes _deepEqual require context.
common/config/rush/npm-shrinkwrap.json Updates locked dependency versions.
.github/copilot-instructions.md Refines repository guidance for selecting @since versions.
Files not reviewed (1)
  • common/config/rush/npm-shrinkwrap.json: Language not supported

@codecov
Copy link

codecov bot commented Feb 16, 2026

Codecov Report

❌ Patch coverage is 80.43478% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.54%. Comparing base (cbf4fc3) to head (a78c4dd).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
core/src/assert/ops/includeOp.ts 37.50% 5 Missing ⚠️
core/src/assert/funcs/equal.ts 71.42% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #277      +/-   ##
==========================================
+ Coverage   91.43%   91.54%   +0.10%     
==========================================
  Files          78       78              
  Lines        3445     3454       +9     
  Branches      886      885       -1     
==========================================
+ Hits         3150     3162      +12     
+ Misses        295      292       -3     
Files with missing lines Coverage Δ
core/src/assert/funcs/keysFunc.ts 93.18% <100.00%> (ø)
core/src/assert/funcs/members.ts 98.80% <100.00%> (+0.01%) ⬆️
core/src/assert/funcs/nested.ts 90.32% <100.00%> (ø)
core/src/config/defaultConfig.ts 100.00% <ø> (ø)
core/src/internal/_formatValue.ts 87.30% <100.00%> (+0.63%) ⬆️
core/src/assert/funcs/equal.ts 83.26% <71.42%> (+1.08%) ⬆️
core/src/assert/ops/includeOp.ts 80.11% <37.50%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nev21 nev21 enabled auto-merge (squash) February 17, 2026 00:17
Copy link
Contributor

@nevware21-bot nevware21-bot left a comment

Choose a reason for hiding this comment

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

Approved by nevware21-bot

@nev21 nev21 merged commit badd619 into main Feb 17, 2026
17 checks passed
@nev21 nev21 deleted the nev21/perf branch February 17, 2026 00:30
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.

2 participants