From c2643b3a59cef432d5808f5483427cbe92e6e0c1 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Fri, 27 Nov 2020 21:08:04 +0100 Subject: [PATCH 1/3] remove implicit cast from CachedWord to Word (1.7%) --- .../BenchmarkApplication.Caching.cs | 27 ++++++++++++++++++- .../PlatformBenchmarks/Data/CachedWorld.cs | 5 +--- .../Kestrel/PlatformBenchmarks/Data/RawDb.cs | 9 +++---- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.Caching.cs b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.Caching.cs index 0098e68d2..6e388762a 100644 --- a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.Caching.cs +++ b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.Caching.cs @@ -2,6 +2,7 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.IO.Pipelines; +using System.Text.Json; using System.Threading.Tasks; namespace PlatformBenchmarks @@ -10,7 +11,31 @@ public partial class BenchmarkApplication { private async Task Caching(PipeWriter pipeWriter, int count) { - OutputMultipleQueries(pipeWriter, await Db.LoadCachedQueries(count)); + OutputCachedWords(pipeWriter, await Db.LoadCachedQueries(count)); + } + + private static void OutputCachedWords(PipeWriter pipeWriter, CachedWorld[] rows) + { + var writer = GetWriter(pipeWriter, sizeHint: 160 * rows.Length); // in reality it's 152 for one + + writer.Write(_dbPreamble); + + var lengthWriter = writer; + writer.Write(_contentLengthGap); + + // Date header + writer.Write(DateHeader.HeaderBytes); + + writer.Commit(); + + Utf8JsonWriter utf8JsonWriter = t_writer ??= new Utf8JsonWriter(pipeWriter, new JsonWriterOptions { SkipValidation = true }); + utf8JsonWriter.Reset(pipeWriter); + + // Body + JsonSerializer.Serialize(utf8JsonWriter, rows, SerializerOptions); + + // Content-Length + lengthWriter.WriteNumeric((uint)utf8JsonWriter.BytesCommitted); } } } diff --git a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/CachedWorld.cs b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/CachedWorld.cs index d32b5a53b..18a022dd4 100644 --- a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/CachedWorld.cs +++ b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/CachedWorld.cs @@ -1,17 +1,14 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System.Runtime.InteropServices; - namespace PlatformBenchmarks { - public class CachedWorld + public sealed class CachedWorld { public int Id { get; set; } public int RandomNumber { get; set; } - public static implicit operator World(CachedWorld world) => new World { Id = world.Id, RandomNumber = world.RandomNumber }; public static implicit operator CachedWorld(World world) => new CachedWorld { Id = world.Id, RandomNumber = world.RandomNumber }; } } diff --git a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/RawDb.cs b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/RawDb.cs index d0164d714..d6f717287 100644 --- a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/RawDb.cs +++ b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/RawDb.cs @@ -63,9 +63,9 @@ public async Task LoadMultipleQueriesRows(int count) return result; } - public Task LoadCachedQueries(int count) + public Task LoadCachedQueries(int count) { - var result = new World[count]; + var result = new CachedWorld[count]; var cacheKeys = _cacheKeys; var cache = _cache; var random = _random; @@ -85,7 +85,7 @@ public Task LoadCachedQueries(int count) return Task.FromResult(result); - static async Task LoadUncachedQueries(int id, int i, int count, RawDb rawdb, World[] result) + static async Task LoadUncachedQueries(int id, int i, int count, RawDb rawdb, CachedWorld[] result) { using (var db = new NpgsqlConnection(rawdb._connectionString)) { @@ -106,8 +106,7 @@ static async Task LoadUncachedQueries(int id, int i, int count, RawDb r for (; i < result.Length; i++) { - var data = await rawdb._cache.GetOrCreateAsync(key, create); - result[i] = data; + result[i] = await rawdb._cache.GetOrCreateAsync(key, create); id = rawdb._random.Next(1, 10001); idParameter.TypedValue = id; From f173d773019c72582fc2da65bee873539e8a2cc9 Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 30 Nov 2020 17:22:04 +0100 Subject: [PATCH 2/3] don't copy the code, it can be generic --- .../BenchmarkApplication.Caching.cs | 27 +------------------ .../BenchmarkApplication.MultipleQueries.cs | 4 +-- 2 files changed, 3 insertions(+), 28 deletions(-) diff --git a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.Caching.cs b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.Caching.cs index 6e388762a..0098e68d2 100644 --- a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.Caching.cs +++ b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.Caching.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.IO.Pipelines; -using System.Text.Json; using System.Threading.Tasks; namespace PlatformBenchmarks @@ -11,31 +10,7 @@ public partial class BenchmarkApplication { private async Task Caching(PipeWriter pipeWriter, int count) { - OutputCachedWords(pipeWriter, await Db.LoadCachedQueries(count)); - } - - private static void OutputCachedWords(PipeWriter pipeWriter, CachedWorld[] rows) - { - var writer = GetWriter(pipeWriter, sizeHint: 160 * rows.Length); // in reality it's 152 for one - - writer.Write(_dbPreamble); - - var lengthWriter = writer; - writer.Write(_contentLengthGap); - - // Date header - writer.Write(DateHeader.HeaderBytes); - - writer.Commit(); - - Utf8JsonWriter utf8JsonWriter = t_writer ??= new Utf8JsonWriter(pipeWriter, new JsonWriterOptions { SkipValidation = true }); - utf8JsonWriter.Reset(pipeWriter); - - // Body - JsonSerializer.Serialize(utf8JsonWriter, rows, SerializerOptions); - - // Content-Length - lengthWriter.WriteNumeric((uint)utf8JsonWriter.BytesCommitted); + OutputMultipleQueries(pipeWriter, await Db.LoadCachedQueries(count)); } } } diff --git a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.MultipleQueries.cs b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.MultipleQueries.cs index 9fec56495..ead734374 100644 --- a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.MultipleQueries.cs +++ b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/BenchmarkApplication.MultipleQueries.cs @@ -14,7 +14,7 @@ private async Task MultipleQueries(PipeWriter pipeWriter, int count) OutputMultipleQueries(pipeWriter, await Db.LoadMultipleQueriesRows(count)); } - private static void OutputMultipleQueries(PipeWriter pipeWriter, World[] rows) + private static void OutputMultipleQueries(PipeWriter pipeWriter, TWord[] rows) { var writer = GetWriter(pipeWriter, sizeHint: 160 * rows.Length); // in reality it's 152 for one @@ -32,7 +32,7 @@ private static void OutputMultipleQueries(PipeWriter pipeWriter, World[] rows) utf8JsonWriter.Reset(pipeWriter); // Body - JsonSerializer.Serialize(utf8JsonWriter, rows, SerializerOptions); + JsonSerializer.Serialize(utf8JsonWriter, rows, SerializerOptions); // Content-Length lengthWriter.WriteNumeric((uint)utf8JsonWriter.BytesCommitted); From 0b7d96b4140f1a61b1fe4dad6a62c70f3784ff4c Mon Sep 17 00:00:00 2001 From: Adam Sitnik Date: Mon, 30 Nov 2020 17:22:23 +0100 Subject: [PATCH 3/3] remove extra space --- .../Kestrel/PlatformBenchmarks/Data/CachedWorld.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/CachedWorld.cs b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/CachedWorld.cs index 18a022dd4..41d78ce40 100644 --- a/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/CachedWorld.cs +++ b/src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/CachedWorld.cs @@ -3,7 +3,7 @@ namespace PlatformBenchmarks { - public sealed class CachedWorld + public sealed class CachedWorld { public int Id { get; set; }