Skip to content

[Performance] XmlSerializer instantiated per-call without caching #56

@mfogliatto

Description

@mfogliatto

Description

In XmlConfigurationLoader.ParseConfigFrom(), a new XmlSerializer(typeof(ReferenceCopConfig)) is created every time a config is parsed. XmlSerializer constructor dynamically generates and compiles serialization assemblies, which is expensive (~50-200ms) and causes memory leaks through assembly loading if called repeatedly.

Affected File

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

Impact

  • Medium. In a Roslyn analyzer context, this is invoked once per compilation. However:
    • In IDE scenarios (VS live analysis), compilations happen frequently on every keystroke
    • The generated assembly is never unloaded, causing gradual memory growth
    • First-compilation startup latency is needlessly high

Suggested Optimization

Cache the serializer as a static field (XmlSerializer for a given type is thread-safe for deserialization):

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

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

This eliminates repeated assembly generation and reduces analyzer startup time.

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