Skip to content

Commit c0b33db

Browse files
committed
perf: prevent unecessary boxing during JS interops
1 parent 2ffdd9f commit c0b33db

File tree

1 file changed

+27
-55
lines changed

1 file changed

+27
-55
lines changed

src/Runtime/Runtime/PublicAPI/Interop/Interop.cs

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections.Generic;
1818
using System.Reflection;
19+
using System.Runtime.CompilerServices;
1920
using System.Threading.Tasks;
2021
using OpenSilver.Internal;
2122

@@ -66,105 +67,76 @@ public static int DumpAllJavascriptObjectsEveryMs
6667

6768
private static T ConvertJavascriptResult<T>(object value)
6869
{
69-
object converted;
7070
Type t = typeof(T);
7171
if (t == typeof(string))
7272
{
73-
if (IsRunningInTheSimulator)
74-
converted = INTERNAL_JSObjectReference.ToString(value);
75-
else
76-
converted = Convert.ToString(value);
73+
string s = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToString(value) : Convert.ToString(value);
74+
return Unsafe.As<string, T>(ref s);
7775
}
7876
else if (t == typeof(double))
7977
{
80-
if (IsRunningInTheSimulator)
81-
converted = INTERNAL_JSObjectReference.ToDouble(value);
82-
else
83-
converted = Convert.ToDouble(value);
78+
double d = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToDouble(value) : Convert.ToDouble(value);
79+
return Unsafe.As<double, T>(ref d);
8480
}
8581
else if (t == typeof(int))
8682
{
87-
if (IsRunningInTheSimulator)
88-
converted = INTERNAL_JSObjectReference.ToInt32(value);
89-
else
90-
converted = Convert.ToInt32(value);
83+
int i = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToInt32(value) : Convert.ToInt32(value);
84+
return Unsafe.As<int, T>(ref i);
9185
}
9286
else if (t == typeof(bool))
9387
{
94-
if (IsRunningInTheSimulator)
95-
converted = INTERNAL_JSObjectReference.ToBoolean(value);
96-
else
97-
converted = Convert.ToBoolean(value);
88+
bool b = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToBoolean(value) : Convert.ToBoolean(value);
89+
return Unsafe.As<bool, T>(ref b);
9890
}
9991
else if (t == typeof(char))
10092
{
101-
if (IsRunningInTheSimulator)
102-
converted = INTERNAL_JSObjectReference.ToChar(value);
103-
else
104-
converted = Convert.ToChar(value);
93+
char c = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToChar(value) : Convert.ToChar(value);
94+
return Unsafe.As<char, T>(ref c);
10595
}
10696
else if (t == typeof(float))
10797
{
108-
if (IsRunningInTheSimulator)
109-
converted = INTERNAL_JSObjectReference.ToSingle(value);
110-
else
111-
converted = Convert.ToSingle(value);
98+
float f = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToSingle(value) : Convert.ToSingle(value);
99+
return Unsafe.As<float, T>(ref f);
112100
}
113101
else if (t == typeof(byte))
114102
{
115-
if (IsRunningInTheSimulator)
116-
converted = INTERNAL_JSObjectReference.ToByte(value);
117-
else
118-
converted = Convert.ToByte(value);
103+
byte b = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToByte(value) : Convert.ToByte(value);
104+
return Unsafe.As<byte, T>(ref b);
119105
}
120106
else if (t == typeof(uint))
121107
{
122-
if (IsRunningInTheSimulator)
123-
converted = INTERNAL_JSObjectReference.ToUInt32(value);
124-
else
125-
converted = Convert.ToUInt32(value);
108+
uint i = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToUInt32(value) : Convert.ToUInt32(value);
109+
return Unsafe.As<uint, T>(ref i);
126110
}
127111
else if (t == typeof(long))
128112
{
129-
if (IsRunningInTheSimulator)
130-
converted = INTERNAL_JSObjectReference.ToInt64(value);
131-
else
132-
converted = Convert.ToInt64(value);
113+
long l = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToInt64(value) : Convert.ToInt64(value);
114+
return Unsafe.As<long, T>(ref l);
133115
}
134116
else if (t == typeof(ulong))
135117
{
136-
if (IsRunningInTheSimulator)
137-
converted = INTERNAL_JSObjectReference.ToUInt64(value);
138-
else
139-
converted = Convert.ToUInt64(value);
118+
ulong l = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToUInt64(value) : Convert.ToUInt64(value);
119+
return Unsafe.As<ulong, T>(ref l);
140120
}
141121
else if (t == typeof(short))
142122
{
143-
if (IsRunningInTheSimulator)
144-
converted = INTERNAL_JSObjectReference.ToInt16(value);
145-
else
146-
converted = Convert.ToInt16(value);
123+
short s = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToInt16(value) : Convert.ToInt16(value);
124+
return Unsafe.As<short, T>(ref s);
147125
}
148126
else if (t == typeof(decimal))
149127
{
150-
if (IsRunningInTheSimulator)
151-
converted = INTERNAL_JSObjectReference.ToDecimal(value);
152-
else
153-
converted = Convert.ToDecimal(value);
128+
decimal d = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToDecimal(value) : Convert.ToDecimal(value);
129+
return Unsafe.As<decimal, T>(ref d);
154130
}
155131
else if (t == typeof(DateTime))
156132
{
157-
if (IsRunningInTheSimulator)
158-
converted = INTERNAL_JSObjectReference.ToDateTime(value);
159-
else
160-
converted = Convert.ToDateTime(value);
133+
DateTime d = IsRunningInTheSimulator ? INTERNAL_JSObjectReference.ToDateTime(value) : Convert.ToDateTime(value);
134+
return Unsafe.As<DateTime, T>(ref d);
161135
}
162136
else
163137
{
164138
throw new ArgumentException($"Type '{t.FullName}' is not supported.");
165139
}
166-
167-
return (T)converted;
168140
}
169141

170142
public static T1 ExecuteJavaScriptGetResult<T1>(string javascript)

0 commit comments

Comments
 (0)