Skip to content

Commit

Permalink
Reduce the tile management overhead.
Browse files Browse the repository at this point in the history
Took some single digit percent frame time on my system.

Contributions:

 * Don't compute hash of hashes.
 * Use hash as key and avoid string interpolation.
 * Use a cheap hash designed for x,y,z

Also as a drive-by get rid of the TileCoordinates extends Point + zoom
coordinate in favor of more consistent composition.
  • Loading branch information
ignatz committed Oct 29, 2023
1 parent c24b6e6 commit 3ba2f8c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
11 changes: 5 additions & 6 deletions lib/src/layer/tile_layer/tile_bounds/tile_bounds_at_zoom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,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
22 changes: 16 additions & 6 deletions lib/src/layer/tile_layer/tile_coordinates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,44 @@ import 'dart:math';
import 'package:meta/meta.dart';

@immutable
class TileCoordinates extends Point<int> {
class TileCoordinates {
final int x;
final int y;
final int z;

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

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

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

// Overridden because Point's distanceTo does not allow comparing with a point
// of a different type.
@override
double distanceTo(Point<num> other) {
final dx = x - other.x;
final dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}

Point scaleBy(Point p) {
return Point(x * p.x, y * p.y);
}

@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
if (identical(this, other)) {
return true;
}
return other is TileCoordinates &&
other.x == x &&
other.y == y &&
other.z == z;
}

@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
10 changes: 8 additions & 2 deletions lib/src/layer/tile_layer/tile_range.dart
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,14 @@ class DiscreteTileRange extends TileRange {
);
}

bool contains(Point<int> point) {
return _bounds.contains(point);
bool contains(TileCoordinates point) {
if (zoom != point.z) {
return false;
}
return (point.x >= _bounds.min.x) &&
(point.x <= _bounds.max.x) &&
(point.y >= _bounds.min.y) &&
(point.y <= _bounds.max.y);
}

Point<int> get min => _bounds.min;
Expand Down

0 comments on commit 3ba2f8c

Please sign in to comment.