Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve PrependArticleSuffix perf #1419

Closed
wants to merge 7 commits into from

Conversation

SimonCropp
Copy link
Collaborator

v2.14.1

Method Mean Error StdDev Allocated
PrependArticleSuffix 426.9 ns 7.07 ns 6.61 ns 1.16 KB

v3.0.0

Method Mean Error StdDev Allocated
PrependArticleSuffix 152.8 ns 1.26 ns 1.18 ns 312 B

@@ -48,60 +48,68 @@ public static string[] PrependArticleSuffix(string[] appended)
{
var inserted = new string[appended.Length];

// ReSharper disable InconsistentNaming
var The = "The".AsSpan();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The could be _The[1..], etc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it seems that The and others are not needed at all.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good idea. i applied the _The[1..]

are not needed at all.

they are used. we could use _The[1..] but the variables make the below code a little more readable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that they are used:

if (append.EndsWith(The))
{
    var suffix = append[append.IndexOf(_The, StringComparison.CurrentCulture)..];
    inserted[i] = ToOriginalFormat(append, suffix);
}

But would not be that just an equivalent of the following?

if (append.EndsWith(_The))
{
    var suffix = append[append.IndexOf(_The, StringComparison.CurrentCulture)..];
    inserted[i] = ToOriginalFormat(append, suffix);
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also... I think the code is not entirely correct:

append.IndexOf(_The, StringComparison.CurrentCulture) will find a first index of " The" instead of last. I think something like "The General Theory of Relativity" would be completely destroyed by this process.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Expected: ["The General Theory of Relativity"]
Actual: ["Theory of Relativity The General"]

@SimonCropp SimonCropp marked this pull request as draft February 21, 2024 03:29
suffix = append.Substring(append.IndexOf(" The", StringComparison.CurrentCulture));
original = ToOriginalFormat(appended, suffix, i);
inserted[i] = original;
var suffix = append[append.IndexOf(_The, StringComparison.CurrentCulture)..];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. suffix here is known to be " The". It does not make sense to try to extract it from the string.
  2. append.IndexOf(_The, StringComparison.CurrentCulture) should be append.LastIndexOf(_The, StringComparison.CurrentCulture). Which is known to be append.Length - " The".Length
  3. ToOriginalFormat(append, suffix) again does the same operation append.IndexOf(suffix, StringComparison.CurrentCulture)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants