From 8e28c32bee0579c51247d13ff36464127825dc0d Mon Sep 17 00:00:00 2001 From: Ilya Gyrdymov Date: Fri, 7 Jun 2024 22:38:37 +0300 Subject: [PATCH 1/2] Speed up Vector.randomFilled --- CHANGELOG.md | 4 ++++ .../float32x4_random_filled.dart | 3 +-- lib/src/vector/float32x4_vector.dart | 21 ++++++++++++------- lib/src/vector/float64x2_vector.g.dart | 17 ++++++++------- pubspec.yaml | 2 +- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c336ad5..3abf496b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 13.12.5 +- `Vector.randomFilled`: + - factory-constructor speed-up + ## 13.12.4 - `Vector.filled`: - factory-constructor speed-up diff --git a/benchmark/vector/float32/vector_initializing/float32x4_random_filled.dart b/benchmark/vector/float32/vector_initializing/float32x4_random_filled.dart index ea462ad6..3b8c5406 100644 --- a/benchmark/vector/float32/vector_initializing/float32x4_random_filled.dart +++ b/benchmark/vector/float32/vector_initializing/float32x4_random_filled.dart @@ -1,5 +1,4 @@ -// Approx. 1.7 second (MacBook Pro 2019), Dart version: 2.16.0 -// Approx. 2.5 sec (MacBook Air mid 2017), Dart VM version: 2.16.0 +// Approx. 2.8 second (MacBook Pro 2019), Dart version: 3.2.4 import 'package:benchmark_harness/benchmark_harness.dart'; import 'package:ml_linalg/vector.dart'; diff --git a/lib/src/vector/float32x4_vector.dart b/lib/src/vector/float32x4_vector.dart index 6a170c0d..6649b76b 100644 --- a/lib/src/vector/float32x4_vector.dart +++ b/lib/src/vector/float32x4_vector.dart @@ -32,12 +32,12 @@ class Float32x4Vector with IterableMixin implements Vector { : length = source.length { _numOfBuckets = _getNumOfBuckets(source.length, _bucketSize); - final typedList = Float32List(_numOfBuckets * _bucketSize); + final list = Float32List(_numOfBuckets * _bucketSize); - _buffer = typedList.buffer; + _buffer = list.buffer; for (var i = 0; i < length; i++) { - typedList[i] = source[i].toDouble(); + list[i] = source[i].toDouble(); } } @@ -54,16 +54,21 @@ class Float32x4Vector with IterableMixin implements Vector { 'Argument `min` should be less than `max`, min: $min, max: $max'); } + if (length < 0) { + throw ArgumentError('Length cannot be negative'); + } + _numOfBuckets = _getNumOfBuckets(length, _bucketSize); - final byteData = ByteData(_numOfBuckets * _bytesPerSimdElement); - _buffer = byteData.buffer; + + final list = Float32List(_numOfBuckets * _bucketSize); + + _buffer = list.buffer; final generator = math.Random(seed); final diff = max - min; for (var i = 0; i < length; i++) { - byteData.setFloat32(_bytesPerElement * i, - generator.nextDouble() * diff + min, Endian.host); + list[i] = generator.nextDouble() * diff + min; } } @@ -74,7 +79,9 @@ class Float32x4Vector with IterableMixin implements Vector { } _numOfBuckets = _getNumOfBuckets(length, _bucketSize); + final list = Float32x4List(_numOfBuckets); + _buffer = list.buffer; final simdValue = Float32x4.splat(value.toDouble()); diff --git a/lib/src/vector/float64x2_vector.g.dart b/lib/src/vector/float64x2_vector.g.dart index b235e825..5b027cec 100644 --- a/lib/src/vector/float64x2_vector.g.dart +++ b/lib/src/vector/float64x2_vector.g.dart @@ -35,12 +35,12 @@ class Float64x2Vector with IterableMixin implements Vector { : length = source.length { _numOfBuckets = _getNumOfBuckets(source.length, _bucketSize); - final typedList = Float64List(_numOfBuckets * _bucketSize); + final list = Float64List(_numOfBuckets * _bucketSize); - _buffer = typedList.buffer; + _buffer = list.buffer; for (var i = 0; i < length; i++) { - typedList[i] = source[i].toDouble(); + list[i] = source[i].toDouble(); } } @@ -58,15 +58,16 @@ class Float64x2Vector with IterableMixin implements Vector { } _numOfBuckets = _getNumOfBuckets(length, _bucketSize); - final byteData = ByteData(_numOfBuckets * _bytesPerSimdElement); - _buffer = byteData.buffer; + + final list = Float64List(_numOfBuckets * _bucketSize); + + _buffer = list.buffer; final generator = math.Random(seed); final diff = max - min; for (var i = 0; i < length; i++) { - byteData.setFloat64(_bytesPerElement * i, - generator.nextDouble() * diff + min, Endian.host); + list[i] = generator.nextDouble() * diff + min; } } @@ -77,7 +78,9 @@ class Float64x2Vector with IterableMixin implements Vector { } _numOfBuckets = _getNumOfBuckets(length, _bucketSize); + final list = Float64x2List(_numOfBuckets); + _buffer = list.buffer; final simdValue = Float64x2.splat(value.toDouble()); diff --git a/pubspec.yaml b/pubspec.yaml index 00fc69bb..0188c3f8 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: ml_linalg description: SIMD-based linear algebra and statistics, efficient manipulation with numeric data -version: 13.12.4 +version: 13.12.5 homepage: https://github.com/gyrdym/ml_linalg environment: From 86e032d13b5012038439eb141a05d060b7c06042 Mon Sep 17 00:00:00 2001 From: Ilya Gyrdymov Date: Fri, 7 Jun 2024 22:42:10 +0300 Subject: [PATCH 2/2] Speed up Vector.randomFilled --- lib/src/vector/float32x4_vector.dart | 1 - lib/src/vector/float64x2_vector.g.dart | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/src/vector/float32x4_vector.dart b/lib/src/vector/float32x4_vector.dart index 6649b76b..21489cfc 100644 --- a/lib/src/vector/float32x4_vector.dart +++ b/lib/src/vector/float32x4_vector.dart @@ -21,7 +21,6 @@ import 'package:ml_linalg/src/vector/serialization/vector_to_json.dart'; import 'package:ml_linalg/src/vector/vector_cache_keys.dart'; import 'package:ml_linalg/vector.dart'; -const _bytesPerElement = Float32List.bytesPerElement; const _bytesPerSimdElement = Float32x4List.bytesPerElement; const _bucketSize = Float32x4List.bytesPerElement ~/ Float32List.bytesPerElement; diff --git a/lib/src/vector/float64x2_vector.g.dart b/lib/src/vector/float64x2_vector.g.dart index 5b027cec..0a753208 100644 --- a/lib/src/vector/float64x2_vector.g.dart +++ b/lib/src/vector/float64x2_vector.g.dart @@ -24,7 +24,6 @@ import 'package:ml_linalg/src/vector/serialization/vector_to_json.dart'; import 'package:ml_linalg/src/vector/vector_cache_keys.dart'; import 'package:ml_linalg/vector.dart'; -const _bytesPerElement = Float64List.bytesPerElement; const _bytesPerSimdElement = Float64x2List.bytesPerElement; const _bucketSize = Float64x2List.bytesPerElement ~/ Float64List.bytesPerElement; @@ -57,6 +56,10 @@ class Float64x2Vector with IterableMixin implements Vector { 'Argument `min` should be less than `max`, min: $min, max: $max'); } + if (length < 0) { + throw ArgumentError('Length cannot be negative'); + } + _numOfBuckets = _getNumOfBuckets(length, _bucketSize); final list = Float64List(_numOfBuckets * _bucketSize);