Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is array item assignment degrading C# program performance?

Summary

While processing a large text file, I came across the following (unexpected) performance degradation that I can't explain. My objectives for this question are:

  • Understand what causes the slowdown described below
  • Find out how to initialize large non-primitive arrays quickly

The Problem

  • Array contains non-primitive reference items
    • Not int[] but MyComplexType[]
    • MyComplexType is a class, not a struct
    • MyComplexType contains some string properties
  • Array is pre-allocated
  • Array is large
  • If an item is created and used without being assigned to the array, program is fast
  • If an item is created and then assigned to an array item, program slows down significantly
    • I expected array item assignment to be a simple reference assignment, but this does not seem to the the case based on my results for my test program below

Test program

Consider the following C# program:

namespace Test
{
    public static class Program
    {
        // Simple data structure
        private sealed class Item
        {
            public Item(int i)
            {
                this.Name = "Hello " + i;
                //this.Name = "Hello";
                //this.Name = null;
            }
            public readonly string Name;
        }

        // Test program
        public static void Main()
        {
            const int length = 1000000;
            var items = new Item[length];

            // Create one million items but don't assign to array
            var w = System.Diagnostics.Stopwatch.StartNew();
            for (var i = 0; i < length; i++)
            {
                var item = new Item(i);
                if (!string.IsNullOrEmpty(item.Name)) // reference the item and its Name property
                {
                    items[i] = null; // do not remember the item
                }
            }
            System.Console.Error.WriteLine("Without assignment: " + w.Elapsed);

            // Create one million items and assign to array
            w.Restart();
            for (var i = 0; i < length; i++)
            {
                var item = new Item(i);
                if (!string.IsNullOrEmpty(item.Name)) // reference the item and its Name property
                {
                    items[i] = item; // remember the item
                }
            }
            System.Console.Error.WriteLine("   With assignment: " + w.Elapsed);
        }
    }
}

It contains two almost-identical loops. Each loop creates one million instances of Item class. First loop uses the created item and then throws away the reference (not persisting it in the items array). Second loop uses the created item and then stores the reference in the items array. Array item assignment is the only difference between the loops.

My Results

  • When I run Release build (optimizations turned on) on my machine, I get the following results:

    Without assignment: 00:00:00.2193348
       With assignment: 00:00:00.8819170
    

    Loop with array assignment is significantly slower than the one without assignment (~4x slower).

  • If I change Item constructor to assign a constant string to Name property:

    public Item(int i)
    {
        //this.Name = "Hello " + i;
        this.Name = "Hello";
        //this.Name = null;
    }
    

    I get the following results:

    Without assignment: 00:00:00.0228067
       With assignment: 00:00:00.0718317
    

    Loop with assignment is still ~3x slower than the one without

  • Finally if I assign null to the Name property:

    public Item(int i)
    {
        //this.Name = "Hello " + i;
        //this.Name = "Hello";
        this.Name = null;
    }
    

    I get the following result:

    Without assignment: 00:00:00.0146696
       With assignment: 00:00:00.0105369
    

    Once no string is allocated, the version without assignment is finally slightly slower (I assume because all those instances are released for garbage collection)

Questions

  • Why is array item assignment slowing the test program so much?

  • Is there an attribute/language construct/etc that will speed up the assignment?

PS: I tried investigating the slowdown using dotTrace, but it was inconclusive. One thing I saw was a lot more string copying and garbage collection overhead in the loop with assignment than in the loop without assignment (even though I expected the reverse).

like image 883
Milan Gardian Avatar asked Nov 05 '13 20:11

Milan Gardian


3 Answers

I suspect most of the timing issues are related to memory allocation.

When you assign the items into the array, they are never becoming eligible for garbage collection. When you have a string as a property that isn't a constant (interned) or null, this is going to cause your memory allocation requirements to go up.

In the first case, I suspect what's happening is that you're churning through the objects fast, so they stay in Gen0, and can be GCed quickly, and that memory segment can be reused. This means that you're never having to allocate more memory from the OS.

In the second case, you're creating strings within your objects, both of which are two allocations, then storing these so they aren't eligible for GC. At some point, you'll need to get more memory, so you'll get allocated memory.

As for your final check - when you set the Name to null, the if (!string.IsNullOrEmpty(item.Name)) check will prevent it from being added. As such, the two code paths, and therefore the timings, become (effectively) identical, though the first is marginally slower (most likely due to the JIT running the first time).

like image 133
Reed Copsey Avatar answered Oct 21 '22 14:10

Reed Copsey


My guess is that the compiler is being really smart and sees you don't need to do anything significant with Item in the case where you're not assigning it. It probably just reuses the Item object memory in the first loop since it can. In the second loop bits of heap need to be allocated since they're all independent and referenced later.

I guess this kind of agrees with what you saw related to garbage collection. One item is created in the first loop vs. many.

A quick note - the first loop is probably using object pooling as it's optimization. This article may provide insight. As Reed is quick to point out, the article talks about app optimizations, but I imagine the allocator itself has a lot of optimizations that do similar things.

like image 30
NG. Avatar answered Oct 21 '22 15:10

NG.


I don't believe this has anything to do (really) with array assignment. It has to do with the amount of time the item and its contained objects have to be kept around, just in case you may later reference them. It's to do with heap allocation and garbage collection generations.

When first allocated the item and it's strings will be in "generation 0". This is often garbage collected and is very hot, maybe even cached, memory. It's very likely that on the next few iterations of the loop the whole "generation 0" will be GC'ed and the memory re-used for new items and their strings. When we add the assignment to the array, the object can not be garbage collected because there is still a reference to it. This causes increased memory consumption.

I believe you will see memory increasing during the execution of your code: I believe that the problem is memory allocations in the heap combined with cache-misses because it is always having to use "fresh" memory and can not benefit from hardware memory caching.

like image 39
dsz Avatar answered Oct 21 '22 15:10

dsz