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

Huge performance difference in string.StartsWith and EndsWith between Linux and Windows for non Invariant, non en-US. #6074

Closed
jskeet opened this issue Jun 8, 2016 · 22 comments
Assignees
Labels
area-System.Globalization enhancement Product code improvement that does NOT require public API changes/additions tenet-performance Performance related issue
Milestone

Comments

@jskeet
Copy link

jskeet commented Jun 8, 2016

Very simple code:

using System;
using System.Diagnostics;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Time(1);
            Time(1000000);
        }

        private static void Time(int iterations)
        {
            var stopwatch = Stopwatch.StartNew();
            for (int i = 0; i < iterations; i++)
            {
                "abcfeg".EndsWith("123");
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.Elapsed);
        }
    }
}

The project.json is mostly the default from dotnet new:

{
  "buildOptions": {
    "emitEntryPoint": true,
    "optimize": true
  },
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0-*"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

On both Linux (Ubuntu 15.10 and Ubuntu 16.04) the output is something like:

00:00:00.0003456
00:00:05.2664877

On Windows, on the same hardware, it's:

00:00:00.0000506
00:00:00.1633352

Version info: .NET Command Line Tools (1.0.0-preview1-002702) on Windows and Ubuntu 15.10; 1.0.0-preview2-002886 on Ubuntu 16.04.

Note that under NUnit, every equality assertion involves three EndsWith calls, making NUnit assertions basically horrifically expensive on Linux...

@stephentoub
Copy link
Member

stephentoub commented Jun 8, 2016

cc: @ellismg

Interesting. I don't have that exact set up handy, but on Ubuntu 14.04 with CLI 1.0.0-preview2-002853 and relatively recent rc3 builds, I see:

stoub@stoublinux2:~/tmp3$ dotnet run
Project tmp3 (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
00:00:00.0002493
00:00:00.0464325
stoub@stoublinux2:~/tmp3$ dotnet run
Project tmp3 (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
00:00:00.0002399
00:00:00.0572944
stoub@stoublinux2:~/tmp3$ dotnet run
Project tmp3 (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
00:00:00.0002480
00:00:00.0502375
stoub@stoublinux2:~/tmp3$ dotnet run
Project tmp3 (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
00:00:00.0004543
00:00:00.0643471
stoub@stoublinux2:~/tmp3$ dotnet run
Project tmp3 (.NETCoreApp,Version=v1.0) was previously compiled. Skipping compilation.
00:00:00.0002738
00:00:00.0627142

so much faster than what you're seeing, and actually faster than what I'm seeing on the Windows host, e.g.

00:00:00.0076654
00:00:00.1033137

But... my LANG is en-US. If I do:

export LANG=en-GB

and then run it again, I get numbers much closer to yours:

00:00:00.0002939
00:00:04.8001375

Not a real solution, but as an experiment, @jskeet, if you change your LANG to en-US, do you see a perf improvement?

@ellismg
Copy link
Contributor

ellismg commented Jun 8, 2016

If the problem is the LANG setting, then what's happening is that our fast paths for ASCII StartsWith/EndsWith are specific today to en-US and Invariant (see the code here.)

Unlike Windows, having to do a full linguistic StartsWith or EndsWith is slow because we have to construct some ICU searching objects which we can't cache across runs (since the object is specific to a target string you are searching for, and we don't maintain a cache of searcher objects).

For 1.1 we should look at expanding the fast path to work for ASCII strings if the collation rules for the current locale don't tailor anything in the ASCII range, which would allow this fast path to be hit for locales like en-GB.

We could also consider trying to re-implement IndexOf in terms of some lower level ICU primitives that we might be able to cache across calls.

@jskeet
Copy link
Author

jskeet commented Jun 8, 2016

@stephentoub: Bingo! Yes, with

$ LANG=en-US dotnet run

I get:

00:00:00.0003087
00:00:00.0630077

Applying the same workaround to running my Noda Time tests halves the total test time, too... (That's where all this started.)

@ellismg
Copy link
Contributor

ellismg commented Jun 8, 2016

@jskeet Another option (and I'm not sure if this is possible via the interfaces NUnit exposes, a quick glance over the source doesn't give me much hope) is to use Ordinal or OrdinalIgnoreCase, which will ignore all the ICU gunk.

@jskeet
Copy link
Author

jskeet commented Jun 8, 2016

@ellismg: The checks here are deep in the bowels of NUnit - but could be fixed very easily with a patch. It's unfortunate that it's necessary, but it feels like a good practical solution to a very real issue. Will file a feature request now...

@ellismg
Copy link
Contributor

ellismg commented Jun 8, 2016

@jskeet Thanks! I very much expect we will do the extension of the ASCII fast paths for 1.1, but we may be in a world where linguistic StartsWith and EndsWith are slower for non ASCII strings or strings where collation for ASCII characters differs (e.g tr-TR) because of how this stuff is implemented in terms of ICU, so if we can upstream general goodness changes to force ordinal comparisions when you don't need linguistic behavior that would be great.

@ellismg ellismg changed the title Huge performance difference in string.EndsWith between Linux and Windows Huge performance difference in string.EndsWith between Linux and Windows for non Invariant, non en-US. Jun 8, 2016
@jskeet
Copy link
Author

jskeet commented Jun 8, 2016

Yup. Would be good if every call to Assert.AreEqual didn't need to do so much work in general, but that's a matter for another day :) Fingers crossed we can get this into NUnit quickly - it seems about as trivial a fix as one could wish for given the crazy perf benefits. (Admittedly Noda Time may be slightly unusual in its number of assertions...)

@wpostma
Copy link

wpostma commented Jun 9, 2016

Given how disgusting and unexpected such ICU regressions are likely to be, could there be a syntax added to .net core that lets you specify ie StartsWith(arg, forceAscii:true) ?

@jskeet
Copy link
Author

jskeet commented Jun 9, 2016

@wpostma: What would forceAscii do? There's already string.StartsWith(string, StringComparison) so you can specify an ordinal comparison, which is precisely what I'm proposing in NUnit.

@wpostma
Copy link

wpostma commented Jun 9, 2016

Um, forget I suggested that. :-)

@gkhanna79
Copy link
Member

@ellismg Is this actionable for 1.1.0?

@mazong1123
Copy link
Contributor

Anyone working on this issue? If not I have time to take a look.

@janvorli
Copy link
Member

@mazong1123 that would be great! Thank you. No-one is assigned, so that means no-one is working on this. I've just assigned you.

@mazong1123
Copy link
Contributor

mazong1123 commented Aug 20, 2017

Today I investigate a little bit of the related source code. Just put my learning here:

There're two path for the EndsWith comparison:

@danmoseley
Copy link
Member

@mazong1123 have you had a chance to look further?

@mazong1123
Copy link
Contributor

@danmosemsft sorry I'm afraid I don't have so much time for working on this issue right now. I'm busy with my day job and for .NET Core I'm only focusing on dotnet/coreclr#14646

However, I can support anyone who wants to work on this issue in my spare time.

@ghost
Copy link

ghost commented Mar 13, 2018

I have noticed this issue when running netcore code along side mono:

var x = System.IO.File.ReadAllText("/tmp/test/random25charstring.txt");
for (int i=0; i< 900000; i++) {
    if (x.StartsWith("notlong/")) { Console.WriteLine("Yes");}

The above code takes 6 seconds on my PC with 2.1.300-preview1-008174, and 370 milliseconds on mono. Interestingly, when I change my LANG to en-US, it takes 7 seconds (my default is en_AU.UTF-8).

The fix as mentioned above is to add Ordinal as the string comparison option:

if (x.StartsWith("notlong/",StringComparison.Ordinal))

This brings it back down to an acceptable 31 milliseconds on my PC.

@danmoseley danmoseley changed the title Huge performance difference in string.EndsWith between Linux and Windows for non Invariant, non en-US. Huge performance difference in string.StartsWith and EndsWith between Linux and Windows for non Invariant, non en-US. Jan 31, 2019
@msftgits msftgits transferred this issue from dotnet/coreclr Jan 30, 2020
@msftgits msftgits added this to the Future milestone Jan 30, 2020
@mikes-gh
Copy link

mikes-gh commented Feb 13, 2020

Just scratched my head for hours on this. Was running a console app with a 30,000 record iteration with multiple string.EndsWith checks. on windows it was fine on linux it killed the CPU and was much slower.

CentOS 7.7-1908

# echo $LANG
en_GB.UTF-8
# 

@adamsitnik
Copy link
Member

@mikes-gh which version of .NET Core are you using?

@mikes-gh
Copy link

<TargetFramework>netcoreapp3.1</TargetFramework>

# dotnet --info
  It was not possible to find any installed .NET Core SDKs
  Did you mean to run .NET Core SDK commands? Install a .NET Core SDK from:
      https://aka.ms/dotnet-download

Host (useful for support):
  Version: 3.1.1
  Commit:  a1388f194c

.NET Core SDKs installed:
  No SDKs were found.

.NET Core runtimes installed:
  Microsoft.AspNetCore.App 3.1.1 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.2.8 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.1 [/usr/share/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET Core runtimes or SDKs:
  https://aka.ms/dotnet-download
# 

@maryamariyan maryamariyan added the untriaged New issue has not been triaged by the area owner label Feb 26, 2020
@tarekgh tarekgh removed the untriaged New issue has not been triaged by the area owner label Jun 17, 2020
@iSazonov
Copy link
Contributor

I think the issue should be re-reviewed since we moved to ICU on Windows.

@adamsitnik
Copy link
Member

dotnet/coreclr#26759 and dotnet/coreclr#26621 combined together have fixed this issue. The PRs were merged in September and October 2019 so they did not make it to .NET Core 3.1, but they are part of the 5.0 release.

I've run these benchmarks from the dotnet/performance repository to verify that:

git clone https://github.com/dotnet/performance
cd performance
python3 ./scripts/benchmarks_ci.py -f netcoreapp3.1 --filter '*StringSearch*Is*'
python3 ./scripts/benchmarks_ci.py -f netcoreapp5.0 --filter '*StringSearch*Is*'
BenchmarkDotNet=v0.12.1, OS=ubuntu 18.04
Intel Xeon CPU E5-1650 v4 3.60GHz, 1 CPU, 12 logical and 6 physical cores
.NET Core SDK=5.0.100-preview.6.20318.15
  [Host]     : .NET Core 3.1.5 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.27001), X64 RyuJIT
  Job-OKAAXU : .NET Core 3.1.5 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.27001), X64 RyuJIT
  Job-QELOCB : .NET Core 5.0.0 (CoreCLR 5.0.20.30506, CoreFX 5.0.20.30506), X64 RyuJIT

Method Runtime Options Mean Ratio Allocated
IsPrefix_FirstHalf .NET Core 3.1 (pl-PL, None, False) 17,821.369 ns 1.00 -
IsPrefix_FirstHalf .NET Core 5.0 (pl-PL, None, False) 3,723.045 ns 0.21 -
IsPrefix_DifferentFirstChar .NET Core 3.1 (pl-PL, None, False) 30,629.231 ns 1.000 -
IsPrefix_DifferentFirstChar .NET Core 5.0 (pl-PL, None, False) 287.086 ns 0.009 -
IsSuffix_SecondHalf .NET Core 3.1 (pl-PL, None, False) 19,515.456 ns 1.00 -
IsSuffix_SecondHalf .NET Core 5.0 (pl-PL, None, False) 5,292.733 ns 0.27 -
IsSuffix_DifferentLastChar .NET Core 3.1 (pl-PL, None, False) 35,581.039 ns 1.00 -
IsSuffix_DifferentLastChar .NET Core 5.0 (pl-PL, None, False) 385.417 ns 0.01 -

The pl-PL culture is just sample culture that is not using the en* cultures execution path. For cases where there is a match, the code is on average 4-5 times faster. For cases where there is a mismatch in first|last character, the code is up to 100 times faster.

@adamsitnik adamsitnik removed Hackathon Issues picked for Hackathon help wanted [up-for-grabs] Good issue for external contributors labels Jul 8, 2020
@adamsitnik adamsitnik modified the milestones: Future, 5.0.0 Jul 8, 2020
@adamsitnik adamsitnik self-assigned this Jul 8, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Globalization enhancement Product code improvement that does NOT require public API changes/additions tenet-performance Performance related issue
Projects
None yet
Development

No branches or pull requests