Skip to content

Commit

Permalink
Merge pull request #10350 from ebryn/lazy-caches
Browse files Browse the repository at this point in the history
Make meta.cache & meta.cacheMeta lazy
  • Loading branch information
ebryn committed Feb 4, 2015
2 parents b2f6ba6 + 39404ae commit 4966110
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/ember-metal/lib/chains.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function addChainWatcher(obj, keyName, node) {
var m = metaFor(obj);
var nodes = m.chainWatchers;

if (!m.hasOwnProperty('chainWatchers')) {
if (!m.hasOwnProperty('chainWatchers')) { // FIXME?!
nodes = m.chainWatchers = {};
}

Expand Down Expand Up @@ -122,7 +122,7 @@ function lazyGet(obj, key) {
var possibleDesc = obj[key];
var desc = (possibleDesc !== null && typeof possibleDesc === 'object' && possibleDesc.isDescriptor) ? possibleDesc : undefined;
if (desc && desc._cacheable) {
if (key in meta.cache) {
if (meta.cache && key in meta.cache) {
return meta.cache[key];
} else {
return undefined;
Expand Down
23 changes: 16 additions & 7 deletions packages/ember-metal/lib/computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ ComputedPropertyPrototype.didChange = function(obj, keyName) {
// the cached value set by the setter
if (this._cacheable && this._suspended !== obj) {
var meta = metaFor(obj);
if (meta.cache[keyName] !== undefined) {
if (meta.cache && meta.cache[keyName] !== undefined) {
meta.cache[keyName] = undefined;
removeDependentKeys(this, obj, keyName, meta);
}
Expand Down Expand Up @@ -348,7 +348,7 @@ ComputedPropertyPrototype.get = function(obj, keyName) {
meta = metaFor(obj);
cache = meta.cache;

var result = cache[keyName];
var result = cache && cache[keyName];

if (result === UNDEFINED) {
return undefined;
Expand All @@ -357,6 +357,10 @@ ComputedPropertyPrototype.get = function(obj, keyName) {
}

ret = this._getter.call(obj, keyName);
cache = meta.cache;
if (!cache) {
cache = meta.cache = {};
}
if (ret === undefined) {
cache[keyName] = UNDEFINED;
} else {
Expand Down Expand Up @@ -447,7 +451,7 @@ ComputedPropertyPrototype._set = function computedPropertySet(obj, keyName, valu
throw new EmberError('Cannot set read-only property "' + keyName + '" on object: ' + inspect(obj));
}

if (cacheable && cache[keyName] !== undefined) {
if (cacheable && cache && cache[keyName] !== undefined) {
if (cache[keyName] !== UNDEFINED) {
cachedValue = cache[keyName];
}
Expand Down Expand Up @@ -482,6 +486,9 @@ ComputedPropertyPrototype._set = function computedPropertySet(obj, keyName, valu
if (!hadCachedValue) {
addDependentKeys(this, obj, keyName, meta);
}
if (!cache) {
cache = meta.cache = {};
}
if (ret === undefined) {
cache[keyName] = UNDEFINED;
} else {
Expand All @@ -500,11 +507,13 @@ ComputedPropertyPrototype._set = function computedPropertySet(obj, keyName, valu
ComputedPropertyPrototype.teardown = function(obj, keyName) {
var meta = metaFor(obj);

if (keyName in meta.cache) {
removeDependentKeys(this, obj, keyName, meta);
}
if (meta.cache) {
if (keyName in meta.cache) {
removeDependentKeys(this, obj, keyName, meta);
}

if (this._cacheable) { delete meta.cache[keyName]; }
if (this._cacheable) { delete meta.cache[keyName]; }
}

return null; // no value to restore
};
Expand Down
8 changes: 4 additions & 4 deletions packages/ember-metal/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,8 @@ export function guidFor(obj) {
//
function Meta(obj) {
this.watching = {};
this.cache = {};
this.cacheMeta = {};
this.cache = undefined;
this.cacheMeta = undefined;
this.source = obj;
this.deps = undefined;
this.listeners = undefined;
Expand Down Expand Up @@ -377,8 +377,8 @@ function meta(obj, writable) {

ret = o_create(ret);
ret.watching = o_create(ret.watching);
ret.cache = {};
ret.cacheMeta = {};
ret.cache = undefined;
ret.cacheMeta = undefined;
ret.source = obj;

if (Ember.FEATURES.isEnabled('mandatory-setter')) {
Expand Down
17 changes: 13 additions & 4 deletions packages/ember-runtime/lib/computed/reduce_computed.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,10 @@ function partiallyRecomputeFor(obj, dependentKey) {
function ReduceComputedPropertyInstanceMeta(context, propertyName, initialValue) {
this.context = context;
this.propertyName = propertyName;
this.cache = metaFor(context).cache;
var contextMeta = metaFor(context);
var contextCache = contextMeta.cache;
if (!contextCache) { contextCache = contextMeta.cache = {}; }
this.cache = contextCache;
this.dependentArrays = {};
this.sugarMeta = {};
this.initialValue = initialValue;
Expand Down Expand Up @@ -574,13 +577,19 @@ ReduceComputedProperty.prototype._callbacks = function () {
};

ReduceComputedProperty.prototype._hasInstanceMeta = function (context, propertyName) {
return !!metaFor(context).cacheMeta[propertyName];
var contextMeta = context.__ember_meta__;
var cacheMeta = contextMeta && contextMeta.cacheMeta;
return !!(cacheMeta && cacheMeta[propertyName]);
};

ReduceComputedProperty.prototype._instanceMeta = function (context, propertyName) {
var cacheMeta = metaFor(context).cacheMeta;
var meta = cacheMeta[propertyName];
var contextMeta = context.__ember_meta__;
var cacheMeta = contextMeta.cacheMeta;
var meta = cacheMeta && cacheMeta[propertyName];

if (!cacheMeta) {
cacheMeta = contextMeta.cacheMeta = {};
}
if (!meta) {
meta = cacheMeta[propertyName] = new ReduceComputedPropertyInstanceMeta(context, propertyName, this.initialValue());
meta.dependentArraysObserver = new DependentArraysObserver(this._callbacks(), this, meta, context, propertyName, meta.sugarMeta);
Expand Down
2 changes: 1 addition & 1 deletion packages/ember-runtime/lib/system/core_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@ CoreObject.reopen({
if (value instanceof Ember.ComputedProperty) {
var cache = Ember.meta(this.constructor).cache;

if (cache._computedProperties !== undefined) {
if (cache && cache._computedProperties !== undefined) {
cache._computedProperties = undefined;
}
}
Expand Down

0 comments on commit 4966110

Please sign in to comment.