Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector hash speed up #179

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 13.12.6
- `Vector.hashCode`:
- Make hash code more reliable

## 13.12.5
- `Vector.randomFilled`:
- factory-constructor speed-up
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Approx. 0.23 second (MacBook Pro 2019), Dart version: 2.16.0
// Approx. 0.23 second (MacBook Pro 2019), Dart version: 3.2.4

import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:ml_linalg/vector.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Approx. 0.62 second (MacBook Pro 2019), Dart version: 2.16.0
// Approx. 0.62 second (MacBook Pro 2019), Dart version: 3.2.4
// Approx. 3.7 second (MacBook Air mid 2017), Dart VM version: 2.16.0

import 'package:benchmark_harness/benchmark_harness.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Approx. 1.9 seconds (MacBook Pro 2019), Dart version: 2.16.0
// creation + hash code calculation: approx. 2.9 seconds (MacBook Air mid 2017) Dart 2.16.0
// Approx. 2.5 seconds (MacBook Pro 2019), Dart version: 3.2.4

import 'package:benchmark_harness/benchmark_harness.dart';
import 'package:ml_linalg/dtype.dart';
Expand All @@ -11,21 +10,24 @@ class Float32x4VectorHashCodeBenchmark extends BenchmarkBase {
Float32x4VectorHashCodeBenchmark()
: super('Vector `hashCode`; $amountOfElements elements');

Vector? vector;
List<num> source = Vector.randomFilled(
amountOfElements,
seed: 1,
min: -1000,
max: 1000,
dtype: DType.float32,
).toList();

static void main() {
Float32x4VectorHashCodeBenchmark().report();
}

@override
void run() {
Vector.randomFilled(
amountOfElements,
seed: 1,
min: -1000,
max: 1000,
Vector.fromList(
source,
dtype: DType.float32,
).hashCode;
);
}
}

Expand Down
25 changes: 16 additions & 9 deletions lib/src/vector/float32x4_vector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ class Float32x4Vector with IterableMixin<double> implements Vector {
final SimdHelper<Float32x4> _simdHelper;
late ByteBuffer _buffer;
late int _numOfBuckets;
int _hash = 0;

@override
Iterator<double> get iterator => _getTypedList().iterator;
Expand Down Expand Up @@ -154,18 +155,24 @@ class Float32x4Vector with IterableMixin<double> implements Vector {
}

@override
int get hashCode => _cache.get(vectorHashKey, () {
if (isEmpty) {
return 0;
}
int get hashCode {
if (isEmpty) {
return 0;
}

if (_hash != 0) {
return _hash;
}

var i = 0;
_hash = 1;
final typesList = _getTypedList();

final summed = _getSimdList()
.reduce((sum, element) => sum + element.scale((31 * (i++)) * 1.0));
for (var i = 0; i < length; i++) {
_hash = _hash * 31 + typesList[i].hashCode;
}

return length + _simdHelper.sumLanesForHash(summed).hashCode;
}, skipCaching: false);
return _hash;
}

@override
Vector operator +(Object value) {
Expand Down
25 changes: 16 additions & 9 deletions lib/src/vector/float64x2_vector.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class Float64x2Vector with IterableMixin<double> implements Vector {
final SimdHelper<Float64x2> _simdHelper;
late ByteBuffer _buffer;
late int _numOfBuckets;
int _hash = 0;

@override
Iterator<double> get iterator => _getTypedList().iterator;
Expand Down Expand Up @@ -157,18 +158,24 @@ class Float64x2Vector with IterableMixin<double> implements Vector {
}

@override
int get hashCode => _cache.get(vectorHashKey, () {
if (isEmpty) {
return 0;
}
int get hashCode {
if (isEmpty) {
return 0;
}

if (_hash != 0) {
return _hash;
}

var i = 0;
_hash = 1;
final typesList = _getTypedList();

final summed = _getSimdList()
.reduce((sum, element) => sum + element.scale((31 * (i++)) * 1.0));
for (var i = 0; i < length; i++) {
_hash = _hash * 31 + typesList[i].hashCode;
}

return length + _simdHelper.sumLanesForHash(summed).hashCode;
}, skipCaching: false);
return _hash;
}

@override
Vector operator +(Object value) {
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: ml_linalg
description: SIMD-based linear algebra and statistics, efficient manipulation with numeric data
version: 13.12.5
version: 13.12.6
homepage: https://github.com/gyrdym/ml_linalg

environment:
Expand Down
Loading