This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1281 from mapbox/cache-parsed-tiles-memory
- Loading branch information
Showing
9 changed files
with
191 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#include <mbgl/map/tile_cache.hpp> | ||
|
||
#include <cassert> | ||
|
||
namespace mbgl { | ||
|
||
void TileCache::setSize(size_t size_) { | ||
size = size_; | ||
|
||
while (orderedKeys.size() > size) { | ||
auto key = orderedKeys.front(); | ||
orderedKeys.pop_front(); | ||
tiles.erase(key); | ||
} | ||
|
||
assert(orderedKeys.size() <= size); | ||
|
||
tiles.reserve(size); | ||
orderedKeys.resize(size); | ||
} | ||
|
||
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(); | ||
} | ||
|
||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#ifndef MBGL_MAP_TILE_CACHE | ||
#define MBGL_MAP_TILE_CACHE | ||
|
||
#include <mbgl/map/tile_data.hpp> | ||
|
||
#include <list> | ||
#include <unordered_map> | ||
|
||
namespace mbgl { | ||
|
||
class TileCache { | ||
public: | ||
TileCache(size_t size_ = 0) : size(size_) {} | ||
|
||
void setSize(size_t); | ||
size_t getSize() const { return size; }; | ||
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; | ||
|
||
size_t size; | ||
}; | ||
|
||
}; | ||
|
||
#endif |