Skip to content

Commit a2e0dac

Browse files
add caching to prevent excessive recursion on resolveAll
1 parent 0270fb9 commit a2e0dac

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

src/namespace.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ function Namespace(name, options) {
124124
* @protected
125125
*/
126126
this._needsRecursiveFeatureResolution = true;
127+
128+
/**
129+
* Whether or not objects contained in this namespace need a resolve.
130+
* @type {boolean}
131+
* @protected
132+
*/
133+
this._needsRecursiveResolve = true;
127134
}
128135

129136
function clearCache(namespace) {
@@ -273,11 +280,13 @@ Namespace.prototype.add = function add(object) {
273280
}
274281

275282
this._needsRecursiveFeatureResolution = true;
283+
this._needsRecursiveResolve = true;
276284

277285
// Also clear parent caches, since they need to recurse down.
278286
var parent = this;
279287
while(parent = parent.parent) {
280288
parent._needsRecursiveFeatureResolution = true;
289+
parent._needsRecursiveResolve = true;
281290
}
282291

283292
object.onAdd(this);
@@ -341,13 +350,16 @@ Namespace.prototype.define = function define(path, json) {
341350
* @returns {Namespace} `this`
342351
*/
343352
Namespace.prototype.resolveAll = function resolveAll() {
353+
if (!this._needsRecursiveResolve) return this;
354+
344355
var nested = this.nestedArray, i = 0;
345356
this.resolve();
346357
while (i < nested.length)
347358
if (nested[i] instanceof Namespace)
348359
nested[i++].resolveAll();
349360
else
350361
nested[i++].resolve();
362+
this._needsRecursiveResolve = false;
351363
return this;
352364
};
353365

src/root.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,8 @@ Root.prototype.loadSync = function loadSync(filename, options) {
268268
* @override
269269
*/
270270
Root.prototype.resolveAll = function resolveAll() {
271+
if (!this._needsRecursiveResolve) return this;
272+
271273
if (this.deferred.length)
272274
throw Error("unresolvable extensions: " + this.deferred.map(function(field) {
273275
return "'extend " + field.extend + "' in " + field.parent.fullName;

src/service.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ Service.prototype.get = function get(name) {
110110
* @override
111111
*/
112112
Service.prototype.resolveAll = function resolveAll() {
113+
if (!this._needsRecursiveResolve) return this;
114+
113115
Namespace.prototype.resolve.call(this);
114116
var methods = this.methodsArray;
115117
for (var i = 0; i < methods.length; ++i)

src/type.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ Type.prototype.toJSON = function toJSON(toJSONOptions) {
303303
* @override
304304
*/
305305
Type.prototype.resolveAll = function resolveAll() {
306+
if (!this._needsRecursiveResolve) return this;
307+
306308
Namespace.prototype.resolveAll.call(this);
307309
var oneofs = this.oneofsArray; i = 0;
308310
while (i < oneofs.length)

0 commit comments

Comments
 (0)