Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
cache parsed tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
ansis committed Apr 13, 2015
1 parent e572bf8 commit b00f230
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 3 deletions.
3 changes: 3 additions & 0 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ class Map : private util::noncopyable {
std::vector<uint32_t> getAnnotationsInBounds(const LatLngBounds&);
LatLngBounds getBoundsForAnnotations(const std::vector<uint32_t>&);

// Reduce memory usage
void onLowMemory();

// Debug
void setDebug(bool value);
void toggleDebug();
Expand Down
6 changes: 6 additions & 0 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ - (BOOL)commonInit
//
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillForeground:) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveMemoryWarning) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];

// set initial position
//
Expand Down Expand Up @@ -2544,4 +2545,9 @@ - (void)setAllowsRotating:(BOOL)allowsRotating
self.rotateEnabled = allowsRotating;
}

- (void)didReceiveMemoryWarning
{
mbglMap->onLowMemory();
}

@end
10 changes: 10 additions & 0 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -818,3 +818,13 @@ void Map::render() {
triggerUpdate();
}
}

void Map::onLowMemory() {
invokeTask([=] {
if (!style) return;
for (const auto &source : style->sources) {
source->onLowMemory();
}
env->performCleanup();
});
};
22 changes: 19 additions & 3 deletions src/mbgl/map/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ TileData::State Source::addTile(Map &map, Worker &worker,
new_tile.data.reset();
}

if (!new_tile.data) {
new_tile.data = cache.get(id.to_uint64());
}

if (!new_tile.data) {
// If we don't find working tile data, we're just going to load it.
if (info.type == SourceType::Vector) {
Expand Down Expand Up @@ -394,28 +398,35 @@ void Source::update(Map &map,
}
}

auto& tileCache = cache;

// Remove tiles that we definitely don't need, i.e. tiles that are not on
// the required list.
std::set<TileID> retain_data;
util::erase_if(tiles, [&retain, &retain_data](std::pair<const TileID, std::unique_ptr<Tile>> &pair) {
util::erase_if(tiles, [&retain, &retain_data, &tileCache](std::pair<const TileID, std::unique_ptr<Tile>> &pair) {
Tile &tile = *pair.second;
bool obsolete = std::find(retain.begin(), retain.end(), tile.id) == retain.end();
if (!obsolete) {
retain_data.insert(tile.data->id);
} else if (tile.data->ready()) {
tileCache.add(tile.id.to_uint64(), tile.data);
}
return obsolete;
});


// Remove all the expired pointers from the set.
util::erase_if(tile_data, [&retain_data](std::pair<const TileID, std::weak_ptr<TileData>> &pair) {
util::erase_if(tile_data, [&retain_data, &tileCache](std::pair<const TileID, std::weak_ptr<TileData>> &pair) {
const util::ptr<TileData> tile = pair.second.lock();
if (!tile) {
return true;
}

bool obsolete = retain_data.find(tile->id) == retain_data.end();
if (obsolete) {
tile->cancel();
if (!tileCache.has(tile->id.to_uint64())) {
tile->cancel();
}
return true;
} else {
return false;
Expand All @@ -426,10 +437,15 @@ void Source::update(Map &map,
}

void Source::invalidateTiles(const std::vector<TileID>& ids) {
cache.clear();
for (auto& id : ids) {
tiles.erase(id);
tile_data.erase(id);
}
}

void Source::onLowMemory() {
cache.clear();
}

}
4 changes: 4 additions & 0 deletions src/mbgl/map/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <mbgl/map/tile_id.hpp>
#include <mbgl/map/tile_data.hpp>
#include <mbgl/map/tile_cache.hpp>
#include <mbgl/style/types.hpp>

#include <mbgl/util/noncopyable.hpp>
Expand Down Expand Up @@ -72,6 +73,8 @@ class Source : public std::enable_shared_from_this<Source>, private util::noncop

std::forward_list<Tile *> getLoadedTiles() const;

void onLowMemory();

SourceInfo info;
bool enabled;

Expand All @@ -96,6 +99,7 @@ class Source : public std::enable_shared_from_this<Source>, private util::noncop

std::map<TileID, std::unique_ptr<Tile>> tiles;
std::map<TileID, std::weak_ptr<TileData>> tile_data;
TileCache cache;
};

}
Expand Down
45 changes: 45 additions & 0 deletions src/mbgl/map/tile_cache.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <mbgl/map/tile_cache.hpp>

#include <cassert>

namespace mbgl{

void TileCache::add(uint64_t key, std::shared_ptr<TileData> data) {

assert(tiles.find(key) == tiles.end());

tiles.emplace(key, data);
orderedKeys.push_back(key);

if (orderedKeys.size() > size) {
get(orderedKeys.front());
}

assert(orderedKeys.size() <= size);
};

std::shared_ptr<TileData> TileCache::get(uint64_t key) {

std::shared_ptr<TileData> data;

auto it = tiles.find(key);
if (it != tiles.end()) {
data = it->second;
tiles.erase(it);
orderedKeys.remove(key);
assert(data->ready());
}

return data;
};

bool TileCache::has(uint64_t key) {
return tiles.find(key) != tiles.end();
}

void TileCache::clear() {
orderedKeys.clear();
tiles.clear();
}

};
29 changes: 29 additions & 0 deletions src/mbgl/map/tile_cache.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef MBGL_MAP_TILE_CACHE
#define MBGL_MAP_TILE_CACHE

#include <mbgl/map/tile_id.hpp>
#include <mbgl/map/tile_data.hpp>

#include <list>
#include <unordered_map>

namespace mbgl{


class TileCache {
public:
TileCache() : size(20) {}

void add(uint64_t key, std::shared_ptr<TileData> data);
std::shared_ptr<TileData> get(uint64_t key);
bool has(uint64_t key);
void clear();
private:
std::unordered_map<uint64_t, std::shared_ptr<TileData>> tiles;
std::list<uint64_t> orderedKeys;

const unsigned int size;
};

};
#endif

0 comments on commit b00f230

Please sign in to comment.