Skip to content
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

Check num combinations aginst threshold before generating paths #84

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Dogfalo
Copy link

@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
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