Skip to content

Commit

Permalink
Only allocate a child-map for SimpleTopLevelIndex.Nodes that represen…
Browse files Browse the repository at this point in the history
…t packages.

This should eliminate over half of all child-maps.

PiperOrigin-RevId: 689975386
  • Loading branch information
nreid260 authored and Javac Team committed Oct 26, 2024
1 parent 0c6eb49 commit db74c1f
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions java/com/google/turbine/binder/lookup/SimpleTopLevelIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

package com.google.turbine.binder.lookup;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.turbine.binder.sym.ClassSymbol;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.jspecify.annotations.Nullable;

Expand All @@ -37,17 +38,20 @@ public class SimpleTopLevelIndex implements TopLevelIndex {
public static class Node {

public @Nullable Node lookup(String bit) {
return children.get(bit);
return (children == null) ? null : children.get(bit);
}

private final @Nullable ClassSymbol sym;

// TODO(cushon): the set of children is typically going to be small, consider optimizing this
// to use a denser representation where appropriate.
private final Map<String, Node> children = new HashMap<>();
private final @Nullable HashMap<String, Node> children;

Node(@Nullable ClassSymbol sym) {
this.sym = sym;
if (sym == null) {
this.sym = null;
this.children = new HashMap<>();
} else {
this.sym = sym;
this.children = null;
}
}

/**
Expand All @@ -57,6 +61,7 @@ public static class Node {
* @return {@code null} if an existing symbol with the same name has already been inserted.
*/
private @Nullable Node insert(String name, @Nullable ClassSymbol sym) {
checkNotNull(children, "Cannot insert child into a class node '%s'", this.sym);
Node child = children.get(name);
if (child != null) {
if (child.sym != null) {
Expand Down Expand Up @@ -105,7 +110,6 @@ public void insert(ClassSymbol sym) {
if (curr == null || !Objects.equals(curr.sym, sym)) {
return;
}
return;
}
}

Expand Down Expand Up @@ -191,6 +195,10 @@ public PackageIndex(Node node) {
new Supplier<ImmutableList<ClassSymbol>>() {
@Override
public ImmutableList<ClassSymbol> get() {
if (node.children == null) {
return ImmutableList.of();
}

ImmutableList.Builder<ClassSymbol> result = ImmutableList.builder();
for (Node child : node.children.values()) {
if (child.sym != null) {
Expand Down

0 comments on commit db74c1f

Please sign in to comment.