Skip to content

Conversation

@h3xds1nz
Copy link
Member

@h3xds1nz h3xds1nz commented Jul 22, 2024

Description

Decreases allocations while enumerating suggestions in Speller. Further improves performance, anywhere from 1.5 to 2.5.

  • Replace ArrayList with List<string>.
  • Replace custom iteration with the base implementation.
  • Raises base allocation of items from 1 to 4 as in most cases the number of suggestions more than 1.
  • The return type could be an IList<string> or IEnumerable<string> but since it is internal I didn't bother.

1 suggestion (worst case, uncommon)

Method Mean [ns] Error [ns] StdDev [ns] Gen0 Code Size [B] Allocated [B]
Original 21.56 ns 0.345 ns 0.288 ns 0.0072 1,415 B 120 B
PR__EDIT 14.20 ns 0.292 ns 0.300 ns 0.0076 1,531 B 128 B

2 suggestions

Method Mean [ns] Error [ns] StdDev [ns] Gen0 Code Size [B] Allocated [B]
Original 38.18 ns 0.292 ns 0.273 ns 0.0095 1,910 B 160 B
PR__EDIT 18.31 ns 0.269 ns 0.251 ns 0.0076 1,594 B 128 B

3 suggestions

Method Mean [ns] Error [ns] StdDev [ns] Gen0 Code Size [B] Allocated [B]
Original 52.59 ns 0.659 ns 0.584 ns 0.0129 2,078 B 216 B
PR__EDIT 21.20 ns 0.213 ns 0.178 ns 0.0076 1,661 B 128 B

6 suggestions

Method Mean [ns] Error [ns] StdDev [ns] Gen0 Code Size [B] Allocated [B]
Original 82.26 ns 1.224 ns 1.085 ns 0.0181 2,874 B 304 B
PR__EDIT 55.22 ns 0.851 ns 0.754 ns 0.0129 1,076 B 216 B

8 suggestions

Method Mean [ns] Error [ns] StdDev [ns] Gen0 Code Size [B] Allocated [B]
Original 99.92 ns 0.714 ns 0.557 ns 0.0181 3,032 B 304 B
PR__EDIT 58.23 ns 0.838 ns 0.743 ns 0.0129 1,160 B 216 B

9 suggestions

Method Mean [ns] Error [ns] StdDev [ns] Gen0 Code Size [B] Allocated [B]
Original 125.89 ns 1.450 ns 1.356 ns 0.0272 3,056 B 456 B
PR__EDIT 78.59 ns 1.368 ns 1.279 ns 0.0219 1,209 B 368 B
Benchmark code
[Benchmark]
public void Original()
{
    foreach (string suggestion in Suggestions)
        EmptyMethod(suggestion);
}

[Benchmark]
public void PR__EDIT()
{
    foreach (string suggestion in SuggestionsV2)
        EmptyMethod(suggestion);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private int EmptyMethod(string suggestions) => suggestions[0];

public IEnumerable<string> Suggestions
{
    get
    {
        IList suggestions = _speller.GetSuggestionsForError();

        for (int i = 0; i < suggestions.Count; i++)
        {
            yield return (string)suggestions[i];
        }
    }
}

public IEnumerable<string> SuggestionsV2
{
    get => _speller.GetSuggestionsForErrorV2();
}

private readonly Speller _speller = new();

public class Speller
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public IList GetSuggestionsForError()
    {
        ArrayList suggestions = new ArrayList(1);

        suggestions.Add("hannah");
        suggestions.Add("lannal");
        suggestions.Add("hannah");

        suggestions.Add("hannah");
        suggestions.Add("lannal");
        suggestions.Add("hannah");

        suggestions.Add("lannal");
        suggestions.Add("hannah");

        suggestions.Add("hannah");

        return suggestions;
    }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public List<string> GetSuggestionsForErrorV2()
    {
        List<string> suggestions = new(4);

        suggestions.Add("hannah");
        suggestions.Add("lannal");
        suggestions.Add("hannah");

        suggestions.Add("hannah");
        suggestions.Add("lannal");
        suggestions.Add("hannah");

        suggestions.Add("lannal");
        suggestions.Add("hannah");

        suggestions.Add("hannah");

        return suggestions;
    }
}

Customer Impact

Improved performance, decrease allocations.

Regression

No.

Testing

Local build, verifying Speller/Suggestions functionality.

Risk

Low.

Microsoft Reviewers: Open in CodeFlow

@h3xds1nz h3xds1nz requested review from a team as code owners July 22, 2024 21:06
@dotnet-policy-service dotnet-policy-service bot added PR metadata: Label to tag PRs, to facilitate with triage Community Contribution A label for all community Contributions labels Jul 22, 2024
@rchauhan18
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@harshit7962
Copy link
Member

@h3xds1nz Thank you for your contributions.

@h3xds1nz
Copy link
Member Author

h3xds1nz commented Oct 1, 2024

@harshit7962 Thank you.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 1, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Community Contribution A label for all community Contributions PR metadata: Label to tag PRs, to facilitate with triage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants