Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
further 40% optimization to lookup that bails out early on fully-qual…
…ified types
  • Loading branch information
mkruskal-google committed May 9, 2025
commit 33f5fb6f79dfbdc2398d66940f51150acce34a11
14 changes: 11 additions & 3 deletions src/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -401,20 +401,28 @@
} else if (!path.length)
return this;

var flatPath = path.join(".");

// Start at root if path is absolute
if (path[0] === "")
return this.root.lookup(path.slice(1), filterTypes);
var flatPath = path.join(".");

Check warning on line 409 in src/namespace.js

View workflow job for this annotation

GitHub Actions / lint

Trailing spaces not allowed
// Early bailout for objects with matching absolute paths
var found = this.root._fullyQualifiedObjects["." + flatPath];
if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
return found;
}

var found = this._lookupImpl(path, flatPath);
// Do a regular lookup at this namespace and below
found = this._lookupImpl(path, flatPath);
if (found && (!filterTypes || filterTypes.indexOf(found.constructor) > -1)) {
return found;
}

// If there hasn't been a match, walk up the tree
if (parentAlreadyChecked)
return null;

// If there hasn't been a match, walk up the tree and look more broadly
var current = this;
while (current.parent) {
found = current.parent._lookupImpl(path, flatPath);
Expand Down
14 changes: 14 additions & 0 deletions src/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ function Root(options) {
* @private
*/
this._edition = "proto2";

/**
* Global lookup cache of fully qualified names.
* @type {Object.<string,ReflectionObject>}
* @private
*/
this._fullyQualifiedObjects = {};
}

/**
Expand Down Expand Up @@ -341,6 +348,11 @@ Root.prototype._handleAdd = function _handleAdd(object) {
object.parent[object.name] = object; // expose namespace as property of its parent
}

if (object instanceof Type || object instanceof Enum) {
// Only store types and enums for quick lookup during resolve.
this._fullyQualifiedObjects[object.fullName] = object;
}

// The above also adds uppercased (and thus conflict-free) nested types, services and enums as
// properties of namespaces just like static code does. This allows using a .d.ts generated for
// a static module with reflection-based solutions where the condition is met.
Expand Down Expand Up @@ -381,6 +393,8 @@ Root.prototype._handleRemove = function _handleRemove(object) {
delete object.parent[object.name]; // unexpose namespaces

}

delete this._fullyQualifiedObjects[object.fullName];
};

// Sets up cyclic dependencies (called in index-light)
Expand Down
Loading