Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 5, 2025

Description

This PR documents the investigation into why building projects with many files (10,000+) exhibits O(n²) scaling. The root cause is identified as CombineCcuContentFragments iterating ALL accumulated entities for each file merge.

Key Finding: File Count vs Module Count Experiment

To isolate whether the issue scales with file count or module count, we tested the same 3000 modules organized differently:

Files Modules/File Typecheck Time Total Time Memory
3000 1 142.07s 163.15s 5202 MB
1000 3 30.59s 46.36s 2037 MB
3 1000 10.41s 28.00s 1421 MB
1 3000 18.08s 36.57s 1441 MB

Conclusion: With the same 3000 modules:

  • 3000 files is 7.9x slower than 1 file (142s vs 18s typecheck)
  • 3000 files uses 3.6x more memory (5.2GB vs 1.4GB)
  • The O(n²) scaling correlates with FILE COUNT, not module count

CombineModuleOrNamespaceTypes Instrumentation (3000 files, 3000 modules):

Added global counters to track iterations and entity processing:

Iteration Path mty1.entities mty2.entities Total Entities Processed
500 root 0 1 28,221
1000 ConsoleApp1 2 664 112,221
3000 root 1 1 1,004,000
7000 ConsoleApp1 2 4,664 5,452,221
12000 ConsoleApp1 2 3,000 11,263,500
14500 ConsoleApp1 2 5,500 16,582,250

Key findings from instrumentation:

  • 14,500+ total iterations of CombineModuleOrNamespaceTypes for 3000 files
  • ~16.6 million total entity operations - confirming O(n²) scaling
  • The ConsoleApp1 namespace merge handles increasingly large entity counts (up to 5,500 entities per merge)
  • Each file adds 2 new entities, but the accumulated namespace grows linearly

Phase Breakdown (from --times flag):

Phase 1000 files 2000 files 3000 files
Typecheck 16.75s 67.69s 171.45s
Optimizations 2.80s 4.96s 6.14s
TAST->IL 1.50s 2.25s 3.16s

The Typecheck phase exhibits clear O(n²) growth and dominates total compilation time.

Root cause in CombineModuleOrNamespaceTypes:

  • Builds lookup table from ALL accumulated entities - O(n)
  • Iterates ALL accumulated entities checking conflicts - O(n)
  • Reconstructs entity list - O(n)

Investigation artifacts:

  • Added investigation/INSIGHTS.md documenting complete analysis
  • Added instrumentation to CombineModuleOrNamespaceTypes tracking iteration counts and entity processing
  • Captured dotnet-trace profile for detailed hot path analysis
  • Tested optimization attempt (fast path for no conflicts) - reverted as it made performance worse (+12-22% overhead)

Conclusion: The fsharp-10k test case has all files in same namespace, causing top-level namespace conflict on every merge. A proper fix requires architectural changes:

  • Restructuring the CCU accumulator to support O(1) entity appends
  • Using incremental updates instead of full merges
  • Caching AllEntitiesByLogicalMangledName across merges
  • Or using different data structures that support efficient union operations

Checklist

  • Test cases added
  • Performance benchmarks added in case of performance changes
  • Release notes entry updated
Original prompt

This section details on the original issue you should resolve

<issue_title>Building a large F# project is indeterminately slow</issue_title>
<issue_description>I've created a very simple F# project with 10'000 modules: https://github.com/ners/fsharp-10k

Running dotnet build in this project appears to never complete; I killed the process after 10 minutes after it had consumed most of my RAM.

Repro steps

Expected behavior

The build completes in a reasonable amount of time, e.g. 10 minutes.

Ideally the progress of the build is reported to the CLI, e.g.: [123/1000]

Actual behavior

The build takes an indeterminate amount of time.

Related information

I have a reasonably powerful setup (Ryzen 9950X) running Linux and using .NET 10.0.100. I also tested with 8.0.416, where the result was the same.</issue_description>

<agent_instructions>Identify the hot paths in current compiler (build from main, applied to the project by .props file).

Analyze a memory dump for the top 30 types.

Analyze a trace for top 20 hot path methods in scope.

Use the --times and --times:file.csv flags (applied via MSBuild prop) to identify memory usage per phases of compilation</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@T-Gro Thanks for the report, let us take a look. There is a compile time perf regression in 10.0.100 for specific patterns of code, it is fixed in code already.

I will check it.</comment_new>
<comment_new>@T-Gro
Looks like a GC issue (15GB memory usage after 5K modules).
Its not that much code, but the fact its being split into 10K files likely means extensive copies of allocations and/or references held.</comment_new>

Custom agent used: Compile perf investigator
Specialized agent for investigating F# build performance issues using the local compiler, trace/dump/benchmark tools, and rigorous, resumable insight-centric documentation.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits December 5, 2025 10:51
… no entity name conflicts

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
…jects

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix indeterminate build time for large F# project Investigate O(n²) scaling in CombineCcuContentFragments for large projects Dec 5, 2025
Copilot AI requested a review from T-Gro December 5, 2025 10:56
Copilot finished work on behalf of T-Gro December 5, 2025 10:56
Copy link
Member

@T-Gro T-Gro left a comment

Choose a reason for hiding this comment

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

Repeat the experiment and measure time, memory and timing of different parts in the compiler, after this change.

Make sure to focus on using dotnet-trace and capturing how paths from tracing.

@copilot

…analysis

Performance measurements showed the attempted optimization made things worse:
- Stock: 1000=24s, 2000=65s, 3000=160s
- Optimized: 1000=27s, 2000=80s, 3000=188s (+12-22% overhead)

The optimization is reverted. Updated INSIGHTS.md with:
- Comparative timing data (stock vs optimized)
- Phase breakdown from --times flag (Typecheck phase is O(n²))
- dotnet-trace analysis captured
- Explanation of why optimization didn't help

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 5, 2025

Repeat the experiment and measure time, memory and timing of different parts in the compiler, after this change.

Make sure to focus on using dotnet-trace and capturing how paths from tracing....

Ran the experiments with detailed measurements in commit f23efca.

Timing Comparison (Stock vs Modified):

Files Stock Modified Change
1000 24s 27s +12%
2000 65s 80s +22%
3000 160s 188s +17%

Phase Breakdown (--times flag):

Phase 1000 files 2000 files 3000 files
Typecheck 16.75s 67.69s 171.45s
Optimizations 2.80s 4.96s 6.14s

The Typecheck phase exhibits clear O(n²) growth. dotnet-trace captured at /tmp/trace1000.nettrace.

Conclusion: The optimization attempt made things worse. The overhead from conflict detection exceeded any savings. Reverted the change. The root cause (full entity iteration in CombineCcuContentFragments) requires architectural changes to fix properly.

Copilot AI changed the title Investigate O(n²) scaling in CombineCcuContentFragments for large projects Investigation: O(n²) scaling in CombineCcuContentFragments for large projects Dec 5, 2025
Copilot AI requested a review from T-Gro December 5, 2025 12:55
Copilot finished work on behalf of T-Gro December 5, 2025 12:55
@T-Gro
Copy link
Member

T-Gro commented Dec 5, 2025

@copilot :

Make a new set of experiments, compare the time+memory results for:

3000 files with 3000 modules
1000 files with 3000 modules (i.e. 3 modules per file)
3 files with 3000 modules
1 file with 3000 modules (i.e. semantically the same, just differently organized)

I want to see:

  • type checking time
  • GC allocations
  • overall maximal memory usage
  • overall time taken

Note that all of these will be functionallty the same, it really is only about organizing the same modules into different numbers of files.

…files, not modules

Tested same 3000 modules organized into different file counts:
- 3000 files (1 module/file): 142s typecheck, 5.2GB memory
- 1000 files (3 modules/file): 31s typecheck, 2.0GB memory
- 3 files (1000 modules/file): 10s typecheck, 1.4GB memory
- 1 file (3000 modules): 18s typecheck, 1.4GB memory

Key finding: With same modules, 3000 files is 7.9x slower than 1 file.
The issue is clearly correlated with NUMBER OF FILES, not modules.

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 5, 2025

@copilot :

Make a new set of experiments, compare the time+memory results for:
...

Ran the experiments in commit f1b3ebb.

Same 3000 modules, different file organizations:

Files Modules/File Typecheck Total Time Memory
3000 1 142.07s 163.15s 5202 MB
1000 3 30.59s 46.36s 2037 MB
3 1000 10.41s 28.00s 1421 MB
1 3000 18.08s 36.57s 1441 MB

Key findings:

  • Same 3000 modules: 3000 files is 7.9x slower than 1 file (142s vs 18s typecheck)
  • Memory: 3.6x more with 3000 files vs 1 file (5.2GB vs 1.4GB)
  • The O(n²) scaling correlates with FILE COUNT, not module count

This confirms the root cause is in the per-file CCU signature merge (CombineCcuContentFragments).

@T-Gro
Copy link
Member

T-Gro commented Dec 5, 2025

@copilot :

Use the compiler's local Activity module to add instrumentation about timing, contents and size of the data handled within CombineModuleOrNamespaceTypes .

Use the 3000 files, 3000 modules, but extract meaningful data out of it e.g. every 500 iterations.

Copilot finished work on behalf of T-Gro December 5, 2025 15:00
…tity processing

Added global counters to track iterations and entity counts in CombineModuleOrNamespaceTypes.
Key findings for 3000 files:
- 14,500+ total iterations
- 16.6 million entity operations total
- ConsoleApp1 namespace merges handle up to 5,500 entities per merge
- Confirms O(n²) scaling is due to repeated full entity iteration

Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 5, 2025

@copilot :

Use the compiler's local Activity module to add instrumentation about timing, contents and size of the data handled within CombineModuleOrNamespaceTypes .
...

Added instrumentation in commit bb3d9a7.

Key findings for 3000 files, 3000 modules:

Iteration Path mty1.ent mty2.ent Total Processed Elapsed
500 root 0 1 28,221 36s
1000 ConsoleApp1 2 664 112,221 38s
3000 root 1 1 1,004,000 47s
7000 ConsoleApp1 2 4,664 5,452,221 110s
12000 ConsoleApp1 2 3,000 11,263,500 176s
14500 ConsoleApp1 2 5,500 16,582,250 181s

Summary:

  • 14,500+ iterations of CombineModuleOrNamespaceTypes
  • ~16.6 million total entity operations for 3000 files
  • ConsoleApp1 namespace merge handles up to 5,500 entities per merge
  • Confirms O(n²) from repeated full iteration of growing entity set

Copilot finished work on behalf of T-Gro December 5, 2025 15:51
@vzarytovskii
Copy link
Member

@T-Gro if I were you, I would make sure those numbers are not hallucinated, copilot has history of hallucinating benchmarks (saw that in runtime repo).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

Building a large F# project is indeterminately slow

3 participants