Skip to content

Commit

Permalink
Additional performance tweaks in edge map implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Aug 5, 2014
1 parent 4deb0d0 commit 5f70f34
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*
* @author Sam Harwell
*/
public class ArrayEdgeMap<T> extends AbstractEdgeMap<T> {
public final class ArrayEdgeMap<T> extends AbstractEdgeMap<T> {

private final AtomicReferenceArray<T> arrayData;
private final AtomicInteger size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
*
* @author Sam Harwell
*/
public class SingletonEdgeMap<T> extends AbstractEdgeMap<T> {
public final class SingletonEdgeMap<T> extends AbstractEdgeMap<T> {

private final int key;
private final T value;
Expand Down
44 changes: 19 additions & 25 deletions runtime/Java/src/org/antlr/v4/runtime/dfa/SparseEdgeMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
*
* @author Sam Harwell
*/
public class SparseEdgeMap<T> extends AbstractEdgeMap<T> {
public final class SparseEdgeMap<T> extends AbstractEdgeMap<T> {
private static final int DEFAULT_MAX_SIZE = 5;

private final int[] keys;
Expand All @@ -59,26 +59,26 @@ public SparseEdgeMap(int minIndex, int maxIndex, int maxSparseSize) {

private SparseEdgeMap(@NotNull SparseEdgeMap<T> map, int maxSparseSize) {
super(map.minIndex, map.maxIndex);
if (maxSparseSize < map.values.size()) {
throw new IllegalArgumentException();
}

synchronized (map) {
if (maxSparseSize < map.values.size()) {
throw new IllegalArgumentException();
}

keys = Arrays.copyOf(map.keys, maxSparseSize);
values = new ArrayList<T>(maxSparseSize);
values.addAll(map.values);
}
}

public int[] getKeys() {
public final int[] getKeys() {
return keys;
}

public List<T> getValues() {
public final List<T> getValues() {
return values;
}

public int getMaxSparseSize() {
public final int getMaxSparseSize() {
return keys.length;
}

Expand All @@ -99,14 +99,15 @@ public boolean containsKey(int key) {

@Override
public T get(int key) {
synchronized (this) {
int index = Arrays.binarySearch(keys, 0, size(), key);
if (index < 0) {
return null;
}

return values.get(index);
// Special property of this collection: values are only even added to
// the end, else a new object is returned from put(). Therefore no lock
// is required in this method.
int index = Arrays.binarySearch(keys, 0, size(), key);
if (index < 0) {
return null;
}

return values.get(index);
}

@Override
Expand Down Expand Up @@ -147,7 +148,7 @@ public AbstractEdgeMap<T> put(int key, T value) {
}
else {
SparseEdgeMap<T> resized = new SparseEdgeMap<T>(this, desiredSize);
System.arraycopy(resized.keys, insertIndex, resized.keys, insertIndex + 1, resized.keys.length - insertIndex - 1);
System.arraycopy(resized.keys, insertIndex, resized.keys, insertIndex + 1, size() - insertIndex);
resized.keys[insertIndex] = key;
resized.values.add(insertIndex, value);
return resized;
Expand All @@ -163,11 +164,6 @@ public SparseEdgeMap<T> remove(int key) {
return this;
}

if (index == values.size() - 1) {
values.remove(index);
return this;
}

SparseEdgeMap<T> result = new SparseEdgeMap<T>(this, getMaxSparseSize());
System.arraycopy(result.keys, index + 1, result.keys, index, size() - index - 1);
result.values.remove(index);
Expand All @@ -176,14 +172,12 @@ public SparseEdgeMap<T> remove(int key) {
}

@Override
public SparseEdgeMap<T> clear() {
public AbstractEdgeMap<T> clear() {
if (isEmpty()) {
return this;
}

SparseEdgeMap<T> result = new SparseEdgeMap<T>(this, getMaxSparseSize());
result.values.clear();
return result;
return new EmptyEdgeMap<T>(minIndex, maxIndex);
}

@Override
Expand Down

0 comments on commit 5f70f34

Please sign in to comment.