|
2 | 2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | 3 | // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
|
| 5 | +import 'dart:_foreign_helper' show JS; |
5 | 6 | import 'dart:_internal' show patch; |
6 | 7 | import 'dart:_native_typed_data'; |
7 | | -import "dart:_internal" show UnmodifiableListBase; |
8 | 8 |
|
9 | 9 | @patch |
10 | 10 | class ByteData { |
@@ -193,3 +193,168 @@ class Float64x2 { |
193 | 193 | @patch |
194 | 194 | factory Float64x2.fromFloat32x4(Float32x4 v) = NativeFloat64x2.fromFloat32x4; |
195 | 195 | } |
| 196 | + |
| 197 | +// Each integer typed array type has a dedicated, strictly monomorphic comparison |
| 198 | +// loop. This keeps V8's Inline Cache at `#[#] !== #[#]` strictly monomorphic |
| 199 | +// (IC size = 1), avoiding polymorphic/megamorphic transitions, deoptimizations, |
| 200 | +// and runtime type checks. Unused types can also be tree-shaken by dart2js. |
| 201 | +@patch |
| 202 | +@pragma('dart2js:prefer-inline') |
| 203 | +bool _uint8ListRangeEquals( |
| 204 | + Uint8List a, |
| 205 | + int aStart, |
| 206 | + Uint8List b, |
| 207 | + int bStart, |
| 208 | + int count, |
| 209 | +) { |
| 210 | + for (int i = 0; i < count; i++) { |
| 211 | + if (JS<bool>('bool', '#[#] !== #[#]', a, aStart + i, b, bStart + i)) { |
| 212 | + return false; |
| 213 | + } |
| 214 | + } |
| 215 | + return true; |
| 216 | +} |
| 217 | + |
| 218 | +@patch |
| 219 | +@pragma('dart2js:prefer-inline') |
| 220 | +bool _int8ListRangeEquals( |
| 221 | + Int8List a, |
| 222 | + int aStart, |
| 223 | + Int8List b, |
| 224 | + int bStart, |
| 225 | + int count, |
| 226 | +) { |
| 227 | + for (int i = 0; i < count; i++) { |
| 228 | + if (JS<bool>('bool', '#[#] !== #[#]', a, aStart + i, b, bStart + i)) { |
| 229 | + return false; |
| 230 | + } |
| 231 | + } |
| 232 | + return true; |
| 233 | +} |
| 234 | + |
| 235 | +@patch |
| 236 | +@pragma('dart2js:prefer-inline') |
| 237 | +bool _uint8ClampedListRangeEquals( |
| 238 | + Uint8ClampedList a, |
| 239 | + int aStart, |
| 240 | + Uint8ClampedList b, |
| 241 | + int bStart, |
| 242 | + int count, |
| 243 | +) { |
| 244 | + for (int i = 0; i < count; i++) { |
| 245 | + if (JS<bool>('bool', '#[#] !== #[#]', a, aStart + i, b, bStart + i)) { |
| 246 | + return false; |
| 247 | + } |
| 248 | + } |
| 249 | + return true; |
| 250 | +} |
| 251 | + |
| 252 | +@patch |
| 253 | +@pragma('dart2js:prefer-inline') |
| 254 | +bool _uint16ListRangeEquals( |
| 255 | + Uint16List a, |
| 256 | + int aStart, |
| 257 | + Uint16List b, |
| 258 | + int bStart, |
| 259 | + int count, |
| 260 | +) { |
| 261 | + for (int i = 0; i < count; i++) { |
| 262 | + if (JS<bool>('bool', '#[#] !== #[#]', a, aStart + i, b, bStart + i)) { |
| 263 | + return false; |
| 264 | + } |
| 265 | + } |
| 266 | + return true; |
| 267 | +} |
| 268 | + |
| 269 | +@patch |
| 270 | +@pragma('dart2js:prefer-inline') |
| 271 | +bool _int16ListRangeEquals( |
| 272 | + Int16List a, |
| 273 | + int aStart, |
| 274 | + Int16List b, |
| 275 | + int bStart, |
| 276 | + int count, |
| 277 | +) { |
| 278 | + for (int i = 0; i < count; i++) { |
| 279 | + if (JS<bool>('bool', '#[#] !== #[#]', a, aStart + i, b, bStart + i)) { |
| 280 | + return false; |
| 281 | + } |
| 282 | + } |
| 283 | + return true; |
| 284 | +} |
| 285 | + |
| 286 | +@patch |
| 287 | +@pragma('dart2js:prefer-inline') |
| 288 | +bool _uint32ListRangeEquals( |
| 289 | + Uint32List a, |
| 290 | + int aStart, |
| 291 | + Uint32List b, |
| 292 | + int bStart, |
| 293 | + int count, |
| 294 | +) { |
| 295 | + for (int i = 0; i < count; i++) { |
| 296 | + if (JS<bool>('bool', '#[#] !== #[#]', a, aStart + i, b, bStart + i)) { |
| 297 | + return false; |
| 298 | + } |
| 299 | + } |
| 300 | + return true; |
| 301 | +} |
| 302 | + |
| 303 | +@patch |
| 304 | +@pragma('dart2js:prefer-inline') |
| 305 | +bool _int32ListRangeEquals( |
| 306 | + Int32List a, |
| 307 | + int aStart, |
| 308 | + Int32List b, |
| 309 | + int bStart, |
| 310 | + int count, |
| 311 | +) { |
| 312 | + for (int i = 0; i < count; i++) { |
| 313 | + if (JS<bool>('bool', '#[#] !== #[#]', a, aStart + i, b, bStart + i)) { |
| 314 | + return false; |
| 315 | + } |
| 316 | + } |
| 317 | + return true; |
| 318 | +} |
| 319 | + |
| 320 | +@patch |
| 321 | +@pragma('dart2js:prefer-inline') |
| 322 | +bool _byteDataRangeEquals( |
| 323 | + ByteData a, |
| 324 | + int aStart, |
| 325 | + ByteData b, |
| 326 | + int bStart, |
| 327 | + int count, |
| 328 | +) { |
| 329 | + int i = 0; |
| 330 | + // Compare 32 bits at a time in little-endian byte order. Equality is byte-order |
| 331 | + // invariant, and all supported modern JS runtimes run on little-endian hardware. |
| 332 | + // Passing a constant `true` enables the JS engine to emit a single direct load |
| 333 | + // instruction without endian branching or BigInt overhead. |
| 334 | + final limit32 = count - 4; |
| 335 | + for (; i <= limit32; i += 4) { |
| 336 | + if (JS<bool>( |
| 337 | + 'bool', |
| 338 | + '#.getUint32(#, true) !== #.getUint32(#, true)', |
| 339 | + a, |
| 340 | + aStart + i, |
| 341 | + b, |
| 342 | + bStart + i, |
| 343 | + )) { |
| 344 | + return false; |
| 345 | + } |
| 346 | + } |
| 347 | + for (; i < count; i++) { |
| 348 | + if (JS<bool>( |
| 349 | + 'bool', |
| 350 | + '#.getUint8(#) !== #.getUint8(#)', |
| 351 | + a, |
| 352 | + aStart + i, |
| 353 | + b, |
| 354 | + bStart + i, |
| 355 | + )) { |
| 356 | + return false; |
| 357 | + } |
| 358 | + } |
| 359 | + return true; |
| 360 | +} |
0 commit comments