Repository navigation
Description
I'm sorting several thousand strings in my App (Windows, Android, iOS), but on iOS it is SIGNIFICANTLY slower than on other platforms. (not running NativeAOT on iOS yet)
source.OrderBy(item => GetNaturalSortKey(keySelector(item)), StringComparer.CurrentCultureIgnoreCase) - for the same data set is taking several ms/ tens ms, on iOS it's taking more than 600ms. Delays in UI etc. on iOS. So we had to #if def on iOS platform for own written natural sort which is faster (on iOS) that simple linq.
I think it's similar to #112123
Maybe things like getting current thread culture for comparison on iOS is taking too long or something like that?
I think it's a regression because with Xamarin.iOS (not sure about net7-ios) we had no issues with performance with part of code.
Reproducible on net9-ios, net10-ios (rc2). Not reproducible on Windows or Android (net5-10)
public static IOrderedEnumerable<T> OrderByNatural<T>(this IEnumerable<T> source, Func<T, string> keySelector)
{
#if IOS
return source.OrderBy(item => GetNaturalSortKey(keySelector(item)), StringComparer.CurrentCultureIgnoreCase);
#else
return source.OrderBy(keySelector, new CustomComparer<string>(NaturalComparer.Compare));
#endif
}
#if IOS
private static readonly StringBuilder stringBuilderName = new();
private static readonly StringBuilder stringBuilderNumber = new();
private static string GetNaturalSortKey(string name)
{
if (string.IsNullOrEmpty(name))
return string.Empty;
// Fast path: if no digits, return original string
bool hasDigits = false;
for (int i = 0; i < name.Length; i++)
{
if (char.IsDigit(name[i]))
{
hasDigits = true;
break;
}
}
if (!hasDigits)
return name;
stringBuilderName.Clear();
stringBuilderNumber.Clear();
foreach (char c in name)
{
if (char.IsDigit(c))
{
stringBuilderNumber.Append(c);
}
else
{
if (stringBuilderNumber.Length > 0)
{
// Pad numbers to 10 digits so they sort naturally
stringBuilderName.Append(stringBuilderNumber.ToString().PadLeft(10, '0'));
stringBuilderNumber.Clear();
}
stringBuilderName.Append(c);
}
}
if (stringBuilderNumber.Length > 0)
{
stringBuilderName.Append(stringBuilderNumber.ToString().PadLeft(10, '0'));
}
return stringBuilderName.ToString();
}
#endif
Activity
michaldobrodenka commentedon Oct 30, 2025
objects like this:
public class Location { public Guid LocationId { get; set; } public string Name { get; set; } };sorting:
var sortedLocationsOriginal = LocationData.Locations.OrderBy(l => l.Name, new CustomComparer<string>(NaturalComparer.Compare)).ToList();Android S23 Ultra - is about 5x faster with large dataset than iPhone 15 (which has faster CPU)
var sortedLocationsOrdinary = LocationData.Locations.OrderBy(l => l.Name).ToList();Android S23 Ultra - 7x faster than iPhone.
There is something fishy with string sorting on iOS. I tried on iOS Release, Debug, (obviosly), NativeAOT etc
dotnet-policy-service commentedon Oct 30, 2025
Tagging subscribers to 'os-ios': @vitek-karas, @kotlarmilos, @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.
dotnet-policy-service commentedon Oct 30, 2025
Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.
michaldobrodenka commentedon Oct 31, 2025
some other guy, similar problem from discord https://discord.com/channels/732297728826277939/732297808148824115/1047865263795744820 similar issue:
"Still in the process of converting a large Xamarin.iOS app to net7-ios and I stumbled on a significant performance regression with Array.Sort when relying on items implementing IComparable<>. The issue seem to go away when using an explicit IComparer (or Comparaison delegate)
Is this something that is known? Just wanted to check here before digging further and trying to build a simple repro case."
dotnet-policy-service commentedon Jul 7, 2026
Tagging subscribers to this area: @dotnet/area-system-globalization
See info in area-owners.md if you want to be subscribed.
[iOS] Optimize CompareStringNative performance
[iOS] Optimize CompareStringNative performance
[iOS] Optimize CompareStringNative performance
[iOS] Optimize CompareStringNative performance
[iOS] Optimize CompareStringNative performance (#130691)
matouskozak commentedon Jul 20, 2026
Partial improvement to the iOS compare string code has been merged #130691. It's likely that it's not fully resolving the regression caused by switch to Foundation-based instead of direct ICU-based sorting but I hope that with other .NET 11 changes, the performance should be closer.