Skip to content

Conversation

@KSemenenko
Copy link
Member

Summary

  • replace the ZIP-based MIME sniffing logic with span-based ASCII comparisons to avoid repeated string allocations
  • reuse a shared separator array when parsing mime.types listings to eliminate per-line allocations

Testing

  • dotnet test

https://chatgpt.com/codex/tasks/task_e_68ea08250b3483269ee0369c9166f2aa

Copilot AI review requested due to automatic review settings October 11, 2025 15:49
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors MIME detection and introduces a sync tool to improve performance and data freshness. The changes replace allocation-heavy ZIP-based content sniffing with efficient span-based ASCII comparisons and implement reusable data structures for parsing mime.types listings.

Key changes:

  • Replaced dictionary-based MIME sniffing with span-based ASCII pattern matching to avoid string allocations
  • Added comprehensive content detection with magic signature support for common file types
  • Introduced sync tool for updating MIME database from upstream sources with curated overrides

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
README.md Updated documentation with comprehensive feature overview, usage examples, and installation instructions
ManagedCode.MimeTypes/mimeTypes.json Added new MIME mappings and reordered entries for consistency
ManagedCode.MimeTypes/MimeTypeCategory.cs Added new categories for Script, Binary, Multipart, and Message
ManagedCode.MimeTypes/MimeHelper.cs Major refactor with span-based content detection, reverse lookup API, and runtime registration
ManagedCode.MimeTypes/ManagedCode.MimeTypes.csproj Updated LangVersion to preview
ManagedCode.MimeTypes.sln Added MimeTypes.Sync project reference
ManagedCode.MimeTypes.Tests/ Added comprehensive test coverage for new functionality
ManagedCode.MimeTypes.Sync/ New console utility for synchronizing MIME database
ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs Enhanced generator with proper escaping and improved key parsing
.github/workflows/mime-sync.yml Added automated workflow for weekly MIME database updates

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

continue;
}

var parts = line.Split(MimeTypeSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

The MimeTypeSeparators array is allocated on each call to Split(). Consider using line.Split(' ', '\t', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries) directly or make the array static readonly at the class level to avoid repeated allocations.

Suggested change
var parts = line.Split(MimeTypeSeparators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var parts = line.Split(' ', '\t', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);

Copilot uses AI. Check for mistakes.
yield break;
}

var separatorIndex = trimmed.IndexOfAny(new[] { '?', '#' });
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

The char array new[] { '?', '#' } is allocated on each call. Consider making this a static readonly field to avoid repeated allocations.

Copilot uses AI. Check for mistakes.
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

The condition header.Length >= 12 is checked twice in succession. The inner check on line 573 is redundant since it's already verified by the outer condition on line 572.

Copilot uses AI. Check for mistakes.
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

Similar to the previous issue, the condition header.Length >= 12 is checked twice. The inner check on line 594 is redundant since it's already verified by the outer condition on line 592.

Copilot uses AI. Check for mistakes.
Comment on lines 64 to 78
const string extension = "customext";
const string mime = "application/x-custom";

try
{
MimeHelper.RegisterMimeType(extension, mime);
MimeHelper.GetMimeType($"file.{extension}").ShouldBe(mime);

MimeHelper.TryGetExtensions(mime, out var extensions).ShouldBeTrue();
extensions.ShouldContain($".{extension}");
}
finally
{
MimeHelper.UnregisterMimeType(extension).ShouldBeTrue();
MimeHelper.GetMimeType(extension).ShouldBe("application/octet-stream");
Copy link

Copilot AI Oct 11, 2025

Choose a reason for hiding this comment

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

[nitpick] Using const for these values is unnecessary since they're only used within this single method. Consider using var or regular string literals directly in the method calls for better readability.

Suggested change
const string extension = "customext";
const string mime = "application/x-custom";
try
{
MimeHelper.RegisterMimeType(extension, mime);
MimeHelper.GetMimeType($"file.{extension}").ShouldBe(mime);
MimeHelper.TryGetExtensions(mime, out var extensions).ShouldBeTrue();
extensions.ShouldContain($".{extension}");
}
finally
{
MimeHelper.UnregisterMimeType(extension).ShouldBeTrue();
MimeHelper.GetMimeType(extension).ShouldBe("application/octet-stream");
try
{
MimeHelper.RegisterMimeType("customext", "application/x-custom");
MimeHelper.GetMimeType($"file.{"customext"}").ShouldBe("application/x-custom");
MimeHelper.TryGetExtensions("application/x-custom", out var extensions).ShouldBeTrue();
extensions.ShouldContain($".{"customext"}");
}
finally
{
MimeHelper.UnregisterMimeType("customext").ShouldBeTrue();
MimeHelper.GetMimeType("customext").ShouldBe("application/octet-stream");

Copilot uses AI. Check for mistakes.
@coveralls
Copy link

coveralls commented Oct 11, 2025

Pull Request Test Coverage Report for Build 18431903005

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 373 of 501 (74.45%) changed or added relevant lines in 2 files are covered.
  • 154 unchanged lines in 3 files lost coverage.
  • Overall coverage increased (+4.9%) to 54.02%

Changes Missing Coverage Covered Lines Changed/Added Lines %
ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs 0 11 0.0%
ManagedCode.MimeTypes/MimeHelper.cs 373 490 76.12%
Files with Coverage Reduction New Missed Lines %
ManagedCode.MimeTypes.Generator/MimeTypeSourceGenerator.cs 1 0.0%
ManagedCode.MimeTypes/MimeHelper.cs 2 74.02%
ManagedCode.MimeTypes/obj/Generated/ManagedCode.MimeTypes.Generator/ManagedCode.MimeTypes.Generator.MimeTypeSourceGenerator/MimeHelper.Properties.cs 151 50.28%
Totals Coverage Status
Change from base Build 16629812820: 4.9%
Covered Lines: 1678
Relevant Lines: 3155

💛 - Coveralls

@KSemenenko
Copy link
Member Author

@codex fix all comments

@chatgpt-codex-connector
Copy link

Summary

Testing

  • ⚠️ dotnet test (fails: command not found in container)

View task →

KSemenenko and others added 3 commits October 11, 2025 18:17
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@KSemenenko KSemenenko merged commit 8c5b402 into main Oct 11, 2025
1 check passed
@KSemenenko KSemenenko deleted the codex/analyze-project-and-provide-improvement-recommendations-6j8r71 branch October 11, 2025 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants