Clean out updateMemoryCost

This commit is contained in:
Zach Pomerantz 2016-04-27 18:37:30 -07:00
parent 1e8d45aecb
commit 854c0ea3e3
3 changed files with 24 additions and 10 deletions

View file

@ -128,10 +128,13 @@ void ScriptableResource::release() {
_resource.reset();
}
void ScriptableResource::updateMemoryCost(const QObject* engine) {
if (_resource && !_resource->isInScript()) {
_resource->setInScript(true);
connect(_resource.data(), SIGNAL(updateSize(qint64)), engine, SLOT(updateMemoryCost(qint64)));
bool ScriptableResource::isInScript() const {
return _resource && _resource->isInScript();
}
void ScriptableResource::setInScript(bool isInScript) {
if (_resource) {
_resource->setInScript(isInScript);
}
}

View file

@ -104,9 +104,10 @@ public:
const QUrl& getUrl() const { return _url; }
int getState() const { return (int)_state; }
const QSharedPointer<Resource>& getResource() const { return _resource; }
// Connects to a SLOT(updateMemoryCost(qint64)) on the given engine
void updateMemoryCost(const QObject* engine);
bool isInScript() const;
void setInScript(bool isInScript);
signals:
void progressChanged(uint64_t bytesReceived, uint64_t bytesTotal);
@ -357,6 +358,9 @@ private slots:
void handleReplyFinished();
private:
friend class ResourceCache;
friend class ScriptableResource;
void setLRUKey(int lruKey) { _lruKey = lruKey; }
void makeRequest();
@ -366,9 +370,6 @@ private:
bool isInScript() const { return _isInScript; }
void setInScript(bool isInScript) { _isInScript = isInScript; }
friend class ResourceCache;
friend class ScriptableResource;
ResourceRequest* _request{ nullptr };
int _lruKey{ 0 };
QTimer* _replyTimer{ nullptr };

View file

@ -274,7 +274,17 @@ static void resultHandlerFromScriptValue(const QScriptValue& value, AnimVariantR
using ScriptableResourceRawPtr = ScriptableResource*;
static QScriptValue scriptableResourceToScriptValue(QScriptEngine* engine, const ScriptableResourceRawPtr& resource) {
resource->updateMemoryCost(engine);
// The first script to encounter this resource will track its memory.
// In this way, it will be more likely to GC.
// This fails in the case that the resource is used across many scripts, but
// in that case it would be too difficult to tell which one should track the memory, and
// this serves the common case (use in a single script).
auto data = resource->getResource();
if (data && !resource->isInScript()) {
resource->setInScript(true);
QObject::connect(data.data(), SIGNAL(updateSize(qint64)), engine, SLOT(updateMemoryCost(qint64)));
}
auto object = engine->newQObject(
const_cast<ScriptableResourceRawPtr>(resource),
QScriptEngine::ScriptOwnership);