Skip to content

Commit 0db0152

Browse files
Cache feature resolution at the namespace level to avoid any O(n^2) scaling
1 parent db3c953 commit 0db0152

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

index.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,9 @@ export abstract class NamespaceBase extends ReflectionObject {
750750
/** Nested objects by name. */
751751
public nested?: { [k: string]: ReflectionObject };
752752

753+
/** Whether or not objects contained in this namespace need feature resolution. */
754+
protected _needsRecursiveFeatureResolution: boolean;
755+
753756
/** Nested objects of this namespace as an array for iteration. */
754757
public readonly nestedArray: ReflectionObject[];
755758

src/namespace.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,17 +117,23 @@ function Namespace(name, options) {
117117
* @private
118118
*/
119119
this._lookupCache = {};
120+
121+
/**
122+
* Whether or not objects contained in this namespace need feature resolution.
123+
* @type {boolean}
124+
* @protected
125+
*/
126+
this._needsRecursiveFeatureResolution = true;
120127
}
121128

122129
function clearCache(namespace) {
123130
namespace._nestedArray = null;
124131
namespace._lookupCache = {};
125132

126133
// Also clear parent caches, since they include nested lookups.
127-
var parent = namespace.parent;
128-
while(parent) {
134+
var parent = namespace;
135+
while(parent = parent.parent) {
129136
parent._lookupCache = {};
130-
parent = parent.parent;
131137
}
132138
return namespace;
133139
}
@@ -266,6 +272,14 @@ Namespace.prototype.add = function add(object) {
266272
}
267273
}
268274

275+
this._needsRecursiveFeatureResolution = true;
276+
277+
// Also clear parent caches, since they need to recurse down.
278+
var parent = this;
279+
while(parent = parent.parent) {
280+
parent._needsRecursiveFeatureResolution = true;
281+
}
282+
269283
object.onAdd(this);
270284
return clearCache(this);
271285
};
@@ -341,6 +355,9 @@ Namespace.prototype.resolveAll = function resolveAll() {
341355
* @override
342356
*/
343357
Namespace.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
358+
if (!this._needsRecursiveFeatureResolution) return this;
359+
this._needsRecursiveFeatureResolution = false;
360+
344361
edition = this._edition || edition;
345362

346363
ReflectionObject.prototype._resolveFeaturesRecursive.call(this, edition);

src/service.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ Service.prototype.resolveAll = function resolveAll() {
121121
* @override
122122
*/
123123
Service.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
124+
if (!this._needsRecursiveFeatureResolution) return this;
125+
124126
edition = this._edition || edition;
125127

126128
Namespace.prototype._resolveFeaturesRecursive.call(this, edition);

src/type.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,8 @@ Type.prototype.resolveAll = function resolveAll() {
317317
* @override
318318
*/
319319
Type.prototype._resolveFeaturesRecursive = function _resolveFeaturesRecursive(edition) {
320+
if (!this._needsRecursiveFeatureResolution) return this;
321+
320322
edition = this._edition || edition;
321323

322324
Namespace.prototype._resolveFeaturesRecursive.call(this, edition);

0 commit comments

Comments
 (0)