daegalus / dart-uuid

Generate RFC9562(v1,v4,v5,v6,v7,v8) UUIDs
MIT License
357 stars 77 forks source link

Performance regression with version 4.5 #131

Open xvrh opened 1 week ago

xvrh commented 1 week ago

With the new version of this package, all our tests were slowed down by a big factor.

Here is a benchmark comparing the new and old version

import 'package:uuid/uuid.dart';
void main() {
  var watch = Stopwatch()..start();

  for (var i = 0; i < 1000000; i++) {
    var uuid = const Uuid().v4();
  }

  print("Elapsed ${watch.elapsed}");
}

On macOS: With version 4.4.2: Elapsed 0:00:00.376794 With version 4.5.1: Elapsed 0:01:02.359221

On linux it is sligtly better With version 4.4.2: Elapsed 0:00:00.400237 With version 4.5.1: Elapsed 0:00:11.670186

Maybe the OS is waiting to collect some entropy with the new rng algorithm?

daegalus commented 1 week ago

yes, because I switched it to CryptoRNG by default, it now probably waits longer for entropy, or has to do a bit more work to make it crypto strong.

If you with to revert to the old speed and behavior, you can use Uuid(goptions: GlobalOptions(MathRNG()) to set it globally. Or use v4(options: V4Options(rng: MathRNG())) to do it at the function level.

daegalus commented 1 week ago

You are also recreating the Uuid object every time. you might get performance benefits if you create it once, and then run the v4() function of the created object in the loop.

var uuid := const Uuid();
for (...) {
 var uuidv4 = const uuid.v4();
}

The way you have it might be burning through entropy faster, because each Uuid creation, reseeds from scratch, instead of just getting more seed when necessary.

xvrh commented 1 week ago

Thanks for the response.

I don't think your idea of putting the const Uuid() out of the loop have any impact. Since it's a const object, it doesn't change the performance.

Anyway, I'll use the GlobalOptions for now.