Skip to content

Optimize hot paths: cache NuGetFramework parsing, avoid string allocations in hash computation#14

Draft
Copilot wants to merge 3 commits intomainfrom
copilot/improve-slow-code-performance
Draft

Optimize hot paths: cache NuGetFramework parsing, avoid string allocations in hash computation#14
Copilot wants to merge 3 commits intomainfrom
copilot/improve-slow-code-performance

Conversation

Copy link
Contributor

Copilot AI commented Nov 29, 2025

Identified and fixed performance bottlenecks in package version checking and hash computation paths.

Changes

  • Cache parsed NuGetFramework instances (NuGetMetadataService.cs)
    • CompatibleTargetFrameworksTyped was re-parsing frameworks on every access
    • Now uses Lazy<T> for thread-safe caching
// Before: O(n) parsing on every property access
public IEnumerable<NuGetFramework> CompatibleTargetFrameworksTyped => 
    CompatibleTargetFrameworks.Select(tf => NuGetFramework.Parse(tf));

// After: parse once, cache result
private Lazy<IReadOnlyList<NuGetFramework>>? _compatibleTargetFrameworksTyped;
public IReadOnlyList<NuGetFramework> CompatibleTargetFrameworksTyped => 
    (_compatibleTargetFrameworksTyped ??= new Lazy<IReadOnlyList<NuGetFramework>>(() => 
        CompatibleTargetFrameworks.Select(tf => NuGetFramework.Parse(tf)).ToList())).Value;
  • Use Count property instead of LINQ Count() (OutdatedService.cs)

    • Direct property access on HashSet<T> vs extension method
  • Eliminate string allocations in hash computation (OutdatedService.cs)

    • Replaced str?.ToLowerInvariant().GetHashCode() with StringComparer.OrdinalIgnoreCase.GetHashCode(str)
    • Avoids allocating lowercase string copies per hash call
Original prompt

Identify and suggest improvements to slow or inefficient code


💡 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 November 29, 2025 11:28
…, optimize hash code

Co-authored-by: dlosch <318550+dlosch@users.noreply.github.com>
Co-authored-by: dlosch <318550+dlosch@users.noreply.github.com>
Copilot AI changed the title [WIP] Identify and improve inefficient code performance Optimize hot paths: cache NuGetFramework parsing, avoid string allocations in hash computation Nov 29, 2025
Copilot AI requested a review from dlosch November 29, 2025 11:34
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