Skip to content

Vector2.clone() performance hit for x86-64 and ARM64? #319

Closed
@JosefWN

Description

@JosefWN

Hello! I'm experiencing a pretty heavy performance hit with additions using Vector2 on ARM64 (M3) in Dart 3.3.3 on macOS. Haven't tested other arithmetics.

For comparison I have used math.Point and a custom PointDouble to avoid potential issues with generics and monomorphization (as detailed in dart-lang/sdk#53912):

class PointBenchmark extends BenchmarkBase {
  const PointBenchmark() : super('AddPoint');

  @override
  void run() {
    for (double i = -500; i <= 500; i += 0.75) {
      for (double j = -500; j <= 500; j += 0.75) {
        final _ = math.Point<double>(i, j) + math.Point<double>(j, i);
      }
    }
  }
}

class PointDoubleBenchmark extends BenchmarkBase {
  const PointDoubleBenchmark() : super('AddPointDouble');

  @override
  void run() {
    for (double i = -500; i <= 500; i += 0.75) {
      for (double j = -500; j <= 500; j += 0.75) {
        final _ = PointDouble(i, j) + PointDouble(j, i);
      }
    }
  }
}

class Vector2Benchmark extends BenchmarkBase {
  const Vector2Benchmark() : super('AddVector2');

  @override
  void run() {
    for (double i = -500; i <= 500; i += 0.75) {
      for (double j = -500; j <= 500; j += 0.75) {
        final _ = Vector2(i, j) + Vector2(j, i);
      }
    }
  }
}

Where PointDouble is:

class PointDouble {
  final double x;
  final double y;

  PointDouble operator +(final PointDouble other) =>
      PointDouble(x + other.x, y + other.y);

  const PointDouble(this.x, this.y);
}

AOT (dart compile exe):

AddPoint(RunTime): 9002.478813559323 us.
AddPointDouble(RunTime): 9050.669491525423 us.
AddVector2(RunTime): 145700.7142857143 us. <- about 16x slower!

JIT (dart run):

AddPoint(RunTime): 84623.54166666667 us. <- Monomorphization issues?
AddPointDouble(RunTime): 8925.813559322034 us.
AddVector2(RunTime): 8937.521186440677 us.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions