Skip to content

[Performance] XmlSerializer instantiation on every config load #53

@mfogliatto

Description

@mfogliatto

Description

XmlConfigurationLoader.ParseConfigFrom() creates a new XmlSerializer(typeof(ReferenceCopConfig)) on every call. XmlSerializer construction is expensive — it generates and compiles serialization assemblies at runtime.

Affected File

src/ReferenceCop/Configuration/XmlConfigurationLoader.cs — line in ParseConfigFrom() method.

Impact

  • The Roslyn analyzer runs during compilation, potentially being invoked for every project in a solution.
  • Each invocation pays the cost of XmlSerializer construction (reflection + dynamic code generation).
  • In large solutions with many projects, this adds measurable overhead to build times.

Suggested Optimization

Cache the XmlSerializer instance in a static field:

private static readonly XmlSerializer ConfigSerializer = new XmlSerializer(typeof(ReferenceCopConfig));

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

XmlSerializer is thread-safe for deserialization, so a shared static instance is safe here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    performancePerformance optimization

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions