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 30, 2023
1 parent c24b6e6 commit e8c0388
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 29 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
12 changes: 7 additions & 5 deletions lib/src/layer/tile_layer/tile_coordinates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,11 @@ 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)';

// 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;
Expand All @@ -24,13 +21,18 @@ class TileCoordinates extends Point<int> {

@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
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 e8c0388

Please sign in to comment.