concept generation in category .net

appears as: generations, generation
Concurrency in .NET

This is an excerpt from Manning's book Concurrency in .NET.

The version of the code that runs using the Complex object as a reference type makes many short-lived allocations to the heap: more than 4 million.3  A short-lived object is stored in the first GC generation, and it’s scheduled to be removed from the memory sooner than generations 1 and 2. This high rate of allocation forces the GC to run, which involves stopping all the threads that are running, except the threads needed for the GC. The interrupted tasks resume only after the GC operation completes. Clearly, the smaller the number of the GC generations, the faster the application performs.

How can the fold function increase the performance speed of a list query regardless of parallelism? To answer, let’s analyze a simple PLINQ query. You saw that the use of functional constructs, like LINQ/PLINQ in .NET, transforms the original sequence avoiding mutation, which in strict-evaluated programming languages such as F# and C# often leads to the generation of intermediate data structures that are unnecessary. This listing shows a PLINQ query that filters and then transforms a sequence of numbers to calculate the sum of the even values times two (doubled). The parallel execution is in bold.

Listing 5.6 PLINQ query to sum the double of even numbers in parallel
var data = new int[100000];
for(int i = 0; i < data.Length; i++)
    data[i]=i;

long total =
 data.AsParallel()   #1  
        .Where(n => n % 2 == 0)
        .Select(n => n + n)
 .Sum(x => (long)x);   #2  

#1   Due to parallelism, the order in which the values are processed isn’t guaranteed; but the result is deterministic because the Sum operation is commutative.
#2   The value is cast to long number Sum(x => (long)x) to avoid an overflow exception.


!@%STYLE%@!
{"css":"{\"css\": \"font-weight: bold;\"}","target":"[[{\"line\":5,\"ch\":6},{\"line\":5,\"ch\":18}]]"}
!@%STYLE%@!

In these few lines of code, for each Where and Select of the PLINQ query, there’s a generation of intermediate sequences that unnecessarily increase memory allocation. In the case of large sequences to transform, the penalty paid to the GC to free memory becomes increasingly higher, with negative consequences for performance. The allocation of objects in memory is expensive; consequently, optimization that avoids extra allocation is valuable for making functional programs run faster. Fortunately, the creation of these unnecessary data structures can often be avoided. The elimination of intermediate data structures to reduce the size of temporary memory allocation is referred to as deforesting. This technique is easily exploitable with the higher-order function fold, which takes the name Aggregate in LINQ. This function is capable of eliminating intermediate data-structure allocations by combining multiple operations, such as filter and map, in a single step, which would otherwise have an allocation for each operation. This code example shows a PLINQ query to sum the double of even numbers in parallel using the Aggregate operator: