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

perf!: optimize CRS performance and add micro-benchmark of limited expressiveness #1727

Merged
merged 2 commits into from
Dec 1, 2023
Merged
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
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
Loading