|
1 | 1 | #if NETFRAMEWORK |
2 | 2 | using System; |
| 3 | +#if NET45_OR_GREATER |
| 4 | +using System.Buffers; |
| 5 | +#endif |
3 | 6 | using System.Globalization; |
4 | 7 | using System.Reflection; |
| 8 | +#if NET40 |
| 9 | + |
| 10 | +using PolyfillsForOldDotNet.System.Buffers; |
| 11 | +#endif |
5 | 12 |
|
6 | 13 | using MsieJavaScriptEngine.Helpers; |
7 | 14 |
|
@@ -93,42 +100,92 @@ protected HostItemBase(Type type, object target, JsEngineMode engineMode, bool a |
93 | 100 | private static PropertyInfo[] GetAvailableProperties(PropertyInfo[] properties) |
94 | 101 | { |
95 | 102 | int propertyCount = properties.Length; |
96 | | - var availableProperties = new PropertyInfo[propertyCount]; |
97 | | - int availablePropertyIndex = 0; |
| 103 | + PropertyInfo[] availableProperties = null; |
| 104 | + int availablePropertyCount = 0; |
| 105 | + |
| 106 | + var propertyArrayPool = ArrayPool<PropertyInfo>.Shared; |
| 107 | + PropertyInfo[] buffer = propertyArrayPool.Rent(propertyCount); |
98 | 108 |
|
99 | | - for (int propertyIndex = 0; propertyIndex < propertyCount; propertyIndex++) |
| 109 | + try |
100 | 110 | { |
101 | | - PropertyInfo property = properties[propertyIndex]; |
102 | | - if (ReflectionHelpers.IsAllowedProperty(property)) |
| 111 | + foreach (PropertyInfo property in properties) |
103 | 112 | { |
104 | | - availableProperties[availablePropertyIndex] = property; |
105 | | - availablePropertyIndex++; |
| 113 | + if (ReflectionHelpers.IsAllowedProperty(property)) |
| 114 | + { |
| 115 | + availablePropertyCount++; |
| 116 | + |
| 117 | + int availablePropertyIndex = availablePropertyCount - 1; |
| 118 | + buffer[availablePropertyIndex] = property; |
| 119 | + } |
106 | 120 | } |
107 | | - } |
108 | 121 |
|
109 | | - Array.Resize(ref availableProperties, availablePropertyIndex); |
| 122 | + if (availablePropertyCount < propertyCount) |
| 123 | + { |
| 124 | + if (availablePropertyCount == 0) |
| 125 | + { |
| 126 | + return []; |
| 127 | + } |
| 128 | + |
| 129 | + availableProperties = new PropertyInfo[availablePropertyCount]; |
| 130 | + Array.Copy(buffer, availableProperties, availablePropertyCount); |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + availableProperties = properties; |
| 135 | + } |
| 136 | + } |
| 137 | + finally |
| 138 | + { |
| 139 | + bool clearArray = availablePropertyCount > 0; |
| 140 | + propertyArrayPool.Return(buffer, clearArray); |
| 141 | + } |
110 | 142 |
|
111 | 143 | return availableProperties; |
112 | 144 | } |
113 | 145 |
|
114 | 146 | private static MethodInfo[] GetAvailableMethods(MethodInfo[] methods, bool allowReflection) |
115 | 147 | { |
116 | 148 | int methodCount = methods.Length; |
117 | | - var availableMethods = new MethodInfo[methodCount]; |
118 | | - int availableMethodIndex = 0; |
| 149 | + MethodInfo[] availableMethods = null; |
| 150 | + int availableMethodCount = 0; |
| 151 | + |
| 152 | + var methodArrayPool = ArrayPool<MethodInfo>.Shared; |
| 153 | + MethodInfo[] buffer = methodArrayPool.Rent(methodCount); |
119 | 154 |
|
120 | | - for (int methodIndex = 0; methodIndex < methodCount; methodIndex++) |
| 155 | + try |
121 | 156 | { |
122 | | - MethodInfo method = methods[methodIndex]; |
123 | | - if (ReflectionHelpers.IsFullyFledgedMethod(method) |
124 | | - && (allowReflection || ReflectionHelpers.IsAllowedMethod(method))) |
| 157 | + foreach (MethodInfo method in methods) |
125 | 158 | { |
126 | | - availableMethods[availableMethodIndex] = method; |
127 | | - availableMethodIndex++; |
| 159 | + if (ReflectionHelpers.IsFullyFledgedMethod(method) |
| 160 | + && (allowReflection || ReflectionHelpers.IsAllowedMethod(method))) |
| 161 | + { |
| 162 | + availableMethodCount++; |
| 163 | + |
| 164 | + int availableMethodIndex = availableMethodCount - 1; |
| 165 | + buffer[availableMethodIndex] = method; |
| 166 | + } |
128 | 167 | } |
129 | | - } |
130 | 168 |
|
131 | | - Array.Resize(ref availableMethods, availableMethodIndex); |
| 169 | + if (availableMethodCount < methodCount) |
| 170 | + { |
| 171 | + if (availableMethodCount == 0) |
| 172 | + { |
| 173 | + return []; |
| 174 | + } |
| 175 | + |
| 176 | + availableMethods = new MethodInfo[availableMethodCount]; |
| 177 | + Array.Copy(buffer, availableMethods, availableMethodCount); |
| 178 | + } |
| 179 | + else |
| 180 | + { |
| 181 | + availableMethods = methods; |
| 182 | + } |
| 183 | + } |
| 184 | + finally |
| 185 | + { |
| 186 | + bool clearArray = availableMethodCount > 0; |
| 187 | + methodArrayPool.Return(buffer, clearArray); |
| 188 | + } |
132 | 189 |
|
133 | 190 | return availableMethods; |
134 | 191 | } |
|
0 commit comments