Bug 58992 - Slow culture specific comparison
Summary: Slow culture specific comparison
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib ()
Version: 5.4 (2017-06)
Hardware: PC All
: --- enhancement
Target Milestone: Future Release
Assignee: Bugzilla
Depends on:
Reported: 2017-08-24 01:49 UTC by Dinesh
Modified: 2017-09-25 13:45 UTC (History)
12 users (show)

Is this bug a regression?: ---
Last known good build:

Performance degradation when Linq Expressions are used for Xamarin Forms Android platform (308.76 KB, application/x-zip-compressed)
2017-08-24 01:49 UTC, Dinesh
repro project (166.75 KB, application/x-zip-compressed)
2017-09-13 21:16 UTC, Jimmy [MSFT]
profiler run output (1.50 MB, application/octet-stream)
2017-09-16 00:44 UTC, Ludovic Henry

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Dinesh 2017-08-24 01:49:52 UTC
Created attachment 24387 [details]
Performance degradation when Linq Expressions are used for Xamarin Forms Android platform

Hi Team,

I had experienced a strange performance degradation when expressions are used for iteration of elements in my application. Here, I am explaining the scenario below.

I have 25000 items as type of IEnumerable which is converted into IQueryable elements, in order to get the OrderBy expressions for further process. And adding those elements in a foreach loop into an another collection where I had noticed the delay to complete the iteration.

For UWP platform, the iteration gets completed with noticeable time and for iOS platform, it took additional time when compared to UWP platform. But for Android platform, there was an adorable time delay. Please find the time variation for different platforms below.

Platform	Time taken in millisecond
Android	13730.2799
iOS	2111.4957
UWP	366.8962

I used a Stopwatch to measure how many milliseconds it took to complete the iteration of elements in a button click event. I have attached the sample in the attachment for your reference.

Can you please let me know how do I overcome from this delay? Also, whether the time delay for iOS platform is noticeable or it is a bug? 

Please let us know if you require further assistance.

Dinesh Babu Yadav
Comment 1 Pannir Selvam 2017-09-12 04:51:23 UTC
Hi Team, Any update on this?
Comment 2 Jimmy [MSFT] 2017-09-13 21:16:11 UTC
Created attachment 24739 [details]
repro project

Because this happens even when not using Xamarin.Forms I'm inclined to say that this is a Xamarin.Android or Mono issue (similar to or possibly related to bug 56240) so I am reassigning the report.

I minimized the original repro project further and also added a basic console app. Iterating over a Queryable that uses a LINQ expression on Android, iOS, and Mono all took noticeably longer than on Windows.

# Test Results
I ran the operation 5 times without the debugger attached and average the times.

## Windows
    99 ms
    98 ms
    123 ms
    108 ms
    110 ms

    Avg: 107.6 ms

## Xamarin.Android
    1879 ms
    1730 ms
    1743 ms
    1756 ms
    1738 ms

    Avg: 1769.2 ms

## Xamarin.iOS
    757 ms
    753 ms
    752 ms
    755 ms
    753 ms

    Avg: 754 ms

## Mono (macOS)
    730 ms
    732 ms
    738 ms
    914 ms
    902 ms

    Avg: 803.2 ms

## Mono 4.8.1 (macOS)
    1034 ms
    1043 ms
    1042 ms
    1042 ms
    1059 ms

    Avg: 1044 ms

Because the performance on Mono 5.4 is still better compared to Mono 4.8 I don't believe this is a regression.
Comment 4 Ludovic Henry 2017-09-16 00:44:34 UTC
Created attachment 24787 [details]
profiler run output

I attached the profiler output of the Console project run with a list of 250k items (instead of 25k in the provided repro).

What can be observed from the profiler output is around 80% of the time is spent comparing strings, which is the core of the application, as it's trying to sort 250k strings (25k in the provided repro).

So the application might be slower on Mono compared to .NET due to faster string comparison on .NET.
Comment 5 Marek Safar 2017-09-25 08:04:03 UTC
This is down to culture-specific comparison which we know it's slower than .net

Changing the code to be

> query = source.OrderBy(TestItem => TestItem.Name, StringComparer.Ordinal);

shows we are slower but not that much