Skip to content

Commit

Permalink
perf: reduce tile management overhead (#1709)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignatz authored Oct 30, 2023
1 parent 69a25e9 commit b941fd7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 29 deletions.
13 changes: 5 additions & 8 deletions lib/src/layer/tile_layer/tile_bounds/tile_bounds_at_zoom.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:math';

import 'package:flutter_map/src/layer/tile_layer/tile_coordinates.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_range.dart';
import 'package:meta/meta.dart';
Expand Down Expand Up @@ -112,12 +110,11 @@ class WrappedTileBoundsAtZoom extends TileBoundsAtZoom {
}

bool _wrappedBothContains(TileCoordinates coordinates) {
return tileRange.contains(
Point(
_wrapInt(coordinates.x, wrapX!),
_wrapInt(coordinates.y, wrapY!),
),
);
return tileRange.contains(TileCoordinates(
_wrapInt(coordinates.x, wrapX!),
_wrapInt(coordinates.y, wrapY!),
coordinates.z,
));
}

bool _wrappedXInRange(TileCoordinates coordinates) {
Expand Down
7 changes: 4 additions & 3 deletions lib/src/layer/tile_layer/tile_coordinates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class TileCoordinates extends Point<int> {

const TileCoordinates(super.x, super.y, this.z);

String get key => '$x:$y:$z';

@override
String toString() => 'TileCoordinate($x, $y, $z)';

Expand All @@ -32,5 +30,8 @@ class TileCoordinates extends Point<int> {
}

@override
int get hashCode => Object.hash(x.hashCode, y.hashCode, z.hashCode);
int get hashCode {
// NOTE: the odd numbers are due to JavaScript's integer precision of 53 bits.
return x | y << 24 | z << 48;
}
}
2 changes: 1 addition & 1 deletion lib/src/layer/tile_layer/tile_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class TileImage extends ChangeNotifier {

AnimationController? get animation => _animationController;

String get coordinatesKey => coordinates.key;
TileCoordinates get coordinatesKey => coordinates;

/// Whether the tile is displayable. This means that either:
/// * Loading errored but an error image is configured.
Expand Down
14 changes: 7 additions & 7 deletions lib/src/layer/tile_layer/tile_image_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ typedef TileCreator = TileImage Function(TileCoordinates coordinates);

@immutable
class TileImageManager {
final Map<String, TileImage> _tiles = {};
final Map<TileCoordinates, TileImage> _tiles = {};

bool containsTileAt(TileCoordinates coordinates) =>
_tiles.containsKey(coordinates.key);
_tiles.containsKey(coordinates);

bool get allLoaded =>
_tiles.values.none((tile) => tile.loadFinishedAt == null);
Expand Down Expand Up @@ -44,7 +44,7 @@ class TileImageManager {
}) {
for (final coordinates in tileBoundsAtZoom.validCoordinatesIn(tileRange)) {
_tiles.putIfAbsent(
coordinates.key,
coordinates,
() => createTileImage(coordinates),
);
}
Expand All @@ -64,7 +64,7 @@ class TileImageManager {

for (final coordinates in tileCoordinates) {
final tile = _tiles.putIfAbsent(
coordinates.key,
coordinates,
() => createTile(coordinates),
);

Expand All @@ -83,7 +83,7 @@ class TileImageManager {
/// All removals should be performed by calling this method to ensure that
// disposal is performed correctly.
void _remove(
String key, {
TileCoordinates key, {
required bool Function(TileImage tileImage) evictImageFromCache,
}) {
final removed = _tiles.remove(key);
Expand All @@ -94,7 +94,7 @@ class TileImageManager {
}

void _removeWithEvictionStrategy(
String key,
TileCoordinates key,
EvictErrorTileStrategy strategy,
) {
_remove(
Expand All @@ -105,7 +105,7 @@ class TileImageManager {
}

void removeAll(EvictErrorTileStrategy evictStrategy) {
final keysToRemove = List<String>.from(_tiles.keys);
final keysToRemove = List<TileCoordinates>.from(_tiles.keys);

for (final key in keysToRemove) {
_removeWithEvictionStrategy(key, evictStrategy);
Expand Down
8 changes: 4 additions & 4 deletions lib/src/layer/tile_layer/tile_image_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import 'package:flutter_map/src/layer/tile_layer/tile_image.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_range.dart';

class TileImageView {
final Map<String, TileImage> _tileImages;
final Map<TileCoordinates, TileImage> _tileImages;
final DiscreteTileRange _visibleRange;
final DiscreteTileRange _keepRange;

TileImageView({
required Map<String, TileImage> tileImages,
required Map<TileCoordinates, TileImage> tileImages,
required DiscreteTileRange visibleRange,
required DiscreteTileRange keepRange,
}) : _tileImages = UnmodifiableMapView(tileImages),
Expand Down Expand Up @@ -62,7 +62,7 @@ class TileImageView {
final z2 = z - 1;
final coords2 = TileCoordinates(x2, y2, z2);

final tile = _tileImages[coords2.key];
final tile = _tileImages[coords2];
if (tile != null) {
if (tile.readyToDisplay) {
retain.add(tile);
Expand Down Expand Up @@ -92,7 +92,7 @@ class TileImageView {
for (var j = 2 * y; j < 2 * y + 2; j++) {
final coords = TileCoordinates(i, j, z + 1);

final tile = _tileImages[coords.key];
final tile = _tileImages[coords];
if (tile != null) {
if (tile.readyToDisplay || tile.loadFinishedAt != null) {
retain.add(tile);
Expand Down
12 changes: 6 additions & 6 deletions test/layer/tile_layer/tile_image_view_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import 'package:test/test.dart';
import '../../test_utils/test_tile_image.dart';

void main() {
Map<String, TileImage> tileImagesMappingFrom(List<TileImage> tileImages) => {
for (final tileImage in tileImages) tileImage.coordinates.key: tileImage
};
Map<TileCoordinates, TileImage> tileImagesMappingFrom(
List<TileImage> tileImages) =>
{for (final tileImage in tileImages) tileImage.coordinates: tileImage};

Matcher containsTileImage(
Map<String, TileImage> tileImages,
Map<TileCoordinates, TileImage> tileImages,
TileCoordinates coordinates,
) =>
contains(tileImages[coordinates.key]);
contains(tileImages[coordinates]);

Matcher doesNotContainTileImage(
Map<String, TileImage> tileImages,
Map<TileCoordinates, TileImage> tileImages,
TileCoordinates coordinates,
) =>
isNot(containsTileImage(tileImages, coordinates));
Expand Down

0 comments on commit b941fd7

Please sign in to comment.