Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance refactor - elide async and await #2481

Merged

Conversation

gathogojr
Copy link
Contributor

@gathogojr gathogojr commented Aug 11, 2022

Description

The following sections describe the nature of changes made to improve performance and memory usage

1. Trade off pure asynchronous semantics (where it makes sense) to circumvent the async state machine overhead

When implementing asynchronous support, there are methods that I changed to async such that even bonehead exceptions would be caught by the state machine for async methods and placed on the returned task.
Those category of methods take the following form:

async Task MethodAsync()
{
    VerifyYadaYada(); // Throws exception if expected conditions are not met
    await MethodAnotherAsync();
}

The VerifyYadaYada methods verifies for example that the stream is not disposed, or that a delta link is being written within a delta resource set, etc.
Without the async keyword, the exception would be raised directly rather than get placed on the returned task.
In this PR, I have dropped the async modifier from such methods for the following reasons:

  • Unless there's a bug in our code, bonehead exceptions resulting from verification failures should never happen.
  • On the strength of the above point, we trade off pure asynchronous semantics - exception being placed on the task returned from the method - for performance. There's little sense in incurring a performance cost (construction of state machine, instantiation of Task objects, etc.) for a condition that will almost never occur. If it occurred, the exception will still be caught by a method with the async modifier higher up in the call stack.

2. Use of local functions to circumvent the async state machine overhead

Another category of methods where I dropped the async modifier are those where the asynchronous logic is run conditionally. By placing the asynchronous logic in a local function, it becomes possible to elide async and await. We get a performance benefit especially where the asynchronous logic is almost never executed. Such methods take the following form:

async Task MethodAsync()
{
    if (true) // Check condition
    {
        // Asynchronous methods
    }
}

To avoid paying a higher cost when the condition is false, the rewritten method looks as follows:

Task MethodAsync()
{
    if (true) // Check condition
    {
        return MethodInnerAsync();

        async Task MethodInnerAsync()
        {
            // Asynchronous methods
        }
    }

    return TaskUtils.CompletedTask; // Static property that returns a completed task instance
}

3. Elide async/await where different branches of logic in a method makes it possible

This PR also elides async and await where each branch of logic in the asynchronous method consist of a single asynchronous method call. For example;

async Task MethodAsync()
{
    if (true) // Condition check
    {
        await MethodAnotherAsync();
    }
    else if (true) // Another condition check
    {
        await MethodYetAnotherAsync();
    }

    throw new ODataException("Houston, we have a problem");
}

We rewrite these category of methods as follows:

Task MethodAsync()
{
    if (true) // Condition check
    {
        return MethodAnotherAsync();
    }
    else if (true) // Another condition check
    {
        return MethodYetAnotherAsync();
    }

    return TaskUtils.GetFaultedTask(new ODataException("Houston, we have a problem"));
}

4. Dropped use of expensive methods defined in TaskUtils

This PR also drops use of expensive methods defined in TaskUtils class - FollowOnSuccessWithTask, FollowOnSuccessWith, etc. Local functions that achieve the same purpose are introduced.

5. Performance and memory usage improvements to particular methods defined in TaskUtils

There are asynchronous methods that we support but the logic in such methods do not currently perform any asynchronous operations.
For example, CreateODataResourceWriterAsync currently only initializes the writer to use for writing OData resources (same logic as CreateODataResourceWriter). It makes use of a method GetTaskForSynchronousOperation defined in TaskUtils class. That method is used as follows:

return TaskUtils.GetTaskForSynchronousOperation(
    () => this.CreateODataResourceWriterImplementation(navigationSource, resourceType));

Where CreateODataResourceWriterImplementation is a synchronous method.

I considered replacing all instances of GetTaskForAsynchronousOperation with Task.FromResult but held back since the GetTaskForAsynchronousOperation method does some exception handling and returns a faulted task for all exceptions except OutOfMemoryException.
The GetTaskForAsynchronousOperation method makes use of 2 helper methods (GetCompletedTask and GetFaultedTask).
I did some benchmarking to see whether using Task.FromResult and Task.FromException in the 2 helper methods would result into better performance and memory usage metrics.

[MemoryDiagnoser]
public class CompletedTaskBenchmark
{
    private static readonly Type OutOfMemoryExceptionType = typeof(OutOfMemoryException);

    [Benchmark]
    public Task CompletedTaskUsingFromResultAsync()
    {
        return Task.FromResult("Task completed");
    }

    [Benchmark]
    public Task CompletedTaskUsingTaskCompletionSourceAsync()
    {
        return GetCompletedTask("Task completed");
    }

    [Benchmark]
    public Task CompletedTaskUsingGetTaskForSynchronousOperationAsync()
    {
        return GetTaskForSynchronousOperation(() => "Task completed");
    }

    [Benchmark]
    public Task CompletedTaskUsingGetTaskForSynchronousOperationRevisedAsync()
    {
        return GetTaskForSynchronousOperationRevised(() => "Task completed");
    }

    static Task<T> GetCompletedTask<T>(T value)
    {
        TaskCompletionSource<T> taskCompletionSource = new TaskCompletionSource<T>();
        taskCompletionSource.SetResult(value);
        return taskCompletionSource.Task;
    }

    internal static Task<T> GetFaultedTask<T>(Exception exception)
    {
        TaskCompletionSource<T> taskCompletionSource = new TaskCompletionSource<T>();
        taskCompletionSource.SetException(exception);
        return taskCompletionSource.Task;
    }

    static Task<T> GetTaskForSynchronousOperation<T>(Func<T> synchronousOperation)
    {
        try
        {
            T result = synchronousOperation();
            return GetCompletedTask(result);
        }
        catch (Exception ex)
        {
            Type type = ex.GetType();

            if (type == OutOfMemoryExceptionType)
            {
                throw;
            }

            return GetFaultedTask<T>(ex);
        }
    }

    static Task<T> GetTaskForSynchronousOperationRevised<T>(Func<T> synchronousOperation)
    {
        try
        {
            T result = synchronousOperation();
            return Task.FromResult(result);
        }
        catch (Exception ex)
        {
            Type type = ex.GetType();

            if (type == OutOfMemoryExceptionType)
            {
                throw;
            }

            return Task.FromException<T>(ex);
        }
    }
}

Below were the results:

|                                                       Method |      Mean |     Error |    StdDev |  Gen 0 | Allocated |
|------------------------------------------------------------- |----------:|----------:|----------:|-------:|----------:|
|                            CompletedTaskUsingFromResultAsync |  8.071 ns | 0.1713 ns | 0.2344 ns | 0.0172 |      72 B |
|                   CompletedTaskUsingTaskCompletedSourceAsync | 37.606 ns | 0.6161 ns | 0.5763 ns | 0.0229 |      96 B |
|        CompletedTaskUsingGetTaskForSynchronousOperationAsync | 40.309 ns | 0.4737 ns | 0.4431 ns | 0.0229 |      96 B |
| CompletedTaskUsingGetTaskForSynchronousOperationRevisedAsync | 13.357 ns | 0.2933 ns | 0.3709 ns | 0.0172 |      72 B |

From the above results, it's clear that the GetCompletedTask and GetFaultedTask as currently implemented are expensive. Using TaskCompletionSource to create a task instance has a higher cost than calling Task.FromResult.
I have refactored the GetTaskForSynchronousOperation to make use of both Task.FromResult and Task.FromException (where possible - Task.FromException is not supported across all frameworks that OData supports)

6. Introduction of overloads of particular methods that accept delegates as parameters to prevent capturing of state from the enclosing context

The following 4 methods accept delegates as a parameter. In a lot of cases where they are used, they capture state from the enclosing context and as a consequence the C# compiler automatically generates a private "display class" that results into inadvertent allocations.

Task WriteTopLevelPayloadAsync(Func<Task>) { /*...*/ } // ODataJsonLightSerializer

ODataContextUrlInfo WriteContextUriPropertyAsync(
    ODataPayloadKind,
    Func<ODataContextUrlInfo>,
    ODataContextUrlInfo,
    string) { /*...*/ } // ODataJsonLightSerializer

Task GetTaskForSynchronousOperation(Action) { /*...*/ } // TaskUtils

Task<T> GetTaskForSynchronousOperation(Func<T>) { /*...*/ } // TaskUtils

Below are examples of scenarios where "display classes" are created:
image

image

Since the above methods are quite heavily used, I introduced protected overloads that help prevent capturing state from the enclosing context. For WriteTopLevelPayloadAsync method for example, I introduced the following overloads:

Task WriteTopLevelPayloadAsync<TArg>(Func<TArg, Task>, TArg) { /*...*/ }

Task WriteTopLevelPayloadAsync<TArg1, TArg2>(Func<TArg1, TArg2>, TArg1, TArg2) { /*...*/ }

Task WriteTopLevelPayloadAsync<TArg1, TArg2, TArg3>(Func<TArg1, TArg2, TArg3>, TArg1, TArg2, TArg3) { /*...*/ }

By making use of the overloads, the "display classes" are eliminated together with the associated allocation overheads
image

No self-allocations in ODataJsonLightResourceSerializer.WriteResourceSetContextUriAsync
image

Objects Allocations Report: Base vs Diff

summary:
better: 10, geomean: 1.199
worse: 11, geomean: 1.196
new (results in the diff that are not in the base): 12
missing (results in the base that are not in the diff): 32
total diff: 65

| Worse                                                    |          diff/base | Base Allocations | Diff Allocations | Modality|
| -------------------------------------------------------- | ------------------:| ----------------:| ----------------:| --------:|
| System.Func<,,,,>                                        |                  3 |             1.00 |             3.00 |         |
| System.Threading.ThreadPoolWorkQueueThreadLocals         |               1.25 |             4.00 |             5.00 |         |
| System.Threading.ThreadPoolWorkQueue.WorkStealingQueue   |               1.25 |             4.00 |             5.00 |         |
| System.Threading.Thread                                  |                1.2 |             5.00 |             6.00 |         |
| System.Threading.ThreadPoolWorkQueue.WorkStealingQueue[] |                1.2 |             5.00 |             6.00 |         |
| System.Action<,>                                         | 1.0263157894736843 |           114.00 |           117.00 |         |
| System.Threading.Tasks.Task<>                            | 1.0180722891566265 |           166.00 |           169.00 |         |
| System.Text.StringBuilder                                | 1.0081967213114753 |           244.00 |           246.00 |         |
| System.Func<,,>                                          | 1.0036231884057971 |          1656.00 |          1662.00 |         |
| System.Object[]                                          | 1.0033444816053512 |           598.00 |           600.00 |         |
| System.Char[]                                            |       1.0029296875 |          1024.00 |          1027.00 |         |

| Better                                                                          |          base/diff | Base Allocations | Diff Allocations | Modality|
| ------------------------------------------------------------------------------- | ------------------:| ----------------:| ----------------:| --------:|
| System.Threading.Tasks.TaskCompletionSource<>                                   |  3.176470588235294 |            54.00 |            17.00 |         |
| System.Action                                                                   | 1.1612903225806452 |           288.00 |           248.00 |         |
| System.Func<>                                                                   | 1.1336206896551724 |           263.00 |           232.00 |         |
| System.Runtime.CompilerServices.AsyncTaskMethodBuilder<>.AsyncStateMachineBox<> | 1.1290322580645162 |            35.00 |            31.00 |         |
| System.Threading.ContextCallback                                                | 1.1111111111111112 |            20.00 |            18.00 |         |
| System.Action<>                                                                 |  1.110655737704918 |           271.00 |           244.00 |         |
| System.Func<,>                                                                  | 1.0348178137651822 |          1278.00 |          1235.00 |         |
| System.Byte[]                                                                   | 1.0165289256198347 |           123.00 |           121.00 |         |
| System.SByte[]                                                                  | 1.0055137844611528 |          8024.00 |          7980.00 |         |
| System.String                                                                   |  1.000027430703186 |         72913.00 |         72911.00 |         |

| New                                                                      | diff/base | Base Allocations | Diff Allocations | Modality|
| ------------------------------------------------------------------------ | --------- | ----------------:| ----------------:| -------- |
| System.Func<,,,>                                                         | N/A       |                  |             6.00 | N/A     |
| System.Action<,,,,>                                                      | N/A       |                  |             2.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightCollectionSerializer.<>c         | N/A       |                  |             1.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.<>c                | N/A       |                  |             1.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightPropertySerializer.<>c           | N/A       |                  |             1.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.<>c           | N/A       |                  |             1.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightServiceDocumentSerializer.<>c    | N/A       |                  |             1.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightValueSerializer.<>c              | N/A       |                  |             1.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightWriter.<>c                       | N/A       |                  |             1.00 | N/A     |
| Microsoft.OData.MultipartMixed.ODataMultipartMixedBatchOutputContext.<>c | N/A       |                  |             1.00 | N/A     |
| Microsoft.OData.MultipartMixed.ODataMultipartMixedBatchWriter.<>c        | N/A       |                  |             1.00 | N/A     |
| Microsoft.OData.RawValueWriter.<>c                                       | N/A       |                  |             1.00 | N/A     |

| Missing                                                                          | diff/base | Base Allocations | Diff Allocations | Modality|
| -------------------------------------------------------------------------------- | --------- | ----------------:| ----------------:| -------- |
| System.Threading.Tasks.ContinuationTaskFromTask                                  | N/A       |            48.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightValueSerializer.<>c__DisplayClass19_0    | N/A       |           102.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightSerializer.<>c__DisplayClass6_0          | N/A       |            62.00 |                  | N/A     |
| System.Threading.Tasks.Task.ContingentProperties                                 | N/A       |            24.00 |                  | N/A     |
| System.Threading.Tasks.StandardTaskContinuation                                  | N/A       |            48.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightWriter.<>c__DisplayClass82_0             | N/A       |            19.00 |                  | N/A     |
| Microsoft.OData.TaskUtils.<>c__DisplayClass27_0<>                                | N/A       |            24.00 |                  | N/A     |
| Microsoft.OData.ODataMessage.<>c__DisplayClass19_0                               | N/A       |            20.00 |                  | N/A     |
| Microsoft.OData.TaskUtils.<>c__DisplayClass13_0<,>                               | N/A       |            20.00 |                  | N/A     |
| Microsoft.OData.ODataMessageWriter.<>c__DisplayClass102_0<>                      | N/A       |            13.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.<>c__DisplayClass20_0 | N/A       |             6.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.<>c__DisplayClass30_0      | N/A       |             6.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.<>c__DisplayClass21_0 | N/A       |             6.00 |                  | N/A     |
| Microsoft.OData.ODataMessageWriter.<>c__DisplayClass101_0                        | N/A       |             7.00 |                  | N/A     |
| Microsoft.OData.ODataWriterCore.<>c__DisplayClass64_0                            | N/A       |             6.00 |                  | N/A     |
| Microsoft.OData.ODataBatchOperationMessage.<>c__DisplayClass10_0                 | N/A       |             4.00 |                  | N/A     |
| Microsoft.OData.ODataBatchWriter.<>c__DisplayClass71_0                           | N/A       |             4.00 |                  | N/A     |
| Microsoft.OData.RawValueWriter.<>c__DisplayClass18_0                             | N/A       |             3.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightWriter.<>c__DisplayClass83_0             | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.<>c__DisplayClass26_0      | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.<>c__DisplayClass28_0      | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.<>c__DisplayClass36_0      | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.<>c__DisplayClass22_0 | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.ODataParameterWriterCore.<>c__DisplayClass75_0                   | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightCollectionSerializer.<>c__DisplayClass4_ | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.<>c__DisplayClass32_0      | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.<>c__DisplayClass38_0      | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightPropertySerializer.<>c__DisplayClass9_0  | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightServiceDocumentSerializer.<>c__DisplayCl | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.ODataWriterCore.<>c__DisplayClass195_0                           | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightPropertySerializer.<>c__DisplayClass9_1  | N/A       |             1.00 |                  | N/A     |
| Microsoft.OData.TaskUtils.<>c                                                    | N/A       |             1.00 |                  | N/A     |

Functions Allocations Report: Base vs Diff

summary:
better: 14, geomean: 1.686
worse: 17, geomean: 1.412
new (results in the diff that are not in the base): 40
missing (results in the base that are not in the diff): 42
total diff: 113

| Worse                                                                            |          diff/base | Base Self Allocations | Diff Self Allocations | Modality|
| -------------------------------------------------------------------------------- | ------------------:| ---------------------:| ---------------------:| --------:|
| Microsoft.OData.ODataWriterCore.InterceptExceptionAsync()                        |                  6 |                  3.00 |                 18.00 |         |
| Microsoft.OData.Json.JsonWriter.StartStreamValueScopeAsync()                     |                  3 |                  1.00 |                  3.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightWriter.StartResourceSetAsync()           |              2.125 |                 16.00 |                 34.00 |         |
| Microsoft.OData.ODataWriterCore.WriteStartAsync()                                |              1.625 |                  8.00 |                 13.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightWriter.WriteEntityReferenceLinkImplement | 1.3333333333333333 |                  6.00 |                  8.00 |         |
| Microsoft.OData.ODataResponseMessage.GetStreamAsync()                            |                1.3 |                 10.00 |                 13.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightPropertySerializer.WritePropertyAsync()  | 1.2857142857142858 |                  7.00 |                  9.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightValueSerializer.WriteResourceValueAsync( | 1.2857142857142858 |                  7.00 |                  9.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.WriteOperationMetadat |                1.2 |                  5.00 |                  6.00 |         |
| Microsoft.OData.MultipartMixed.ODataMultipartMixedBatchOutputContext.CreateOData |                1.2 |                  5.00 |                  6.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightPropertySerializer.WritePropertyInfoAsyn |  1.105263157894737 |                 19.00 |                 21.00 |         |
| Microsoft.OData.UriParser.ODataPathExtensions.AddKeySegment(Microsoft.OData.UriP | 1.0588235294117647 |                 17.00 |                 18.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightPropertySerializer.WritePrimitivePropert | 1.0588235294117647 |                 17.00 |                 18.00 |         |
| Microsoft.OData.ODataSimplifiedOptions.GetODataSimplifiedOptions(System.IService | 1.0416666666666667 |                 24.00 |                 25.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightWriter.StartResourceAsync()              | 1.0263157894736843 |                 38.00 |                 39.00 |         |
| Microsoft.OData.ODataWriterCore.WriteStartResourceSetImplementationAsync()       | 1.0204081632653061 |                 49.00 |                 50.00 |         |
| Microsoft.OData.ODataWriterCore.PushScope(WriterState, Microsoft.OData.ODataItem | 1.0192307692307692 |                 52.00 |                 53.00 |         |

| Better                                                                           |          base/diff | Base Self Allocations | Diff Self Allocations | Modality|
| -------------------------------------------------------------------------------- | ------------------:| ---------------------:| ---------------------:| --------:|
| Microsoft.OData.JsonLight.ODataJsonLightValueSerializer.WritePrimitiveValueAsync |                 28 |                112.00 |                  4.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightSerializer.WriteContextUriPropertyAsync( |                4.2 |                 21.00 |                  5.00 |         |
| Microsoft.OData.ODataBatchWriter.CreateOperationRequestMessageInternalAsync()    | 1.7777777777777777 |                 16.00 |                  9.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.WritePropertyImplementatio |                1.5 |                  3.00 |                  2.00 |         |
| Microsoft.OData.ODataWriterCore.CreateTextWriterImplementationAsync()            | 1.3333333333333333 |                  4.00 |                  3.00 |         |
| Microsoft.OData.ODataBatchWriter.InterceptException(System.Action<Microsoft.ODat |               1.25 |                  5.00 |                  4.00 |         |
| Microsoft.OData.ODataWriterCore.WriteStartResourceImplementationAsync.AnonymousM | 1.2352941176470589 |                 21.00 |                 17.00 |         |
| Microsoft.OData.MultipartMixed.ODataMultipartMixedBatchFormat.CreateOutputContex | 1.1818181818181819 |                 13.00 |                 11.00 |         |
| Microsoft.OData.SimpleLazy<T>.CreateValue()                                      | 1.1818181818181819 |                 13.00 |                 11.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightSerializer.ctor(Microsoft.OData.JsonLigh | 1.1653333333333333 |                437.00 |                375.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightPropertySerializer.WriteCollectionProper | 1.1428571428571428 |                  8.00 |                  7.00 |         |
| Microsoft.OData.Json.JsonWriter.StartScopeAsync()                                | 1.1126760563380282 |                 79.00 |                 71.00 |         |
| Microsoft.OData.RawValueWriter.WriteRawValueAsync(object)                        | 1.1071428571428572 |                 31.00 |                 28.00 |         |
| Microsoft.OData.JsonLight.ODataJsonLightWriter.ctor(Microsoft.OData.JsonLight.OD | 1.0149253731343284 |                 68.00 |                 67.00 |         |

| New                                                                              | diff/base | Base Self Allocations | Diff Self Allocations | Modality|
| -------------------------------------------------------------------------------- | --------- | ---------------------:| ---------------------:| -------- |
| Microsoft.OData.ODataMessage.GetStreamAsync.__GetMessageStreamAsync19_0()        | N/A       |                       |                 52.00 | N/A     |
| Microsoft.OData.ODataBatchOperationMessage.GetStreamAsync.__GetStreamInnerAsync1 | N/A       |                       |                 11.00 | N/A     |
| Microsoft.OData.TaskUtils.GetTaskForSynchronousOperation<T1, T2, T3, T4, T5>(Sys | N/A       |                       |                 32.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightSerializer..ctor.AnonymousMethod__6_0()  | N/A       |                       |                 30.00 | N/A     |
| Microsoft.OData.ODataCollectionWriterCore.WriteItemImplementationAsync.Anonymous | N/A       |                       |                  1.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.WriteResourceSetStart | N/A       |                       |                  1.00 | N/A     |
| Microsoft.OData.ODataCollectionWriterCore.InterceptExceptionAsync()              | N/A       |                       |                  1.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightSerializer.WriteContextUriPropertyImplem | N/A       |                       |                 20.00 | N/A     |
| Microsoft.OData.ODataMessageWriter.WriteToOutputAsync()                          | N/A       |                       |                  4.00 | N/A     |
| Microsoft.OData.Json.JsonWriterAsyncExtensions.WritePrimitiveValueAsync(Microsof | N/A       |                       |                  1.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightSerializer..ctor.AnonymousMethod__6_2()  | N/A       |                       |                 26.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.CreateODataResourceWriterA | N/A       |                       |                  6.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightSerializer..ctor.AnonymousMethod__6_1()  | N/A       |                       |                 23.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightPropertySerializer.WriteTopLevelProperty | N/A       |                       |                  5.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.CreateODataResourceSetWrit | N/A       |                       |                 13.00 | N/A     |
| Microsoft.OData.ODataWriterCore.InterceptExceptionAsync(System.Func<Microsoft.OD | N/A       |                       |                  1.00 | N/A     |
| Microsoft.OData.TaskUtils.GetTaskForSynchronousOperation<T1, T2>(System.Action<T | N/A       |                       |                  8.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.CreateODataParameterWriter | N/A       |                       |                  5.00 | N/A     |
| Microsoft.OData.TaskUtils.GetTaskForSynchronousOperation<T1, T2, T3>(System.Func | N/A       |                       |                  8.00 | N/A     |
| Microsoft.OData.TaskUtils.GetTaskForSynchronousOperation<T1, T2, T3, T4>(System. | N/A       |                       |                  6.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.CreateODataCollectionWrite | N/A       |                       |                  5.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.WriteOperationAsync() | N/A       |                       |                  1.00 | N/A     |
| Microsoft.OData.ODataWriterCore.WriteStartResourceSetImplementationAsync.Anonymo | N/A       |                       |                  3.00 | N/A     |
| Microsoft.OData.JsonLightInstanceAnnotationWriter.WriteInstanceAnnotationsAsync( | N/A       |                       |                  4.00 | N/A     |
| Microsoft.OData.ODataWriterCore.VerifyCanWriteStartResourceSetAsync(bool, Micros | N/A       |                       |                  5.00 | N/A     |
| Microsoft.OData.ODataCollectionWriterCore.WriteStartAsync(Microsoft.OData.ODataC | N/A       |                       |                  4.00 | N/A     |
| Microsoft.OData.WriterValidator.ValidateTypeKind(Microsoft.OData.Edm.EdmTypeKind | N/A       |                       |                  2.00 | N/A     |
| Microsoft.OData.HttpUtils.ReadMediaTypeParameter(string, ref int, System.Collect | N/A       |                       |                  5.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightValueSerializer.get_PropertySerializer() | N/A       |                       |                  2.00 | N/A     |
| Microsoft.OData.MultipartMixed.ODataMultipartMixedBatchWriter.WriteStartChangese | N/A       |                       |                  3.00 | N/A     |
| Microsoft.OData.ODataMessageWriter.WriteServiceDocumentAsync(Microsoft.OData.ODa | N/A       |                       |                  4.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.WriteResourceEndMetad | N/A       |                       |                  4.00 | N/A     |
| Microsoft.OData.EdmExtensionMethods.FindNavigationTarget(Microsoft.OData.Edm.IEd | N/A       |                       |                  3.00 | N/A     |
| Microsoft.OData.ODataParameterWriterCore.WriteEndAsync()                         | N/A       |                       |                  3.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.CreateODataUriParameterRes | N/A       |                       |                  1.00 | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.CreateODataDeltaResourceSe | N/A       |                       |                  1.00 | N/A     |
| Microsoft.OData.MultipartMixed.ODataMultipartMixedBatchWriterUtils.WriteStartBou | N/A       |                       |                  2.00 | N/A     |
| Microsoft.OData.UriParser.ODataPathInfo.ctor(Microsoft.OData.UriParser.ODataPath | N/A       |                       |                  2.00 | N/A     |
| Microsoft.OData.ODataMessageWriter.CreateODataParameterWriterAsync(Microsoft.ODa | N/A       |                       |                  4.00 | N/A     |
| Microsoft.OData.ODataParameterWriterCore.VerifyCanWriteStart(bool)               | N/A       |                       |                  3.00 | N/A     |

| Missing                                                                          | diff/base | Base Self Allocations | Diff Self Allocations | Modality|
| -------------------------------------------------------------------------------- | --------- | ---------------------:| ---------------------:| -------- |
| Microsoft.OData.ODataMessage.GetStreamAsync(System.Func<System.Threading.Tasks.T | N/A       |                 57.00 |                       | N/A     |
| Microsoft.OData.MultipartMixed.ODataMultipartMixedBatchWriterUtils.WriteRequestP | N/A       |                  2.00 |                       | N/A     |
| Microsoft.OData.TaskUtils.FollowOnSuccessWithImplementation<T>(System.Threading. | N/A       |                 79.00 |                       | N/A     |
| Microsoft.OData.ODataBatchOperationMessage.GetStreamAsync()                      | N/A       |                 15.00 |                       | N/A     |
| Microsoft.OData.Json.JsonWriterAsyncExtensions.WritePrimitiveValueAsync()        | N/A       |                  3.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightWriter.PrepareResourceForWriteStartAsync | N/A       |                 38.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightSerializer..ctor.AnonymousMethod__0()    | N/A       |                 30.00 |                       | N/A     |
| Microsoft.OData.ODataWriterCore.WriteStartDeletedResourceImplementationAsync.Ano | N/A       |                  2.00 |                       | N/A     |
| Microsoft.OData.Json.JsonValueUtils.WriteValueAsync()                            | N/A       |                  2.00 |                       | N/A     |
| Microsoft.OData.TaskUtils.FollowOnSuccessWith<T1, T2>(System.Threading.Tasks.Tas | N/A       |                 40.00 |                       | N/A     |
| Microsoft.OData.ODataMessageWriter<T>.MoveNext()                                 | N/A       |                 30.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightWriter.PrepareDeletedResourceForWriteSta | N/A       |                 18.00 |                       | N/A     |
| Microsoft.OData.ODataMessage.GetStreamAsync.AnonymousMethod__0(System.Threading. | N/A       |                 21.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightWriter.PrepareResourceForWriteStartAsync | N/A       |                 16.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightWriter.StartResourceAsync(Microsoft.ODat | N/A       |                  1.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightSerializer..ctor.AnonymousMethod__2()    | N/A       |                 26.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.CreateODataResourceWriterA | N/A       |                  6.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightSerializer..ctor.AnonymousMethod__1()    | N/A       |                 23.00 |                       | N/A     |
| Microsoft.OData.ODataBatchWriter.CreateOperationRequestMessageAsync()            | N/A       |                 12.00 |                       | N/A     |
| Microsoft.OData.TaskUtils.GetTaskForSynchronousOperation<T>(System.Func<T>)      | N/A       |                 15.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.WriteDeltaContextUriA | N/A       |                 12.00 |                       | N/A     |
| Microsoft.OData.ODataWriterCore.VerifyCanWriteStartResourceSetAsync()            | N/A       |                 13.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.CreateODataResourceSetWrit | N/A       |                 13.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.WriteResourceContextU | N/A       |                 12.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.CreateODataResourceWriterA | N/A       |                 12.00 |                       | N/A     |
| Microsoft.OData.TaskUtils.GetTaskForSynchronousOperation(System.Action)          | N/A       |                 10.00 |                       | N/A     |
| Microsoft.OData.ODataWriterCore.WriteStartAsync(Microsoft.OData.ODataResourceSet | N/A       |                  1.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.WriteDeltaContextUriA | N/A       |                 11.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.WriteResourceSetConte | N/A       |                 12.00 |                       | N/A     |
| Microsoft.OData.ODataMessageWriter.WriteToOutputAsync<T>(Microsoft.OData.ODataPa | N/A       |                 14.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightPropertySerializer.WriteTopLevelProperty | N/A       |                  5.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.CreateODataParameterWriter | N/A       |                  5.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightOutputContext.CreateODataCollectionWrite | N/A       |                  5.00 |                       | N/A     |
| Microsoft.OData.TaskUtils.GetCompletedTask<T>(T)                                 | N/A       |                 13.00 |                       | N/A     |
| Microsoft.OData.TaskUtils.FollowOnSuccessWithContinuation<T>(System.Threading.Ta | N/A       |                  6.00 |                       | N/A     |
| Microsoft.OData.ODataWriterCore.CreateTextWriterAsync()                          | N/A       |                  3.00 |                       | N/A     |
| Microsoft.OData.JsonLightInstanceAnnotationWriter.WriteInstanceAnnotationsAsync( | N/A       |                  4.00 |                       | N/A     |
| Microsoft.OData.MultipartMixed.ODataMultipartMixedBatchWriter.StreamDisposedAsyn | N/A       |                  4.00 |                       | N/A     |
| Microsoft.OData.ODataMessageWriter.WriteToOutputAsync.__WriteToOutputInnerAsync0 | N/A       |                  4.00 |                       | N/A     |
| Microsoft.OData.JsonLight.ODataJsonLightResourceSerializer.WriteResourceSetConte | N/A       |                  4.00 |                       | N/A     |
| Microsoft.OData.ODataMessageWriter.WriteToOutputAsync(Microsoft.OData.ODataPaylo | N/A       |                  7.00 |                       | N/A     |
| Microsoft.OData.ODataCollectionWriterCore.WriteStartAsync()                      | N/A       |                  4.00 |                       | N/A     |

Checklist (Uncheck if it is not completed)

  • Test cases added
  • Build and test with one-click build and test script passed

Additional work necessary

If documentation update is needed, please add "Docs Needed" label to the issue and provide details about the required document change in the issue.

@KenitoInc
Copy link
Contributor

Can we get some benchmarks for this change

@gathogojr gathogojr force-pushed the feature/odata-writers-performance-refactor branch from 622c172 to f76e74b Compare August 22, 2022 12:35
@habbes

This comment was marked as resolved.

@gathogojr gathogojr force-pushed the feature/odata-writers-performance-refactor branch from f76e74b to fd6cf38 Compare August 29, 2022 08:37
@habbes
Copy link
Contributor

habbes commented Sep 19, 2022

I've re-run the "SerializationComparisons" benchmarks and now I see improvements in both performance and memory of up to 10%. Great work.

Before

Method WriterName Mean Error StdDev Gen 0 Gen 1 Allocated
WriteToFileAsync ODataMessageWriter-Async 911.242 ms 8.6172 ms 7.6389 ms 66000.0000 1000.0000 403,823 KB
WriteToFileAsync ODataMessageWriter-Utf8JsonWriter-Async 471.671 ms 0.6272 ms 0.5867 ms 44000.0000 1000.0000 271,267 KB

After

Method WriterName Mean Error StdDev Gen 0 Gen 1 Allocated
WriteToFileAsync ODataMessageWriter-Async 816.809 ms 2.3509 ms 2.0840 ms 59000.0000 1000.0000 362,984 KB
WriteToFileAsync ODataMessageWriter-Utf8JsonWriter-Async 429.522 ms 0.7683 ms 0.7187 ms 40000.0000 1000.0000 246,378 KB

@gathogojr
Copy link
Contributor Author

Can we get some benchmarks for this change

I added some comparison results for before and after and @habbes also run some benchmarks and shared the data in a comment

@pull-request-quantifier-deprecated

This PR has 1533 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Large
Size       : +980 -553
Percentile : 100%

Total files changed: 41

Change summary by file extension:
.cs : +980 -508
.bsl : +0 -45

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@gathogojr gathogojr merged commit 7b19a5e into OData:master Sep 26, 2022
@gathogojr gathogojr deleted the feature/odata-writers-performance-refactor branch September 26, 2022 06:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants