FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Avoid redundant ElementType allocation and lookup in Depset#getSet. · codemod/bazel-java@722c25b · GitHub

Repository navigation

Commit 722c25b

Browse files
authored andcommitted
Avoid redundant ElementType allocation and lookup in Depset#getSet.
`Depset#getSet(Class<T>)` is called frequently during rule analysis to unpack typed nested sets. It previously allocated a new `ElementType` wrapper and resolved reflection metadata on every invocation, even though `elemClass` is already validated and canonicalized during construction. Fast-pathing `elemClass == type` and deferring `getElementType()` allocation to the error path eliminates heap allocations and reflection overhead on valid casts. PiperOrigin-RevId: 985969732 Change-Id: I0780a447182d7ec713af5dd2095cedd2fef12ca7
1 parent 9f1272b commit 722c25b

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

  • src/main/java/com/google/devtools/build/lib/collect/nestedset

‎src/main/java/com/google/devtools/build/lib/collect/nestedset/Depset.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,11 @@ private static Class<?> checkType(@Nullable Class<?> existingElemType, Class<?>
236236
* @throws TypeException if the type does not accurately describe all elements
237237
*/
238238
public <T> NestedSet<T> getSet(Class<T> type) throws TypeException {
239-
ElementType elemType = getElementType();
240-
if (!set.isEmpty() && !elemType.canBeCastTo(type)) {
239+
if (!set.isEmpty() && !ElementType.canBeCastTo(elemClass, type)) {
241240
throw new TypeException(
242241
String.format(
243242
"got a depset of '%s', expected a depset of '%s'",
244-
elemType, Starlark.classType(type)));
243+
getElementType(), Starlark.classType(type)));
245244
}
246245
@SuppressWarnings("unchecked")
247246
NestedSet<T> res = (NestedSet<T>) set;
@@ -539,10 +538,11 @@ private static Class<?> getTypeClass(Class<?> cls) {
539538
// getTypeClass calls here and in ElementType.of, and remove the special
540539
// case for Object.class since isAssignableFrom will allow any supertype
541540
// of the element type, whether or not it is a Starlark value class.
542-
private boolean canBeCastTo(Class<?> cls) {
543-
return this.cls == null
541+
private static boolean canBeCastTo(@Nullable Class<?> elemClass, Class<?> cls) {
542+
return elemClass == null
543+
|| elemClass == cls
544544
|| cls == Object.class // historical exception
545-
|| getTypeClass(cls).isAssignableFrom(this.cls);
545+
|| getTypeClass(cls).isAssignableFrom(elemClass);
546546
}
547547

548548
@Override

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL