mfogliatto / mfogliatto/ReferenceCop

[Performance] XmlConfigurationLoader creates new XmlSerializer on every instantiation

Open Beginner friendly
#50 0 comments 0 reactions 0 assignees View on GitHub
performance
Dominant language
C#
Stars
1
Forks
2
PR merge metrics
No merged PRs in 30d

Description

## Description

`XmlConfigurationLoader.ParseConfigFrom()` creates a new `XmlSerializer(typeof(ReferenceCopConfig))` each time it's called. `XmlSerializer` constructors dynamically generate and compile a serialization assembly at runtime.

## Affected Files

- `src/ReferenceCop/Configuration/XmlConfigurationLoader.cs` (line ~48, `ParseConfigFrom` method)

## Impact

- **Startup cost**: Each `new XmlSerializer()` triggers runtime code generation (~50-200ms for complex types), adding latency during analyzer initialization
- **Memory leak in .NET Framework**: Dynamically generated assemblies are never unloaded in .NET Framework, causing a slow memory leak if the analyzer is invoked repeatedly (e.g., in IDE real-time analysis scenarios)
- **Repeated allocation**: In MSBuild/Roslyn scenarios where the analyzer initializes per-project, this compounds across the build

## Suggested Optimization

Cache the serializer as a static field:

```csharp
private static readonly XmlSerializer Serializer = new XmlSerializer(typeof(ReferenceCopConfig));

private static ReferenceCopConfig ParseConfigFrom(Stream stream)
{
return (ReferenceCopConfig)Serializer.Deserialize(stream);
}
```

The `XmlSerializer(Type)` constructor is thread-safe for the simple single-type form, and the cached instance can be safely shared.

Contributor guide

Open the contributing guide

Research direction

Open src/ReferenceCop/Configuration/XmlConfigurationLoader.cs and inspect ParseConfigFrom around line 48, then confirm how often the serializer is created. Verify that repeated parsing reuses the same serializer and that deserialization behavior remains unchanged; the issue does not name a specific test to run.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
performance, tooling
Issue type
Refactor
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
85/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.