Reverted Dave's changes to image cache in 2008-12-05T03:33:26Z!davewx7@gmail.com...
...since they were causing serious slowdowns e.g. during animations.
This commit is contained in:
parent
edf7be1b40
commit
1ad4dbd100
2 changed files with 19 additions and 36 deletions
|
@ -67,8 +67,8 @@ namespace image {
|
|||
|
||||
std::list<int> dummy_list;
|
||||
|
||||
template<typename T, typename SizeFunctor>
|
||||
void cache_type<T, SizeFunctor>::flush()
|
||||
template<typename T>
|
||||
void cache_type<T>::flush()
|
||||
{
|
||||
typename std::vector<cache_item<T> >::iterator beg = content_.begin();
|
||||
typename std::vector<cache_item<T> >::iterator end = content_.end();
|
||||
|
@ -780,9 +780,6 @@ surface get_image(const image::locator& i_locator, TYPE type)
|
|||
res = create_optimized_surface(res);
|
||||
i_locator.add_to_cache(*imap, res);
|
||||
|
||||
DBG_CACHE << "IMAGES: " << (images_.size()/1024) << " HEXED: " << (images_.size()/1024) << " SCALED_TO_HEX: " << (scaled_to_hex_images_.size()/1024) << " SCALED_TO_ZOOM: " << (scaled_to_zoom_.size()/1024) << " UNMASKED: " << (unmasked_images_.size()/1024) << " BRIGHTENED: " << (brightened_images_.size()/1024) << " SEMI: " << (semi_brightened_images_.size()/1024)
|
||||
<< " TOTAL: " << (images_.size() + hexed_images_.size() + scaled_to_hex_images_.size() + scaled_to_zoom_.size() + unmasked_images_.size() + brightened_images_.size() + semi_brightened_images_.size())/(1024*1024) << "\n";
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -868,8 +865,8 @@ void precache_file_existence(const std::string& subdir)
|
|||
}
|
||||
|
||||
|
||||
template<typename T, typename SizeFunctor>
|
||||
cache_item<T>& cache_type<T, SizeFunctor>::get_element(int index){
|
||||
template<typename T>
|
||||
cache_item<T>& cache_type<T>::get_element(int index){
|
||||
assert (index != -1);
|
||||
while(static_cast<size_t>(index) >= content_.size()) {
|
||||
content_.push_back(cache_item<T>());
|
||||
|
@ -883,22 +880,19 @@ cache_item<T>& cache_type<T, SizeFunctor>::get_element(int index){
|
|||
}
|
||||
return elt;
|
||||
}
|
||||
template<typename T, typename SizeFunctor>
|
||||
void cache_type<T, SizeFunctor>::on_load(int index){
|
||||
template<typename T>
|
||||
void cache_type<T>::on_load(int index){
|
||||
if(index == -1) return ;
|
||||
cache_item<T>& elt = content_[index];
|
||||
if(!elt.loaded) return ;
|
||||
lru_list_.push_front(index);
|
||||
DBG_CACHE << "cache size: " << size_functor_(elt.item) << "\n";
|
||||
DBG_CACHE << "cache size max: " << cache_size_ << "/" << cache_max_size_ << "\n";
|
||||
cache_size_ += size_functor_(elt.item);
|
||||
elt.position = lru_list_.begin();
|
||||
while(cache_size_ > cache_max_size_-100) {
|
||||
cache_item<T>& elt = content_[lru_list_.back()];
|
||||
cache_size_ -= size_functor_(elt.item);
|
||||
elt.loaded=false;
|
||||
elt.item = T();
|
||||
lru_list_.pop_back();
|
||||
cache_size_--;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -72,22 +72,13 @@ namespace image {
|
|||
std::list<int>::iterator position;
|
||||
};
|
||||
|
||||
struct SizeSurface {
|
||||
int operator()(const surface& s) const { return 256 + (s ? s->w*s->h*4 : 0); }
|
||||
};
|
||||
|
||||
struct SizeOne {
|
||||
template<typename T>
|
||||
int operator()(const T&) const { return 1; }
|
||||
};
|
||||
|
||||
template<typename T, typename SizeFunctor>
|
||||
template<typename T>
|
||||
class cache_type
|
||||
{
|
||||
public:
|
||||
cache_type() :
|
||||
cache_size_(0),
|
||||
cache_max_size_(10000000),
|
||||
cache_max_size_(2000),
|
||||
lru_list_(),
|
||||
content_()
|
||||
{
|
||||
|
@ -96,13 +87,11 @@ namespace image {
|
|||
cache_item<T>& get_element(int index);
|
||||
void on_load(int index);
|
||||
void flush();
|
||||
size_t size() const { return cache_size_; }
|
||||
private:
|
||||
size_t cache_size_;
|
||||
size_t cache_max_size_;
|
||||
int cache_size_;
|
||||
int cache_max_size_;
|
||||
std::list<int> lru_list_;
|
||||
std::vector<cache_item<T> > content_;
|
||||
SizeFunctor size_functor_;
|
||||
};
|
||||
|
||||
//a generic image locator. Abstracts the location of an image.
|
||||
|
@ -167,18 +156,18 @@ namespace image {
|
|||
// loads the image it is pointing to from the disk
|
||||
surface load_from_disk() const;
|
||||
|
||||
bool in_cache(cache_type<surface,SizeSurface>& cache) const
|
||||
bool in_cache(cache_type<surface>& cache) const
|
||||
{ return index_ == -1 ? false : cache.get_element(index_).loaded; }
|
||||
surface locate_in_cache(cache_type<surface,SizeSurface>& cache) const
|
||||
surface locate_in_cache(cache_type<surface>& cache) const
|
||||
{ return index_ == -1 ? surface() : cache.get_element(index_).item; }
|
||||
void add_to_cache(cache_type<surface,SizeSurface>& cache, const surface &image) const
|
||||
void add_to_cache(cache_type<surface>& cache, const surface &image) const
|
||||
{ if(index_ != -1 ) cache.get_element(index_) = cache_item<surface>(image); cache.on_load(index_); }
|
||||
|
||||
bool in_cache(cache_type<locator,SizeOne>& cache) const
|
||||
bool in_cache(cache_type<locator>& cache) const
|
||||
{ return index_ == -1 ? false : cache.get_element(index_).loaded; cache.on_load(index_); }
|
||||
locator locate_in_cache(cache_type<locator,SizeOne>& cache) const
|
||||
locator locate_in_cache(cache_type<locator>& cache) const
|
||||
{ return index_ == -1 ? locator() : cache.get_element(index_).item; }
|
||||
void add_to_cache(cache_type<locator,SizeOne>& cache, const locator &image) const
|
||||
void add_to_cache(cache_type<locator>& cache, const locator &image) const
|
||||
{ if(index_ != -1) cache.get_element(index_) = cache_item<locator>(image); }
|
||||
protected:
|
||||
static int last_index_;
|
||||
|
@ -192,8 +181,8 @@ namespace image {
|
|||
};
|
||||
|
||||
|
||||
typedef cache_type<surface,SizeSurface> image_cache;
|
||||
typedef cache_type<locator,SizeOne> locator_cache;
|
||||
typedef cache_type<surface> image_cache;
|
||||
typedef cache_type<locator> locator_cache;
|
||||
typedef std::map<t_translation::t_terrain, surface> mini_terrain_cache_map;
|
||||
extern mini_terrain_cache_map mini_terrain_cache;
|
||||
extern mini_terrain_cache_map mini_fogged_terrain_cache;
|
||||
|
|
Loading…
Add table
Reference in a new issue