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

[Caching] remove implicit cast from CachedWord to Word (1.7%) #1607

Merged
merged 3 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<TWord>(PipeWriter pipeWriter, TWord[] rows)
{
var writer = GetWriter(pipeWriter, sizeHint: 160 * rows.Length); // in reality it's 152 for one

Expand All @@ -32,7 +32,7 @@ private static void OutputMultipleQueries(PipeWriter pipeWriter, World[] rows)
utf8JsonWriter.Reset(pipeWriter);

// Body
JsonSerializer.Serialize<World[]>(utf8JsonWriter, rows, SerializerOptions);
JsonSerializer.Serialize<TWord[]>(utf8JsonWriter, rows, SerializerOptions);

// Content-Length
lengthWriter.WriteNumeric((uint)utf8JsonWriter.BytesCommitted);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 };
}
}
9 changes: 4 additions & 5 deletions src/BenchmarksApps/Kestrel/PlatformBenchmarks/Data/RawDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public async Task<World[]> LoadMultipleQueriesRows(int count)
return result;
}

public Task<World[]> LoadCachedQueries(int count)
public Task<CachedWorld[]> LoadCachedQueries(int count)
{
var result = new World[count];
var result = new CachedWorld[count];
var cacheKeys = _cacheKeys;
var cache = _cache;
var random = _random;
Expand All @@ -85,7 +85,7 @@ public Task<World[]> LoadCachedQueries(int count)

return Task.FromResult(result);

static async Task<World[]> LoadUncachedQueries(int id, int i, int count, RawDb rawdb, World[] result)
static async Task<CachedWorld[]> LoadUncachedQueries(int id, int i, int count, RawDb rawdb, CachedWorld[] result)
{
using (var db = new NpgsqlConnection(rawdb._connectionString))
{
Expand All @@ -106,8 +106,7 @@ static async Task<World[]> LoadUncachedQueries(int id, int i, int count, RawDb r

for (; i < result.Length; i++)
{
var data = await rawdb._cache.GetOrCreateAsync<CachedWorld>(key, create);
result[i] = data;
result[i] = await rawdb._cache.GetOrCreateAsync<CachedWorld>(key, create);

id = rawdb._random.Next(1, 10001);
idParameter.TypedValue = id;
Expand Down