Skip to content

Commit fe8ae98

Browse files
authored
[wasm] Improve exception handling measurements (#52846)
Add conditional throw to the leaf methods to avoid optimizing out the exception handling code in `TryCatch` measurement. Add new `NoExceptionHandling` measurement to measure case when no exception handling is involved. Example measurement, chrome/amd64: | measurement | AOT | interp | |-:|-:|-:| | Exceptions, NoExceptionHandling | 0.0164us | 0.0558us | | Exceptions, TryCatch | 0.1400us | 0.0592us | | Exceptions, TryCatchThrow | 0.0064ms | 0.0028ms | | Exceptions, TryCatchFilter | 0.4415us | 0.0645us | | Exceptions, TryCatchFilterInline | 0.1263us | 0.0527us | | Json, non-ASCII text serialize | 1.4475ms | 8.6952ms | | Json, non-ASCII text deserialize | 6.5332ms | 12.2220ms | | Json, small serialize | 0.2020ms | 0.2362ms | | Json, small deserialize | 0.2293ms | 0.3614ms | | Json, large serialize | 53.3021ms | 68.0000ms | | Json, large deserialize | 61.5176ms | 100.0377ms |
1 parent 24544e0 commit fe8ae98

1 file changed

Lines changed: 45 additions & 3 deletions

File tree

src/mono/sample/wasm/browser-bench/Exceptions.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class ExceptionsTask : BenchTask
1515
public ExceptionsTask()
1616
{
1717
measurements = new Measurement[] {
18+
new NoExceptionHandling(),
1819
new TryCatch(),
1920
new TryCatchThrow(),
2021
new TryCatchFilter(),
@@ -41,10 +42,32 @@ public abstract class ExcMeasurement : BenchTask.Measurement
4142
public override int InitialSamples => 10000;
4243
}
4344

45+
class NoExceptionHandling : ExcMeasurement
46+
{
47+
public override string Name => "NoExceptionHandling";
48+
public override int InitialSamples => 1000000;
49+
bool increaseCounter = false;
50+
int unusedCounter;
51+
52+
public override void RunStep()
53+
{
54+
DoNothing();
55+
}
56+
57+
[MethodImpl(MethodImplOptions.NoInlining)]
58+
void DoNothing ()
59+
{
60+
if (increaseCounter)
61+
unusedCounter++;
62+
}
63+
}
64+
4465
class TryCatch : ExcMeasurement
4566
{
4667
public override string Name => "TryCatch";
4768
public override int InitialSamples => 1000000;
69+
bool doThrow = false;
70+
4871
public override void RunStep()
4972
{
5073
try
@@ -58,12 +81,16 @@ public override void RunStep()
5881
[MethodImpl(MethodImplOptions.NoInlining)]
5982
void DoNothing ()
6083
{
84+
if (doThrow)
85+
throw new Exception ("Reached DoThrow and throwed");
6186
}
6287
}
6388

6489
class TryCatchThrow : ExcMeasurement
6590
{
6691
public override string Name => "TryCatchThrow";
92+
bool doThrow = true;
93+
6794
public override void RunStep()
6895
{
6996
try
@@ -78,13 +105,16 @@ public override void RunStep()
78105
[MethodImpl(MethodImplOptions.NoInlining)]
79106
void DoThrow()
80107
{
81-
throw new System.Exception("Reached DoThrow and throwed");
108+
if (doThrow)
109+
throw new System.Exception("Reached DoThrow and throwed");
82110
}
83111
}
84112

85113
class TryCatchFilter : ExcMeasurement
86114
{
87115
public override string Name => "TryCatchFilter";
116+
bool doThrow = false;
117+
88118
public override void RunStep()
89119
{
90120
try
@@ -99,12 +129,16 @@ public override void RunStep()
99129
[MethodImpl(MethodImplOptions.NoInlining)]
100130
void DoNothing()
101131
{
132+
if (doThrow)
133+
throw new Exception("Reached DoThrow and throwed");
102134
}
103135
}
104136

105137
class TryCatchFilterInline : ExcMeasurement
106138
{
107139
public override string Name => "TryCatchFilterInline";
140+
bool doThrow = false;
141+
108142
public override void RunStep()
109143
{
110144
try
@@ -119,12 +153,16 @@ public override void RunStep()
119153
[MethodImpl(MethodImplOptions.AggressiveInlining)]
120154
void DoNothing()
121155
{
156+
if (doThrow)
157+
throw new Exception("Reached DoThrow and throwed");
122158
}
123159
}
124160

125161
class TryCatchFilterThrow : ExcMeasurement
126162
{
127163
public override string Name => "TryCatchFilterThrow";
164+
bool doThrow = true;
165+
128166
public override void RunStep()
129167
{
130168
try
@@ -142,13 +180,16 @@ public override void RunStep()
142180
[MethodImpl(MethodImplOptions.NoInlining)]
143181
void DoThrow()
144182
{
145-
throw new System.Exception("Reached DoThrow and throwed");
183+
if (doThrow)
184+
throw new System.Exception("Reached DoThrow and throwed");
146185
}
147186
}
148187

149188
class TryCatchFilterThrowApplies : ExcMeasurement
150189
{
151190
public override string Name => "TryCatchFilterThrowApplies";
191+
bool doThrow = true;
192+
152193
public override void RunStep()
153194
{
154195
try
@@ -163,7 +204,8 @@ public override void RunStep()
163204
[MethodImpl(MethodImplOptions.NoInlining)]
164205
void DoThrow()
165206
{
166-
throw new System.Exception("Reached DoThrow and throwed");
207+
if (doThrow)
208+
throw new System.Exception("Reached DoThrow and throwed");
167209
}
168210
}
169211
}

0 commit comments

Comments
 (0)