async-runtimes-benchmarks icon indicating copy to clipboard operation
async-runtimes-benchmarks copied to clipboard

Significantly improve .NET's code

Open OoLunar opened this issue 2 years ago • 11 comments

Significantly improves .NET's code, closes https://github.com/pkolaczk/async-runtimes-benchmarks/pull/4

Additionally ValueTasks are typically used in hotpaths over Task's due to their newer implementation and don't suffer the scars of .NET framework.

OoLunar avatar May 28 '23 23:05 OoLunar

You added bin and obj folders. I don't think they should be included in git. 😉

batzen avatar May 29 '23 05:05 batzen

Whoops! You're right

OoLunar avatar May 29 '23 21:05 OoLunar

Good job @OoLunar , I agree your correcations.

heku avatar Jun 04 '23 03:06 heku

in fact would be better to use top-level statements to show beauty of modern c# for the doubters hehe also i'd use Task WhenAll(IEnumerable<Task> tasks) overload so i dont allocate array by myself

my code would look like this:

const int delayMs = 1000 * 10; 
var taskCount = args.Length > 0 ? int.Parse(args[0]) : 100000;
var tasks = Enumerable
    .Range(0, taskCount)
    .Select(_ => Task.Delay(delayMs));
await Task.WhenAll(tasks);

rolandcheck avatar Jun 05 '23 23:06 rolandcheck

in fact would be better to use top-level statements to show beauty of modern c# for the doubters hehe also i'd use Task WhenAll(IEnumerable<Task> tasks) overload so i dont allocate array by myself

my code would look like this:

const int delayMs = 1000 * 10; 
var taskCount = args.Length > 0 ? int.Parse(args[0]) : 100000;
var tasks = Enumerable
    .Range(0, taskCount)
    .Select(_ => Task.Delay(delayMs));
await Task.WhenAll(tasks);

And also do top level namespace

leo-costa avatar Jun 09 '23 04:06 leo-costa

Have either one of y'all benchmarked your new code? While I agree, the code could look appealing to non-C# users, the goal here is to provide efficiency, not readability.

i'd use Task WhenAll(IEnumerable<Task> tasks) overload so i dont allocate array by myself

What... do you think an enumerable is, and how do you think it is internally represented? Arrays are quite efficient upon iteration, indexing and long lifetimes. Enumerables are a read-only interface which hides implementation from user. The goal here is to create X amount of tasks and wait for all of them to finish executing, thus there is no reason to hide the implementation via enumerables since we are the only people who are able to access the implementation.

OoLunar avatar Jun 11 '23 00:06 OoLunar

@OoLunar of course i did benchmark different solutions, as far as i remember there was no noticeable difference between your and my code, but original code was somewhat inefficient.
I pretty much know what Enumerable is, dont tell me that. by saying use overload i do not "hide the implementation", i use existing overload of Task.WhenAll method. also what implementation did i hide? filling an array? isnt it just your implementation?

rolandcheck avatar Jun 11 '23 00:06 rolandcheck

There's no noticeable difference because the bulk of the benchmark is waitinh on the tasks to complete, however, in a less contrived and more realistic scenario, LINQ vs manual construction can make a huge difference - and if your goal is to showcase efficient C# code, using an infamous performance hog (LINQ) doesn't accomplish that.

akiraveliara avatar Jun 11 '23 00:06 akiraveliara

@akiraveliara my goal is to create readable & efficient code for this particular problem. i know linq can be expensive in some cases, but it does not bother me as it really does not matter in this context

rolandcheck avatar Jun 11 '23 00:06 rolandcheck

This particular problem does not require readable code, it requires efficient code. You are welcome to create a separate pull request if you believe otherwise.

OoLunar avatar Jun 11 '23 01:06 OoLunar

@akiraveliara my goal is to create readable & efficient code for this particular problem. i know linq can be expensive in some cases, but it does not bother me as it really does not matter in this context

if your goal is to write both readable and efficient code you failed in both departments LINQ is simply not as intuitive - especially to people new to c# - as just using a plain old array, and due to the abstractions that are hidden behind the scenes when using IEnumerable you take a hit in performance. You wont get more efficient than just using an array. Even if it is just a few nanoseconds, this is supposed to be a benchmark, so even that magnitude matters.

JulianusIV avatar Jun 11 '23 01:06 JulianusIV