From f627db0c9535dce0ae3d59c7eb3aed8a69fd3e32 Mon Sep 17 00:00:00 2001
From: Christian Wulf <chw@informatik.uni-kiel.de>
Date: Mon, 16 Jun 2014 16:32:18 +0200
Subject: [PATCH] array wrapper costs performance

---
 results/overhead-findings.txt                 |  9 ++++---
 .../throughput/methodcall/ArrayWrapper.java   | 27 +++++++++++++++++++
 .../throughput/methodcall/ConsumerStage.java  |  5 +++-
 .../throughput/methodcall/FixedSizedPipe.java | 16 ++++++++++-
 4 files changed, 52 insertions(+), 5 deletions(-)
 create mode 100644 src/test/java/teetime/examples/throughput/methodcall/ArrayWrapper.java

diff --git a/results/overhead-findings.txt b/results/overhead-findings.txt
index f649f1f5..ff309c6a 100644
--- a/results/overhead-findings.txt
+++ b/results/overhead-findings.txt
@@ -3,6 +3,7 @@
 -for loop with super type vs. concrete type (reason: due to less JIT optimization possibilities)
 -passing by argument vs. instance variable (reason: unknown)
 -pipe with array vs. single element (reason: unknown)
+-access via array wrapper vs. array directly
 -
 
 [irrelevant w.r.t. overhead]
@@ -16,8 +17,10 @@
 
 2:	7400 ns
 8:	1200 ns (iterative; argument/return w/o pipe)
-9:	9400 ns (queued pipe)
-10:	4900 ns (single element pipe)
-11: 7400 ns (fixed sized pipe)
+9:	9400 ns (executeWithPorts: queued pipe)
+10:	4900 ns (executeWithPorts: single element pipe)
+11: 7400 ns (executeWithPorts: fixed sized pipe)
+	11: 8600 ns (executeWithPorts: fixed sized pipe with CircularArray(int))
+	11: 8200 ns (executeWithPorts: fixed sized pipe with CircularArray(int) w/o mask)
 12: 3300 ns (recursive; argument/return w/o pipe)
 13: 3300 ns (recursive; argument/return w/o pipe; w/o pipeline class)
diff --git a/src/test/java/teetime/examples/throughput/methodcall/ArrayWrapper.java b/src/test/java/teetime/examples/throughput/methodcall/ArrayWrapper.java
new file mode 100644
index 00000000..7cb232b2
--- /dev/null
+++ b/src/test/java/teetime/examples/throughput/methodcall/ArrayWrapper.java
@@ -0,0 +1,27 @@
+package teetime.examples.throughput.methodcall;
+
+public final class ArrayWrapper<T> {
+
+	private final T[] elements;
+
+	// private int lastFreeIndex;
+
+	@SuppressWarnings("unchecked")
+	public ArrayWrapper(final int initialCapacity) {
+		super();
+		this.elements = (T[]) new Object[initialCapacity];
+	}
+
+	public final T get(final int index) {
+		return this.elements[index];
+	}
+
+	public final void put(final int index, final T element) {
+		this.elements[index] = element;
+	}
+
+	public final int getCapacity() {
+		return this.elements.length;
+	}
+
+}
diff --git a/src/test/java/teetime/examples/throughput/methodcall/ConsumerStage.java b/src/test/java/teetime/examples/throughput/methodcall/ConsumerStage.java
index ffd3c99e..75c3c69e 100644
--- a/src/test/java/teetime/examples/throughput/methodcall/ConsumerStage.java
+++ b/src/test/java/teetime/examples/throughput/methodcall/ConsumerStage.java
@@ -21,9 +21,12 @@ public abstract class ConsumerStage<I, O> extends AbstractStage<I, O> {
 	@Override
 	public void executeWithPorts() {
 		I element = this.getInputPort().receive();
+
+		this.setReschedulable(!this.getInputPort().pipe.isEmpty());
+
 		this.execute5(element);
 
-		// this.setReschedulable(!this.getOutputPort().pipe.isEmpty());
+		// this.send(result);
 
 		// if (!this.getOutputPort().pipe.isEmpty()) {
 		// super.executeWithPorts();
diff --git a/src/test/java/teetime/examples/throughput/methodcall/FixedSizedPipe.java b/src/test/java/teetime/examples/throughput/methodcall/FixedSizedPipe.java
index 119add4c..ded0991b 100644
--- a/src/test/java/teetime/examples/throughput/methodcall/FixedSizedPipe.java
+++ b/src/test/java/teetime/examples/throughput/methodcall/FixedSizedPipe.java
@@ -3,6 +3,7 @@ package teetime.examples.throughput.methodcall;
 public class FixedSizedPipe<T> implements IPipe<T> {
 
 	private final T[] elements = (T[]) new Object[4];
+	// private final ArrayWrapper<T> elements = new ArrayWrapper<T>(2);
 	private int lastFreeIndex;
 
 	public static <T> void connect(final OutputPort<T> sourcePort, final InputPort<T> targetPort) {
@@ -13,12 +14,24 @@ public class FixedSizedPipe<T> implements IPipe<T> {
 
 	@Override
 	public void add(final T element) {
+		if (this.lastFreeIndex == this.elements.length) {
+			// if (this.lastFreeIndex == this.elements.getCapacity()) {
+			try {
+				Thread.sleep(1000);
+			} catch (InterruptedException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+			// grow
+		}
 		this.elements[this.lastFreeIndex++] = element;
+		// this.elements.put(this.lastFreeIndex++, element);
 	}
 
 	@Override
 	public T removeLast() {
 		return this.elements[--this.lastFreeIndex];
+		// return this.elements.get(--this.lastFreeIndex);
 	}
 
 	@Override
@@ -28,7 +41,8 @@ public class FixedSizedPipe<T> implements IPipe<T> {
 
 	@Override
 	public T readLast() {
-		return this.elements[this.lastFreeIndex];
+		return this.elements[this.lastFreeIndex - 1];
+		// return this.elements.get(this.lastFreeIndex - 1);
 	}
 
 }
-- 
GitLab