summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Performance/CodeQuality/Inlining/ConstantArgsString.cs
blob: b7319086d722c38c940d089fee5130debf6227c2 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

// Strings are the only ref class where there is built-in support for
// constant objects.

using Microsoft.Xunit.Performance;
using System;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Reflection;
using System.Collections.Generic;
using Xunit;

[assembly: OptimizeForBenchmarks]
[assembly: MeasureInstructionsRetired]

public static class ConstantArgsString
{

#if DEBUG
    public const int Iterations = 1;
#else
    public const int Iterations = 100000;
#endif

    // Ints feeding math operations.
    //
    // Inlining in Bench0xp should enable constant folding
    // Inlining in Bench0xn will not enable constant folding

    static string Ten = "Ten";
    static string Five = "Five";

    static string Id(string x)
    {
        return x;
    }

    static char F00(string x)
    {
        return x[0];
    }

    static bool Bench00p()
    {
        string t = "Ten";
        char f = F00(t);
        return (f == 'T');
    }

    static bool Bench00n()
    {
        string t = Ten;
        char f = F00(t);
        return (f == 'T');
    }

    static int F01(string x)
    {
        return x.Length;
    }

    static bool Bench01p()
    {
        string t = "Ten";
        int f = F01(t);
        return (f == 3);
    }

    static bool Bench01n()
    {
        string t = Ten;
        int f = F01(t);
        return (f == 3);
    }

    static bool Bench01p1()
    {
        return "Ten".Length == 3;
    }

    static bool Bench01n1()
    {
        return Ten.Length == 3;
    }

    static bool Bench02p()
    {
        return "Ten".Equals("Ten", StringComparison.Ordinal);
    }

    static bool Bench02n()
    {
        return "Ten".Equals(Ten, StringComparison.Ordinal);
    }

    static bool Bench02n1()
    {
        return Ten.Equals("Ten", StringComparison.Ordinal);
    }

    static bool Bench02n2()
    {
        return Ten.Equals(Ten, StringComparison.Ordinal);
    }

    static bool Bench03p()
    {
        return "Ten" == "Ten";
    }

    static bool Bench03n()
    {
        return "Ten" == Ten;
    }

    static bool Bench03n1()
    {
        return Ten == "Ten";
    }

    static bool Bench03n2()
    {
        return Ten == Ten;
    }

    static bool Bench04p()
    {
        return "Ten" != "Five";
    }

    static bool Bench04n()
    {
        return "Ten" != Five;
    }

    static bool Bench04n1()
    {
        return Ten != "Five";
    }

    static bool Bench04n2()
    {
        return Ten != Five;
    }

    static bool Bench05p()
    {
        string t = "Ten";
        return (t == t);
    }

    static bool Bench05n()
    {
        string t = Ten;
        return (t == t);
    }

    static bool Bench06p()
    {
        return "Ten" != null;
    }

    static bool Bench06n()
    {
        return Ten != null;
    }

    static bool Bench07p()
    {
        return !"Ten".Equals(null);
    }

    static bool Bench07n()
    {
        return !Ten.Equals(null);
    }

    static bool Bench08p()
    {
        return !"Ten".Equals("Five", StringComparison.Ordinal);
    }

    static bool Bench08n()
    {
        return !"Ten".Equals(Five, StringComparison.Ordinal);
    }

    static bool Bench08n1()
    {
        return !Ten.Equals("Five", StringComparison.Ordinal);
    }

    static bool Bench08n2()
    {
        return !Ten.Equals(Five, StringComparison.Ordinal);
    }

    static bool Bench09p()
    {
        return string.Equals("Ten", "Ten");
    }

    static bool Bench09n()
    {
        return string.Equals("Ten", Ten);
    }

    static bool Bench09n1()
    {
        return string.Equals(Ten, "Ten");
    }

    static bool Bench09n2()
    {
        return string.Equals(Ten, Ten);
    }

    static bool Bench10p()
    {
        return !string.Equals("Five", "Ten");
    }

    static bool Bench10n()
    {
        return !string.Equals(Five, "Ten");
    }

    static bool Bench10n1()
    {
        return !string.Equals("Five", Ten);
    }

    static bool Bench10n2()
    {
        return !string.Equals(Five, Ten);
    }

    static bool Bench11p()
    {
        return !string.Equals("Five", null);
    }

    static bool Bench11n()
    {
        return !string.Equals(Five, null);
    }

    private static IEnumerable<object[]> MakeArgs(params string[] args)
    {
        return args.Select(arg => new object[] { arg });
    }

    public static IEnumerable<object[]> TestFuncs = MakeArgs(
        "Bench00p",  "Bench00n",
        "Bench01p",  "Bench01n",
        "Bench01p1", "Bench01n1",
        "Bench02p",  "Bench02n",
        "Bench02n1", "Bench02n2",
        "Bench03p",  "Bench03n",
        "Bench03n1", "Bench03n2",
        "Bench04p",  "Bench04n",
        "Bench04n1", "Bench04n2",
        "Bench05p",  "Bench05n",
        "Bench06p",  "Bench06n",
        "Bench07p",  "Bench07n",
        "Bench08p",  "Bench08n",
        "Bench08n1", "Bench08n2",
        "Bench09p",  "Bench09n",
        "Bench09n1", "Bench09n2",
        "Bench10p",  "Bench10n",
        "Bench10n1", "Bench10n2",
        "Bench11p",  "Bench11n"
    );

    static Func<bool> LookupFunc(object o)
    {
        TypeInfo t = typeof(ConstantArgsString).GetTypeInfo();
        MethodInfo m = t.GetDeclaredMethod((string) o);
        return m.CreateDelegate(typeof(Func<bool>)) as Func<bool>;
    }

    [Benchmark]
    [MemberData(nameof(TestFuncs))]
    public static void Test(object funcName)
    {
        Func<bool> f = LookupFunc(funcName);
        foreach (var iteration in Benchmark.Iterations)
        {
            using (iteration.StartMeasurement())
            {
                for (int i = 0; i < Iterations; i++)
                {
                    f();
                }
            }
        }
    }

    static bool TestBase(Func<bool> f)
    {
        bool result = true;
        for (int i = 0; i < Iterations; i++)
        {
            result &= f();
        }
        return result;
    }

    public static int Main()
    {
        bool result = true;

        foreach(object[] o in TestFuncs)
        {
            string funcName = (string) o[0];
            Func<bool> func = LookupFunc(funcName);
            bool thisResult = TestBase(func);
            if (!thisResult)
            {
                Console.WriteLine("{0} failed", funcName);
            }
            result &= thisResult;
        }

        return (result ? 100 : -1);
    }
}