Skip to content

Check num combinations aginst threshold before generating paths#84

Open
Dogfalo wants to merge 1 commit intoantonmedv:masterfrom
Dogfalo:check-threshold-before-generation
Open

Check num combinations aginst threshold before generating paths#84
Dogfalo wants to merge 1 commit intoantonmedv:masterfrom
Dogfalo:check-threshold-before-generation

Conversation

@Dogfalo
Copy link
Copy Markdown

@Dogfalo Dogfalo commented Sep 4, 2024

Check Number of Combinations Against Threshold Before Generating Paths

Summary

This pull request adds a safeguard against potential memory exhaustion in the findUniquePath function by checking the total number of possible combinations before generating them.

Description

In the findUniquePath function, generating combinations of paths can lead to high memory usage if the number of combinations is large.

This update introduces a check that calculates the total number of combinations before proceeding to generate the combinations. If the number of combinations exceeds the configured threshold, the function will return a fallback path (if provided) or null.

Changes Made

  • Added a check to calculate the total number of combinations before generating paths.
  • Modified the function to return early if the number of combinations exceeds the threshold, thus avoiding unnecessary computation and potential memory issues.

if (paths.length > config.threshold) {
return fallback ? fallback() : null
// Check first the total number of combinations first since generating the combinations can cause memory exhaustion
const numCombinations = stack.reduce((acc, i) => acc * i.length, 1);
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

I'm not sure about this calculation logic. Better more the if into combinations() generator and throw an error of limit.

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