Skip to content

Commit df40d8c

Browse files
authored
perf: avoid caching a single multiplication (#1743)
1 parent ad8318b commit df40d8c

File tree

6 files changed

+19
-31
lines changed

6 files changed

+19
-31
lines changed

example/lib/plugins/scalebar_utils.dart

+5-14
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,7 @@ import 'dart:math';
22
import 'dart:ui';
33

44
import 'package:latlong2/latlong.dart';
5-
6-
const double piOver180 = pi / 180.0;
7-
8-
double toDegrees(double radians) {
9-
return radians / piOver180;
10-
}
11-
12-
double toRadians(double degrees) {
13-
return degrees * piOver180;
14-
}
5+
import 'package:vector_math/vector_math_64.dart';
156

167
LatLng calculateEndingGlobalCoordinates(
178
LatLng start, double startBearing, double distance) {
@@ -25,8 +16,8 @@ LatLng calculateEndingGlobalCoordinates(
2516
const aSquared = a * a;
2617
const bSquared = b * b;
2718
const f = mFlattening;
28-
final phi1 = toRadians(start.latitude);
29-
final alpha1 = toRadians(startBearing);
19+
final phi1 = degrees2Radians * start.latitude;
20+
final alpha1 = degrees2Radians * startBearing;
3021
final cosAlpha1 = cos(alpha1);
3122
final sinAlpha1 = sin(alpha1);
3223
final s = distance;
@@ -138,7 +129,7 @@ LatLng calculateEndingGlobalCoordinates(
138129

139130
// build result
140131
return LatLng(
141-
clampDouble(toDegrees(phi2), -90, 90),
142-
clampDouble(start.longitude + toDegrees(L), -180, 180),
132+
clampDouble(radians2Degrees * phi2, -90, 90),
133+
clampDouble(start.longitude + (L * radians2Degrees), -180, 180),
143134
);
144135
}

example/pubspec.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ dependencies:
1818
shared_preferences: ^2.2.1
1919
url_strategy: ^0.2.0
2020
http: ^1.1.0
21+
vector_math: ^2.1.2
2122

2223
dependency_overrides:
2324
flutter_map_cancellable_tile_provider:

lib/src/geo/latlng_bounds.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'dart:math' as math;
22

33
import 'package:latlong2/latlong.dart';
4+
import 'package:vector_math/vector_math_64.dart';
45

56
/// Data structure representing rectangular bounding box constrained by its
67
/// northwest and southeast corners
@@ -98,8 +99,9 @@ class LatLngBounds {
9899
final lambda1 = southWest.longitudeInRad;
99100
final phi2 = northEast.latitudeInRad;
100101

101-
final dLambda = degToRadian(northEast.longitude -
102-
southWest.longitude); // delta lambda = lambda2-lambda1
102+
final dLambda = degrees2Radians *
103+
(northEast.longitude -
104+
southWest.longitude); // delta lambda = lambda2-lambda1
103105

104106
final bx = math.cos(phi2) * math.cos(dLambda);
105107
final by = math.cos(phi2) * math.sin(dLambda);

lib/src/gestures/flutter_map_interactive_viewer.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'package:flutter_map/src/map/options/interaction.dart';
1616
import 'package:flutter_map/src/map/options/options.dart';
1717
import 'package:flutter_map/src/misc/point_extensions.dart';
1818
import 'package:latlong2/latlong.dart';
19+
import 'package:vector_math/vector_math_64.dart';
1920

2021
typedef InteractiveViewerBuilder = Widget Function(
2122
BuildContext context,
@@ -660,7 +661,7 @@ class FlutterMapInteractiveViewerState
660661
final rotationCenter =
661662
_camera.project(_camera.offsetToCrs(_lastFocalLocal));
662663
final vector = oldCenterPt - rotationCenter;
663-
final rotatedVector = vector.rotate(degToRadian(rotationDiff));
664+
final rotatedVector = vector.rotate(degrees2Radians * rotationDiff);
664665
final newCenter = rotationCenter + rotatedVector;
665666

666667
widget.controller.moveAndRotate(

lib/src/map/camera/camera.dart

+5-13
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import 'package:flutter_map/src/map/options/options.dart';
99
import 'package:flutter_map/src/misc/bounds.dart';
1010
import 'package:flutter_map/src/misc/point_extensions.dart';
1111
import 'package:latlong2/latlong.dart';
12+
import 'package:vector_math/vector_math_64.dart';
1213

1314
/// Describes the view of a map. This includes the size/zoom/position/crs as
1415
/// well as the minimum/maximum zoom. This class is mostly immutable but has
@@ -53,9 +54,6 @@ class MapCamera {
5354
/// Lazily calculated field
5455
Point<double>? _pixelOrigin;
5556

56-
/// Lazily calculated field
57-
double? _rotationRad;
58-
5957
/// This is the [LatLngBounds] corresponding to four corners of this camera.
6058
/// This takes rotation in to account.
6159
LatLngBounds get visibleBounds => _bounds ??= LatLngBounds(
@@ -105,12 +103,10 @@ class MapCamera {
105103
Bounds<double>? pixelBounds,
106104
LatLngBounds? bounds,
107105
Point<double>? pixelOrigin,
108-
double? rotationRad,
109106
}) : _cameraSize = size,
110107
_pixelBounds = pixelBounds,
111108
_bounds = bounds,
112-
_pixelOrigin = pixelOrigin,
113-
_rotationRad = rotationRad;
109+
_pixelOrigin = pixelOrigin;
114110

115111
/// Initializes [MapCamera] from the given [options] and with the
116112
/// [nonRotatedSize] set to [kImpossibleSize].
@@ -135,7 +131,6 @@ class MapCamera {
135131
nonRotatedSize: nonRotatedSize,
136132
minZoom: minZoom,
137133
maxZoom: maxZoom,
138-
rotationRad: _rotationRad,
139134
);
140135
}
141136

@@ -171,7 +166,6 @@ class MapCamera {
171166
rotation: rotation,
172167
nonRotatedSize: nonRotatedSize,
173168
size: _cameraSize,
174-
rotationRad: _rotationRad,
175169
);
176170
}
177171

@@ -189,7 +183,6 @@ class MapCamera {
189183
rotation: rotation,
190184
nonRotatedSize: nonRotatedSize,
191185
size: _cameraSize,
192-
rotationRad: _rotationRad,
193186
);
194187

195188
/// Calculates the size of a bounding box which surrounds a box of size
@@ -200,7 +193,7 @@ class MapCamera {
200193
) {
201194
if (rotation == 0.0) return nonRotatedSize;
202195

203-
final rotationRad = degToRadian(rotation);
196+
final rotationRad = degrees2Radians * rotation;
204197
final cosAngle = math.cos(rotationRad).abs();
205198
final sinAngle = math.sin(rotationRad).abs();
206199
final width = (nonRotatedSize.x * cosAngle) + (nonRotatedSize.y * sinAngle);
@@ -210,9 +203,8 @@ class MapCamera {
210203
return Point<double>(width, height);
211204
}
212205

213-
/// The current rotation value in radians. This is calculated and cached when
214-
/// it is first called.
215-
double get rotationRad => _rotationRad ??= degToRadian(rotation);
206+
/// The current rotation value in radians
207+
double get rotationRad => rotation * degrees2Radians;
216208

217209
/// Calculates point value for the given [latLng] using this camera's
218210
/// [crs] and [zoom] (or the provided [zoom]).

lib/src/map/controller/internal.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import 'package:flutter_map/src/misc/move_and_rotate_result.dart';
1313
import 'package:flutter_map/src/misc/point_extensions.dart';
1414
import 'package:flutter_map/src/misc/position.dart';
1515
import 'package:latlong2/latlong.dart';
16+
import 'package:vector_math/vector_math_64.dart';
1617

1718
/// This controller is for internal use. All updates to the state should be done
1819
/// by calling methods of this class to ensure consistency.
@@ -190,7 +191,7 @@ class FlutterMapInternalController extends ValueNotifier<_InternalState> {
190191
camera.unproject(
191192
rotationCenter +
192193
(camera.project(camera.center) - rotationCenter)
193-
.rotate(degToRadian(rotationDiff)),
194+
.rotate(degrees2Radians * rotationDiff),
194195
),
195196
camera.zoom,
196197
offset: Offset.zero,

0 commit comments

Comments
 (0)