Skip to content

[iOS] Slow string sorting compared to other platforms #121204

Closed

Description

@michaldobrodenka

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

  1. added
    tenet-performance
    on Oct 30, 2025
  2. added
    untriaged
    on Oct 30, 2025
  3. added
    needs-area-label
    on Oct 30, 2025
  4. michaldobrodenka commented on Oct 30, 2025

    @michaldobrodenka
    Author

    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

  5. added
    os-ios
    and removed
    needs-area-label
    on Oct 30, 2025
  6. dotnet-policy-service commented on Oct 30, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to 'os-ios': @vitek-karas, @kotlarmilos, @steveisok, @akoeplinger
    See info in area-owners.md if you want to be subscribed.

  7. dotnet-policy-service commented on Oct 30, 2025

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-runtime
    See info in area-owners.md if you want to be subscribed.

  8. michaldobrodenka commented on Oct 31, 2025

    @michaldobrodenka
    Author

    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."

  9. removed
    untriaged
    on Nov 4, 2025
  10. added this to the 11.0.0 milestone on Nov 4, 2025
  11. dotnet-policy-service commented on Jul 7, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-globalization
    See info in area-owners.md if you want to be subscribed.

  12. added 5 commits that reference this issue on Jul 14, 2026
    ef31e85
    cad4e8c
    ce9fefe
    9f25b0e
    630a700
  13. matouskozak commented on Jul 20, 2026

    @matouskozak
    Member

    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.

  14. locked and limited conversation to collaborators on Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-System.Globalizationos-iostenet-performance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions