Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9222407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:49:46+00:00 2026-06-18T03:49:46+00:00

I’m having to write an immediate mode implementation of Linq (due to memory allocation

  • 0

I’m having to write an “immediate” mode implementation of Linq (due to memory allocation restrictions on Unity/Mono – long story, not really important).

I’m fine with everything performing as fast as or faster than real Linq until I come to ThenBy. Clearly my method for applying this is flawed as my performance drops to 4x slower that the real deal.

So what I’m doing right now is –

For each OrderBy, ThenBy clause

  • Create a list of the for the results of each selector, add all of the results of the selector evaluation to the list
  • Create a lambda that uses the default comparer which uses the list indexed off the two parameters

It looks like this:

public static IEnumerable<T> OrderByDescending<T,TR>(this IEnumerable<T> source, Func<T,TR> clause, IComparer<TR> comparer = null)
{
    comparer = comparer ?? Comparer<TR>.Default;
    var linqList = source as LinqList<T>;
    if(linqList == null)
    {
        linqList = Recycler.New<LinqList<T>>();
        linqList.AddRange(source);
    }
    if(linqList.sorter!=null)
        throw new Exception("Use ThenBy and ThenByDescending after an OrderBy or OrderByDescending");
    var keys = Recycler.New<List<TR>>();
    keys.Capacity = keys.Capacity > linqList.Count ? keys.Capacity : linqList.Count;
    foreach(var item in source)
    {
        keys.Add(clause(item));
    }
    linqList.sorter = (x,y)=>-comparer.Compare(keys[x],keys[y]);
    return linqList;


}

public static IEnumerable<T> ThenBy<T,TR>(this IEnumerable<T> source, Func<T,TR> clause, IComparer<TR> comparer = null)
{
    comparer = comparer ?? Comparer<TR>.Default;
    var linqList = source as LinqList<T>;
    if(linqList == null || linqList.sorter==null)
    {
        throw new Exception("Use OrderBy or OrderByDescending first");
    }
    var keys = Recycler.New<List<TR>>();
    keys.Capacity = keys.Capacity > linqList.Count ? keys.Capacity : linqList.Count;
    foreach(var item in source)
    {
        keys.Add(clause(item));
    }
    linqList.sorters.Add((z,x,y)=>z != 0 ? z : comparer.Compare(keys[x],keys[y]));
    return linqList;


}

Then what I do in the sort function is create a lamda that applies the sorts in order – so I end up with a function that looks like a Comparer<int> and returns the correct ordering.

It starts this really poor performance. I’ve tried version using currying and different signatures for OrderBy and ThenBy functions, but nothing is really working any faster and I’m wondering if I’m just missing a trick about multikey sorting.

The sort variables and function:

    public List<Func<int,int,int,int>> sorters = new List<Func<int, int, int, int>>();
    public Func<int,int,int> sorter;
    public List<int> sortList = new List<int>();
    bool sorted;
    private List<T> myList = new List<T>();

    void ResolveSorters()
    {
        if(sorter==null)
            return;

        Func<int,int,int> function = null;

        if(sorters.Count==0)
        {
            function = sorter;
        }
        else
        {
            function = sorter;
            foreach(var s in sorters)
            {
                var inProgress = function;
                var current = s;
                function = (x,y)=>current(inProgress(x,y), x,y);
            }
        }
        sortList.Capacity = sortList.Capacity < myList.Count ? myList.Count : sortList.Capacity;
        sortList.Clear();
        sortList.AddRange(System.Linq.Enumerable.Range(0,myList.Count));
        //var c = myList.Count;
        /*for(var i =0; i < c; i++)
            sortList.Add(i);*/
        sortList.Sort(new Comparison<int>(function));
        sorted = true;
        sorters.Clear();
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:49:47+00:00Added an answer on June 18, 2026 at 3:49 am

    I’ll need to guess but I’m still taking a shot at this. I think we should try getting rid of that nested lambda stuff and delegate conversions. I’m not sure how well that performs. The sort function should be this:

    Func<int, int, int>[] sorters = ...; //fill this. it really should be an array!
    Comparison<int> = (a, b) => {
     foreach (var s in sorters) {
      var cmp = s(a, b);
      if(cmp != 0) return cmp;
     }
     return 0;
    };
    

    So we got rid of the nested invocations. All a simple loop now. You can build specialized versions for small loop sizes:

    Func<int, int, int>[] sorters = ...; //fill this. it really should be an array!
    switch (sorters.Length) {
     case 2: {
       var s0 = sorters[0], s1 = sorters[1];
       Comparison<int> = (a, b) => {
         var cmp = s0(a, b);
         if(cmp != 0) return cmp;
         var cmp = s1(a, b);
         if(cmp != 0) return cmp;
         return 0;
       };
    }
    

    Unroll the loop so that no arrays appear anymore during the sort.

    All of this is really working around the fact that we don’t have static knowledge of the sort function’s structure. It would be much faster if the comparison function was just handed in by the caller.

    Update: Repro (100% more throughput than LINQ)

            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
    
            Func<int, int, int>[] sorters = new Func<int, int, int>[]
                {
                    (a, b) => (a & 0x1).CompareTo(b & 0x1),
                    (a, b) => (a & 0x2).CompareTo(b & 0x2),
                    (a, b) => (a & 0x4).CompareTo(b & 0x4),
                    (a, b) => a.CompareTo(b),
                };
    
            Func<int, int, int> comparisonB = sorters[0];
            for (int i = 1; i < sorters.Length; i++)
            {
                var func1 = comparisonB;
                var func2 = sorters[i];
                comparisonB = (a, b) =>
                    {
                        var cmp = func1(a, b);
                        if (cmp != 0) return cmp;
                        return func2(a, b);
                    };
            }
            var comparisonC = new Comparison<int>(comparisonB);
    
            Comparison<int> comparisonA = (a, b) =>
            {
                foreach (var s in sorters)
                {
                    var cmp = s(a, b);
                    if (cmp != 0) return cmp;
                }
                return 0;
            };
    
            Func<int, int, int> s0 = sorters[0], s1 = sorters[1], s2 = sorters[2], s3 = sorters[3];
            Comparison<int> comparisonD = (a, b) =>
                {
                    var cmp = s0(a, b);
                    if (cmp != 0) return cmp;
                    cmp = s1(a, b);
                    if (cmp != 0) return cmp;
                    cmp = s2(a, b);
                    if (cmp != 0) return cmp;
                    cmp = s3(a, b);
                    if (cmp != 0) return cmp;
                    return 0;
                };
    
            {
                GC.Collect();
                var data = CreateSortData();
                var sw = Stopwatch.StartNew();
                Array.Sort(data, comparisonC);
                sw.Stop();
                Console.WriteLine(sw.Elapsed.TotalSeconds);
            }
    
            {
                GC.Collect();
                var data = CreateSortData();
                var sw = Stopwatch.StartNew();
                Array.Sort(data, comparisonA);
                sw.Stop();
                Console.WriteLine(sw.Elapsed.TotalSeconds);
            }
    
            {
                GC.Collect();
                var data = CreateSortData();
                var sw = Stopwatch.StartNew();
                Array.Sort(data, comparisonD);
                sw.Stop();
                Console.WriteLine(sw.Elapsed.TotalSeconds);
            }
    
            {
                GC.Collect();
                var data = CreateSortData();
                var sw = Stopwatch.StartNew();
                foreach (var source in data.OrderBy(x => x & 0x1).ThenBy(x => x & 0x2).ThenBy(x => x & 0x4).ThenBy(x => x))
                {
    
                }
                sw.Stop();
                Console.WriteLine(sw.Elapsed.TotalSeconds);
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.