-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBranchRenderBenchmarks.cs
More file actions
66 lines (57 loc) · 2.91 KB
/
Copy pathBranchRenderBenchmarks.cs
File metadata and controls
66 lines (57 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using BenchmarkDotNet.Attributes;
using Heddle;
using Heddle.Runtime;
namespace Heddle.Performance
{
/// <summary>
/// Phase 3 branching allocation/perf guards (spec D15 / criterion 8), run with
/// <c>dotnet run -c Release -- --filter *BranchRenderBenchmarks*</c>.
/// <list type="bullet">
/// <item><c>RenderListNoBranches</c> / <c>RenderListIfPair10K</c> — no channel participant: 0 B frame
/// delta vs the pre-phase baseline (guards the D2 fast path + D7 opportunistic no-op).</item>
/// <item><c>RenderListIfElse10K</c> — one <c>ScopeLocals</c> per iteration (the documented worst-case
/// trade, ~320 000 B on x64 with no JIT rescue; newer runtimes stack-allocate part of it).</item>
/// <item><c>RenderFlagshipNoPublish</c> — a realistic never-publishes document: 0 B frame delta.</item>
/// </list>
/// </summary>
[MemoryDiagnoser]
public class BranchRenderBenchmarks
{
public class Cell { public bool F { get; set; } public string Name { get; set; } }
public class ListModel { public List<Cell> Items { get; set; } }
public class FlagModel { public bool IsFeatured { get; set; } public bool IsArchived { get; set; } }
private HeddleTemplate _noBranches;
private HeddleTemplate _ifPair;
private HeddleTemplate _ifElse;
private HeddleTemplate _flagship;
private ListModel _listModel;
private FlagModel _flagModel;
[GlobalSetup]
public void Setup()
{
HeddleTemplate.Configure(typeof(BranchRenderBenchmarks).GetTypeInfo().Assembly);
_noBranches = new HeddleTemplate("@list(Items){{@(Name)}}", new CompileContext(typeof(ListModel)));
_ifPair = new HeddleTemplate("@list(Items){{@if(F){{@(Name)}}@ifnot(F){{-}}}}", new CompileContext(typeof(ListModel)));
_ifElse = new HeddleTemplate("@list(Items){{@if(F){{@(Name)}}@else(){{-}}}}", new CompileContext(typeof(ListModel)));
_flagship = new HeddleTemplate(
"<div>@if(IsFeatured){{<b>F</b>}}@ifnot(IsFeatured){{@if(IsArchived){{<i>A</i>}}@ifnot(IsArchived){{R}}}}</div>",
new CompileContext(typeof(FlagModel)));
_listModel = new ListModel
{
Items = Enumerable.Range(0, 10000).Select(i => new Cell { F = i % 2 == 0, Name = "n" + i }).ToList()
};
_flagModel = new FlagModel { IsFeatured = false, IsArchived = true };
}
[Benchmark]
public string RenderListNoBranches() => _noBranches.Generate(_listModel);
[Benchmark]
public string RenderListIfPair10K() => _ifPair.Generate(_listModel);
[Benchmark]
public string RenderListIfElse10K() => _ifElse.Generate(_listModel);
[Benchmark]
public string RenderFlagshipNoPublish() => _flagship.Generate(_flagModel);
}
}