public static async Task <Int32> LoadFromFileAndCache(this ValueTasks runnable, String key)
    {
        const String fileName = @"Values.txt";

        if (!File.Exists(fileName))
        {
            using (var stream = new StreamWriter(File.OpenWrite(fileName)))
                await stream.WriteLineAsync($"{key};10");
        }

        using (var stream = File.OpenText(fileName))
        {
            String line;
            while ((line = await stream.ReadLineAsync()) != null)
            {
                var splitted = line.Split(Convert.ToChar(";"));
                var k        = splitted[0];
                var v        = Convert.ToInt32(splitted[1]);

                if (k != key)
                {
                    continue;
                }

                runnable.cachedValues.TryAdd(k, v);
                return(v);
            }
        }

        return(0);
    }
Ejemplo n.º 2
0
        public async void TestWhenAny()
        {
            Debug.Log(System.DateTime.Now);
            var res = await ValueTasks.WhenAny(WaitSeconds(1), WaitSeconds(2), WaitSeconds(3));

            Debug.Log(System.DateTime.Now);
            Assert.True(res.index == 0);
        }
    public static async Task LoopTenTimesAndSumResult(this ValueTasks runnable, Func <Int32, Task <Int32> > action)
    {
        var total = 0;

        for (var i = 0; i < 10; i++)
        {
            total += await action(i);
        }
        Console.WriteLine($"Result {total}");
    }
    public static void Explain(this ValueTasks runnable, TextWriter writer)
    {
        writer.WriteLine(@"
- Nice for highperf scenarios and only then!
- Complex to use and easy to get wrong
- Stats:

 |                   Method | Repeats |        Mean |      Error |       StdDev |      Median | Scaled | ScaledSD |   Gen 0 | Allocated |
 |------------------------- |-------- |------------:|-----------:|-------------:|------------:|-------:|---------:|--------:|----------:|
 |          **ConsumeTask** |    1000 |  9,307.1 ns | 396.345 ns | 1,091.649 ns |  9,501.1 ns |   2.00 |     0.60 | 11.4441 |   72072 B |
 |    ConsumeValueTaskWrong |    1000 | 11,073.7 ns | 468.996 ns | 1,382.844 ns | 10,329.0 ns |   2.38 |     0.73 |       - |       0 B |
 | ConsumeValueTaskProperly |    1000 |  5,075.2 ns | 543.450 ns | 1,602.374 ns |  4,455.4 ns |   1.00 |     0.00 |       - |       0 B |
 |    ConsumeValueTaskCrazy |    1000 |  4,140.6 ns | 211.741 ns |   604.109 ns |  4,201.2 ns |   0.89 |     0.28 |       - |       0 B |        

https://github.com/adamsitnik/StateOfTheDotNetPerformance        
");
    }
    public static async Task <int> LoadFromFileAndCache(this ValueTasks runnable, string key)
    {
        using (var stream = File.OpenText(@"Values.txt"))
        {
            string line;
            while ((line = await stream.ReadLineAsync()) != null)
            {
                var splitted = line.Split(Convert.ToChar(";"));
                var k        = splitted[0];
                var v        = Convert.ToInt32(splitted[1]);

                if (k != key)
                {
                    continue;
                }

                runnable.cachedValues.TryAdd(k, v);
                return(v);
            }
        }
        return(0);
    }
Ejemplo n.º 6
0
    public static void Explain(this ValueTasks runnable, TextWriter writer)
    {
        writer.WriteLine(@"
- Nice for highperf scenarios and only then!
- It's not about replacing Task
- Has a single purpose: Reduce heap allocations on the hot path where common synchronous execution is possible
- Do not
 - Await the instance multiple times
 - Call `AsTask` multiple times
 - Use `.Result` or `.GetAwaiter().GetResult()` when not yet completed
 - Use more than one of these techniques to consume the instance
- Complex to use and easy to get wrong
- Stats:

|                   Method | Repeats |        Mean |       Error |      StdDev |      Median | Ratio | RatioSD |   Gen 0 | Gen 1 | Gen 2 | Allocated |
|------------------------- |-------- |------------:|------------:|------------:|------------:|------:|--------:|--------:|------:|------:|----------:|
|              ConsumeTask |    1000 | 13,068.1 ns | 1,537.87 ns | 4,437.11 ns | 10,850.4 ns |  3.08 |    0.95 | 17.2119 |     - |     - |   72072 B |
|    ConsumeValueTaskWrong |    1000 | 13,884.2 ns |   549.64 ns | 1,523.06 ns | 13,473.2 ns |  3.11 |    0.50 |       - |     - |     - |         - |
| ConsumeValueTaskProperly |    1000 |  4,567.8 ns |    90.81 ns |   133.11 ns |  4,543.2 ns |  1.00 |    0.00 |       - |     - |     - |         - |
|    ConsumeValueTaskCrazy |    1000 |  3,380.4 ns |    67.19 ns |    74.69 ns |  3,371.6 ns |  0.74 |    0.03 |       - |     - |     - |         - |     

https://github.com/adamsitnik/StateOfTheDotNetPerformance        
");
    }
Ejemplo n.º 7
0
        void Method()
        {
            Func <ValueTask> Method = async delegate
            {
                await ValueTasks.Delay(10);

                await ValueTasks.Delay(20);
            };

            Func <ValueTask> Method2 = async delegate
            {
                if (Environment.UserInteractive)
                {
                    await ValueTasks.Delay(10);
                }

                await ValueTasks.Delay(20);

                int LocalFunction()
                {
                    return(4);
                }
            };

            Func <ValueTask> Method3 = async delegate { await ValueTasks.Delay(10); };

            Func <ValueTask <int> > Method4 = async delegate
            {
                if (Environment.UserInteractive)
                {
                    return(await ValueTasks.FromResult(3));
                }

                await ValueTasks.Delay(10);

                return(await ValueTasks.FromResult(4));
            };

            Func <ValueTask <int> > Method5 = async delegate
            {
                await ValueTasks.Delay(10);

                return(await ValueTasks.FromResult(3));

                int LocalFunction() => 4;
            };

            Func <ValueTask> Method6 = async delegate { await ValueTasks.FromResult(3); };

            Func <ValueTask <int> > Method7 = async delegate { return(await ValueTasks.FromResult(3)); };

            Func <ValueTask <int> > Method_AwaitNonLast = async delegate
            {
                await ValueTasks.Delay(10);

                await ValueTasks.Delay(20);

                return(3);
            };

            Func <ValueTask> Method2_AwaitNonLast = async delegate
            {
                using (new Process())
                {
                    await ValueTasks.Delay(10);

                    await ValueTasks.Delay(20);
                }
            };

            Func <ValueTask <int> > Method3_AwaitNonLast = async delegate
            {
                await ValueTasks.Delay(10);

                var result = await ValueTasks.FromResult(3);

                return(result);
            };

            Func <ValueTask> Method_WithConfigureAwait = async delegate { await ValueTasks.Delay(10).ConfigureAwait(false); };

            Func <ValueTask> Method_WithConfigureAwait_AsExpressionBodied = async delegate { await ValueTasks.Delay(10).ConfigureAwait(false); };

            Func <ValueTask <int> > Method_NestedInUsingScope = async delegate
            {
                using (new Process())
                {
                    return(await ValueTasks.FromResult(3));
                }
            };

            Func <ValueTask <int> > Method_NestedInUsingScope = async delegate(int x)
            {
                using (new Process())
                {
                    if (x > 2)
                    {
                        return(await ValueTasks.FromResult(3));
                    }
                }
            };

            Func <ValueTask <int> > Method_UsingDeclaration = async delegate
            {
                using var p = new Process();

                return(await ValueTasks.FromResult(3));
            };

            Func <ValueTask <int> > Method_UsingDeclaration = async delegate(int x)
            {
                using var p = new Process();

                if (x > 2)
                {
                    return(await ValueTasks.FromResult(3));
                }
            };

            Func <ValueTask <int> > Method_NestedInTryBlock = async delegate
            {
                try
                {
                    return(await ValueTasks.FromResult(3));
                }
                catch
                {
                    throw;
                }
            };

            Func <ValueTask <int> > Method_NestedInTryBlock = async delegate(int x)
            {
                try
                {
                    if (x > 2)
                    {
                        return(await ValueTasks.FromResult(3));
                    }
                }
                catch
                {
                    throw;
                }
            };
        }
    }
 public static void PrintFastPath(this ValueTasks runnable, Int32 i)
 {
     Console.WriteLine($"Fast path {i}.");
 }
 public static void PrintAsyncPath(this ValueTasks runnable, Int32 i)
 {
     Console.WriteLine($"Async path {i}.");
 }