Skip to content

Commit

Permalink
perf!: optimize CRS performance and add micro-benchmark of limited ex…
Browse files Browse the repository at this point in the history
…pressiveness (#1727)
  • Loading branch information
ignatz authored Dec 1, 2023
1 parent 670cb36 commit c7b0de5
Show file tree
Hide file tree
Showing 10 changed files with 377 additions and 261 deletions.
107 changes: 107 additions & 0 deletions benchmark/crs.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import 'dart:async';
import 'dart:math' as math;

import 'package:flutter_map/src/geo/crs.dart';
import 'package:latlong2/latlong.dart';
import 'package:logger/logger.dart';

class NoFilter extends LogFilter {
@override
bool shouldLog(LogEvent event) => true;
}

typedef Result = ({
String name,
Duration duration,
});

Future<Result> timedRun(String name, dynamic Function() body) async {
Logger().i('running $name...');
final watch = Stopwatch()..start();
await body();
watch.stop();

return (name: name, duration: watch.elapsed);
}

// NOTE: to have a more prod like comparison, run with:
// $ dart compile exe benchmark/crs.dart && ./benchmark/crs.exe
//
// If you run in JIT mode, the resulting execution times will be a lot more similar.
Future<void> main() async {
Logger.level = Level.all;
Logger.defaultFilter = () => NoFilter();
Logger.defaultPrinter = () => SimplePrinter();

final results = <Result>[];
const N = 100000000;

const crs = Epsg3857();
results.add(await timedRun('Concrete type: ${crs.code}.latLngToXY()', () {
double x = 0;
double y = 0;
for (int i = 0; i < N; ++i) {
final latlng = LatLng((i % 90).toDouble(), (i % 180).toDouble());
final (cx, cy) = crs.latLngToXY(latlng, 1);
x += cx;
y += cy;
}
return x + y;
}));

results.add(await timedRun('Concrete type: ${crs.code}.latLngToPoint()', () {
double x = 0;
double y = 0;
for (int i = 0; i < N; ++i) {
final latlng = LatLng((i % 90).toDouble(), (i % 180).toDouble());
final p = crs.latLngToPoint(latlng, 1);
x += p.x;
y += p.y;
}
return x + y;
}));

const crss = <Crs>[
Epsg3857(),
Epsg4326(),
];

for (final crs in crss) {
results.add(await timedRun('${crs.code}.latLngToXY()', () {
double x = 0;
double y = 0;
for (int i = 0; i < N; ++i) {
final latlng = LatLng((i % 90).toDouble(), (i % 180).toDouble());
final (cx, cy) = crs.latLngToXY(latlng, 1);
x += cx;
y += cy;
}
return x + y;
}));

results.add(await timedRun('${crs.code}.latlngToPoint()', () {
double x = 0;
double y = 0;
for (int i = 0; i < N; ++i) {
final latlng = LatLng((i % 90).toDouble(), (i % 180).toDouble());
final point = crs.latLngToPoint(latlng, 1);
x += point.x;
y += point.y;
}
return x + y;
}));

results.add(await timedRun('${crs.code}.pointToLatLng()', () {
double x = 0;
double y = 0;
for (int i = 0; i < N; ++i) {
final latlng = crs.pointToLatLng(math.Point<double>(x, y), 1);
x += latlng.longitude;
y += latlng.latitude;
}
return x + y;
}));
}

Logger().i('Results:\n${results.map((r) => r.toString()).join('\n')}');
}
2 changes: 0 additions & 2 deletions example/lib/pages/custom_crs/custom_crs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,6 @@ class CustomCrsPageState extends State<CustomCrsPage> {
// Scale factors (pixels per projection unit, for example pixels/meter) for zoom levels;
// specify either scales or resolutions, not both
scales: null,
// The transformation to use when transforming projected coordinates into pixel coordinates
transformation: null,
);
}

Expand Down
1 change: 0 additions & 1 deletion example/lib/pages/epsg3413_crs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ class EPSG3413PageState extends State<EPSG3413Page> {
bounds: epsg3413Bounds,
origins: const [Point(0, 0)],
scales: null,
transformation: null,
);
}

Expand Down
Loading

0 comments on commit c7b0de5

Please sign in to comment.