Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

955
Views
C# How Can I Sort A List without Generating Any Garbage at All (Unity3d)

The method used by List.Sort ends up using Array.Sort which generates 2 bytes of garbage even if you pass in an instance for IComparer. I need to sort many lists every frame in Unity3d so I need an algorithm that will not create a single byte of garbage if possible. Stack allocation for temporary variables should be fine.

Unity Profiler Pic

if (comparer == null)
    comparer = (IComparer<T>)Comparer<T>.Default;
if (BinaryCompatibility.TargetsAtLeast_Desktop_V4_5)
    ArraySortHelper<T>.IntrospectiveSort(keys, index, length, comparer);
else
    ArraySortHelper<T>.DepthLimitedQuickSort(keys, index, length + index - 1, comparer, 32);
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Check out this library in this github repo. I found this in bottom in this article which is about how LINQ affects GC and which LINQ function is safe to use on every frame. This library is written especially for unity and as author says this library is

A no-GC version of C#'s IEnumerator and LINQ

There is sort method which implements quickSort algorithms and does not produce garbage. To be honest I don't fully tested it yet, but as much as I used that it works finely.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!