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

fix(guided remediation): reduce memory footprint by computing dependency subgraphs instead of chains #1538

Merged
merged 7 commits into from
Jan 30, 2025

Conversation

michaelkedar
Copy link
Member

Guided remediation had been using DependencyChains to track paths to a vulnerable package (for computing things like depth and which direct dependencies to relax). It was computing every possible path in the graph to a dependency, which grows roughly exponentially with depth / connectivity. This was using an unreasonable amount of memory on some particularly large/complex projects.

I've changed the logic to instead compute one DependencySubgraph - the set of nodes and edges that would contain every path to a dependency. This should significantly reduce memory usage (and cpu usage from allocs) when running on larger projects.

This change has touched quite a few places in the code, and the logic is a bit complex. I've tried my best to check that everything still behaves as expected.

@codecov-commenter
Copy link

codecov-commenter commented Jan 28, 2025

Codecov Report

Attention: Patch coverage is 72.61905% with 69 lines in your changes missing coverage. Please review.

Project coverage is 69.31%. Comparing base (2821e79) to head (99cb285).

Files with missing lines Patch % Lines
internal/tui/dependency-graph.go 0.00% 49 Missing ⚠️
internal/resolution/dependency_subgraph.go 90.90% 10 Missing and 4 partials ⚠️
internal/resolution/resolve.go 50.00% 5 Missing ⚠️
internal/tui/vuln-info.go 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1538      +/-   ##
==========================================
+ Coverage   69.18%   69.31%   +0.12%     
==========================================
  Files         200      200              
  Lines       18989    19038      +49     
==========================================
+ Hits        13138    13196      +58     
+ Misses       5141     5135       -6     
+ Partials      710      707       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cuixq
Copy link
Contributor

cuixq commented Jan 28, 2025

Any statistics about how much memory usage improves?

@michaelkedar
Copy link
Member Author

Any statistics about how much memory usage improves?

For a particularly gnarly package-lock.json (with almost 6000 packages), the old ComputeChains was allocating 130 GB (!!) vs new ComputeSubgraphs allocating 4.5 MB - around 1/30000th. The total allocations is 137 GB vs 5GB, mostly from writing the lockfile in-place.
(not sure about how much was in use at any given time, just total allocations)

Copy link
Contributor

@cuixq cuixq left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@oliverchang oliverchang left a comment

Choose a reason for hiding this comment

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

awesome optimization!

internal/resolution/dependency_subgraph.go Show resolved Hide resolved
internal/resolution/dependency_subgraph.go Outdated Show resolved Hide resolved
@michaelkedar michaelkedar merged commit d8d794b into google:main Jan 30, 2025
13 checks passed
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.

4 participants