Skip to content
Snippets Groups Projects
Commit 4104b2ed authored by Christian Wulf's avatar Christian Wulf
Browse files

improved performance

parent 3d5dba55
No related branches found
No related tags found
No related merge requests found
...@@ -32,6 +32,7 @@ public class CircularArray<T> { ...@@ -32,6 +32,7 @@ public class CircularArray<T> {
private final long logSize; private final long logSize;
private final T[] segment; private final T[] segment;
private final long mask;
/** /**
* *
...@@ -42,6 +43,7 @@ public class CircularArray<T> { ...@@ -42,6 +43,7 @@ public class CircularArray<T> {
public CircularArray(final long logSize) { public CircularArray(final long logSize) {
this.logSize = logSize; this.logSize = logSize;
this.segment = (T[]) new Object[1 << this.logSize]; this.segment = (T[]) new Object[1 << this.logSize];
this.mask = this.getCapacity() - 1; // mask = 0..01..1
} }
public long getCapacity() { public long getCapacity() {
...@@ -49,11 +51,11 @@ public class CircularArray<T> { ...@@ -49,11 +51,11 @@ public class CircularArray<T> {
} }
public T get(final long i) { public T get(final long i) {
return this.segment[(int) (i % this.getCapacity())]; // risk of overflow return this.segment[(int) (i & this.mask)]; // risk of overflow
} }
public void put(final long i, final T o) { public void put(final long i, final T o) {
this.segment[(int) (i % this.getCapacity())] = o; // risk of overflow this.segment[(int) (i & this.mask)] = o; // risk of overflow
} }
public CircularArray<T> grow(final long b, final long t) { public CircularArray<T> grow(final long b, final long t) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment