Skip to content

reduce memory allocations in Requirements.simplify - #4314

Merged
forki merged 2 commits into
fsprojects:masterfrom
chinwobble:perf
Jan 19, 2026
Merged

forki merged 2 commits into
fsprojects:masterfrom
chinwobble:perf

Conversation

@chinwobble

@chinwobble chinwobble commented Jan 16, 2026 •

Copy link
Copy Markdown
Contributor

Context:
I am converting a project from nuget to paket and there are many conflicts. I noticed that the dotnet paket install command can take several minutes just to tell me there is a conflict and paket cannot generate a lock file.

I have used https://github.com/microsoft/perfview to find where the bottlenecks.

I have identified a few bottlenecks:

  • my paket.dependencies has 86 lines in it and it is allocating ~2.7 GB of memory
  • the api calls to nuget are quite slow. 300ms just to return the list versions of available for each package. Based on my paket.dependencies file, there are 563 api calls nuget.org. These are async and run concurrently but In some cases there are large gaps between api calls of 200ms+.

I have run perf view with the following settings

build.cmd publish 

PerfView.exe  "/DataFile:PerfViewData-baseline.etl" /BufferSizeMB:256 /StackCompression /KernelEvents:VirtualAlloc,VAMap,Default /Process:"paket" /ClrEvents:GC,Binder,Security,AppDomainResourceManagement,Contention,Exception,Threading,JITSymbols,Type,GCHeapSurvivalAndMovement,GCHeapAndTypeNames,Stack,ThreadTransfer,Codesymbols,Compilation /NoGui /NoNGenRundown /DotNetAllocSampled run C:\dev\paket\bin\net8\paket.exe install

Equivalent to the below settings:

image

Using the tool GC Heap Alloc Ignore Free tool and setting include pattern to be:

Process% paket (29684);paket!<StartupCode$paket>.$Paket.Program.main@();paket!Paket.Program.main()

By default events are captured machine wide, we are only interested in memory allocation events that from stack traces within the paket process' main method. perfview captures diagnostic system wide.

Drilling into the call tree shows that Requirements.Simplify' allocates 13% of the memory of the lifetime of paket install. I use the .NET SampAlloc which samples allocations as I ran out of disk space running with .NET Alloc CheckBox. It consumed 70 GB+.

image

GC Stats

After collecting the trace before and after the code change the stats have changed accordingly:

  Before After
Total CPU Time: 16.841 ms 15.399 ms
Total GC CPU Time: 732 ms 653 ms
Total Allocs : 2,759.402 MB 2222.850 MB
Total GC Pause: 765 ms 653 ms
% Time paused for Garbage Collection: 1.40% 2.00%
% CPU Time spent Garbage Collecting: 4.30% 4.20%

@chinwobble
chinwobble marked this pull request as ready for review January 17, 2026 06:22
@chinwobble chinwobble changed the title reduce memory allocations reduce memory allocations in Requirements.simplify Jan 17, 2026
member x.RepresentedFrameworks =
match x with
| FrameworkRestrictionP.ExactlyP r -> [ r ] |> Set.ofList
| FrameworkRestrictionP.ExactlyP r -> Set.singleton r

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

this line reduces allocations by 72 bytes per operation.

| [single] -> single.RepresentedFrameworks
| h :: rest ->
// Use fold to avoid Seq.map allocation
List.fold (fun (acc : Set<TargetProfile>) (fr : FrameworkRestrictionP) ->

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

using fold instead of list.Map reduce allocations by 104 bytes per operation

@forki

forki commented Jan 19, 2026

Copy link
Copy Markdown
Member

thanks!

@forki
forki merged commit 0a1f654 into fsprojects:master Jan 19, 2026
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