Skip to content

Optimization suggestion #2013

Description

@oswaldsql

Considder updating the code in src/coverlet.core/Coverage.cs [Line 203 - L278] (GetCoverageResult) to something like the following.

Should reduce the number of dictionary lookups from a minimum of 9 (maximum 14) to a constant of 5.

public CoverageResult GetCoverageResult()
{
    this.CalculateCoverage();

    var modules = new Modules();
    foreach (var result in this._results)
    {
        var documents = new Documents();
        foreach (var doc in result.Documents.Values)
        {
            if (!documents.TryGetValue(doc.Path, out var classes))
            {
                classes = new();
                documents.Add(doc.Path, classes);
            }

            // Construct Line Results
            foreach (var line in doc.Lines.Values)
            {
                if (!classes.TryGetValue(line.Class, out var methods))
                {
                    methods = new();
                    classes.Add(line.Class, methods);
                }

                if (!methods.TryGetValue(line.Method, out var method))
                {
                    method = new();
                    methods.Add(line.Method, method);
                }

                method.Lines.Add(line.Number, line.Hits);
            }

            // Construct Branch Results
            foreach (var branch in doc.Branches.Values)
            {
                var branchInfo = new BranchInfo()
                {
                    Line = branch.Number,
                    Hits = branch.Hits,
                    Offset = branch.Offset,
                    EndOffset = branch.EndOffset,
                    Path = branch.Path,
                    Ordinal = branch.Ordinal
                };

                if (!classes.TryGetValue(branch.Class, out var methods))
                {
                    methods = new();
                    classes.Add(branch.Class, methods);
                }

                if (!methods.TryGetValue(branch.Method, out var method))
                {
                    method = new();
                    methods.Add(branch.Method, method);
                }

                method.Branches.Add(branchInfo);
            }
        }

        modules.Add(Path.GetFileName(result.ModulePath), documents);
        this._instrumentationHelper.RestoreOriginalModule(result.ModulePath, this.Identifier);
    }

... The rest of the method goes here

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions