gree / lwf

LWF - Lightweight SWF
http://gree.github.com/lwf/
zlib License
627 stars 167 forks source link

Textures cache is drained when cocos2d::LWFNode is released #124

Closed stari4ek closed 9 years ago

stari4ek commented 9 years ago

Code:

void LWFResourceCache::unloadLWFDataInternal(const shared_ptr<LWFData> &data)
{
    map<string, bool>::iterator
        it(data->resourceCache.begin()), itend(data->resourceCache.end());
    TextureCache *cache = Director::getInstance()->getTextureCache();
    for (; it != itend; ++it) {
        Texture2D *texture = cache->getTextureForKey(it->first.c_str());
        if (texture && texture->getReferenceCount() == 1)
            cache->removeTexture(texture);
    }
}

Every time we load LWF file which was loaded before - we will load (decode png, apply alpha processing) same images again. Some ideas:

I can imagine that in some rare situations, we'd prefer to have as much free memory a possible before new LWF is loaded, especially if it uses a lot of huge textures, but it's rare situation.

Some statistics (TextureCache::addImage) as example. We have "pause" screen as separated LWF. First and second screen opening With cache drain: 300ms/291ms Without: 302ms/2ms

Can we have it configurable at least?

splhack commented 9 years ago

Yes, definitely. You can change the behavior whatever you want because it is open source.

stari4ek commented 9 years ago

Sorry, no offense here.

Just wanted to hear thoughts of author about current approach and probably to get more flexible solution in upstream.

Thank You.

splhack commented 9 years ago

I don't understand the point. You wanted to keep textures on memory? why don't you keep LWF instance or LWF data on memory?

stari4ek commented 9 years ago

The idea of cache is to save result of expensive work and re-use it when it will be needed once again. This is not about owning objects. Values stored in any cache always live longer than it's users.

If You look through whole cocos2dx project you will see, that there is no any manual cache draining used, except FPS counter.

Keeping all LWF objects will lock textures in cache and if memory warning will happened - they can't be released and app won't survive. It's very different from approach, when textures are kept in cache for case when they will be needed once again, but if app is out of memory - they can be easily released.

From cocos2dx srcs:

    /** Purges the dictionary of loaded textures.
    * Call this method if you receive the "Memory Warning".
    * In the short term: it will free some resources preventing your app from being killed.
    * In the medium term: it will allocate more resources.
    * In the long term: it will be the same.
    */
    void removeAllTextures();

Very detailed explanation: http://stackoverflow.com/questions/15704367/cocos2d-2-0-when-is-good-practice-to-purge-caches

Thank You,

splhack commented 9 years ago

I know the behavior and still I don't understand your point at all. You can rewrite the code in order to get the behavior what you want.

stari4ek commented 9 years ago

Thank you. I will create PR, probably it will show the idea much better.

splhack commented 9 years ago

Thank you. But no need to create PR.