Skip to content

Commit

Permalink
Lower "expected degree" for internal maps, of which we have O(N) per …
Browse files Browse the repository at this point in the history
…graph/network. This greatly reduces memory consumption for graphs of small average degree, in exchange for a small performance hit for graphs of large average degree.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=125369630
  • Loading branch information
Bezier89 authored and cpovirk committed Jun 20, 2016
1 parent fe259bf commit b8ebf50
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
Expand Up @@ -18,6 +18,7 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.graph.GraphConstants.DEFAULT_NODE_COUNT;
import static com.google.common.graph.GraphErrorMessageUtils.NODE_NOT_IN_GRAPH;

import com.google.common.collect.Maps;
Expand Down Expand Up @@ -53,9 +54,6 @@
* @param <N> Node parameter type
*/
abstract class AbstractConfigurableGraph<N> extends AbstractGraph<N> {
// The default of 11 is rather arbitrary, but roughly matches the sizing of just new HashMap()
private static final int DEFAULT_MAP_SIZE = 11;

private final boolean isDirected;
private final boolean allowsSelfLoops;
private final ElementOrder<? super N> nodeOrder;
Expand All @@ -71,7 +69,7 @@ abstract class AbstractConfigurableGraph<N> extends AbstractGraph<N> {

private static <S> Map<S, NodeAdjacencies<S>> getNodeMapforBuilder(
GraphBuilder<? super S> builder) {
int expectedNodeSize = builder.expectedNodeCount.or(DEFAULT_MAP_SIZE);
int expectedNodeSize = builder.expectedNodeCount.or(DEFAULT_NODE_COUNT);
switch (builder.nodeOrder.type()) {
case UNORDERED:
return Maps.newHashMapWithExpectedSize(expectedNodeSize);
Expand Down
Expand Up @@ -18,6 +18,8 @@

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.graph.GraphConstants.DEFAULT_EDGE_COUNT;
import static com.google.common.graph.GraphConstants.DEFAULT_NODE_COUNT;
import static com.google.common.graph.GraphErrorMessageUtils.EDGE_NOT_IN_GRAPH;
import static com.google.common.graph.GraphErrorMessageUtils.NODE_NOT_IN_GRAPH;

Expand Down Expand Up @@ -59,9 +61,6 @@
* @param <E> Edge parameter type
*/
abstract class AbstractConfigurableNetwork<N, E> extends AbstractNetwork<N, E> {
// The default of 11 is rather arbitrary, but roughly matches the sizing of just new HashMap()
private static final int DEFAULT_MAP_SIZE = 11;

private final boolean isDirected;
private final boolean allowsParallelEdges;
private final boolean allowsSelfLoops;
Expand All @@ -86,7 +85,7 @@ abstract class AbstractConfigurableNetwork<N, E> extends AbstractNetwork<N, E> {

private static <S, T> Map<S, NodeConnections<S, T>> getNodeMapForBuilder(
NetworkBuilder<? super S, ? super T> builder) {
int expectedNodeSize = builder.expectedNodeCount.or(DEFAULT_MAP_SIZE);
int expectedNodeSize = builder.expectedNodeCount.or(DEFAULT_NODE_COUNT);
switch (builder.nodeOrder.type()) {
case UNORDERED:
return Maps.newHashMapWithExpectedSize(expectedNodeSize);
Expand All @@ -101,7 +100,7 @@ private static <S, T> Map<S, NodeConnections<S, T>> getNodeMapForBuilder(

private static <S, T> Map<T, S> getEdgeMapForBuilder(
NetworkBuilder<? super S, ? super T> builder) {
int expectedEdgeSize = builder.expectedEdgeCount.or(DEFAULT_MAP_SIZE);
int expectedEdgeSize = builder.expectedEdgeCount.or(DEFAULT_EDGE_COUNT);
switch (builder.edgeOrder.type()) {
case UNORDERED:
return Maps.newHashMapWithExpectedSize(expectedEdgeSize);
Expand Down
Expand Up @@ -59,8 +59,10 @@ private DirectedNodeAdjacencies(
}

static <N> DirectedNodeAdjacencies<N> of() {
// We store predecessors and successors in the same map, so expected size is twice the degree.
int inAndOutDegree = EXPECTED_DEGREE * 2;
return new DirectedNodeAdjacencies<N>(
Maps.<N, Adjacency>newHashMapWithExpectedSize(EXPECTED_DEGREE), 0, 0);
Maps.<N, Adjacency>newHashMapWithExpectedSize(inAndOutDegree), 0, 0);
}

static <N> DirectedNodeAdjacencies<N> ofImmutable(
Expand Down
9 changes: 6 additions & 3 deletions guava/src/com/google/common/graph/GraphConstants.java
Expand Up @@ -19,11 +19,14 @@
/**
* A utility class to hold various constants used by the Guava Graph library.
*/
// TODO(user): Decide what else to put here (error message strings, node/edge map sizes, etc.)
// TODO(user): Decide what else to put here (error message strings, etc.)
final class GraphConstants {

private GraphConstants() {}

// TODO(user): Enable users to specify the expected (in/out?) degree of nodes.
static final int EXPECTED_DEGREE = 11;
// TODO(user): Enable users to specify the expected degree of nodes.
static final int EXPECTED_DEGREE = 2;

static final int DEFAULT_NODE_COUNT = 10;
static final int DEFAULT_EDGE_COUNT = DEFAULT_NODE_COUNT * EXPECTED_DEGREE;
}

0 comments on commit b8ebf50

Please sign in to comment.