From efb749936b92beffc6166fb1ebbf5dd28a9b899c Mon Sep 17 00:00:00 2001 From: Simon Willnauer Date: Fri, 11 Apr 2014 22:46:44 +0200 Subject: [PATCH] [TEST] Improve performance of MockBigArray MockPageRecycler --- .../test/cache/recycler/MockBigArrays.java | 20 +++-------- .../cache/recycler/MockPageCacheRecycler.java | 35 +++++++++++-------- 2 files changed, 25 insertions(+), 30 deletions(-) diff --git a/src/test/java/org/elasticsearch/test/cache/recycler/MockBigArrays.java b/src/test/java/org/elasticsearch/test/cache/recycler/MockBigArrays.java index 873990492da..435d1127eb4 100644 --- a/src/test/java/org/elasticsearch/test/cache/recycler/MockBigArrays.java +++ b/src/test/java/org/elasticsearch/test/cache/recycler/MockBigArrays.java @@ -294,9 +294,7 @@ public class MockBigArrays extends BigArrays { @Override protected void randomizeContent(long from, long to) { - for (long i = from; i < to; ++i) { - set(i, (byte) random.nextInt(1 << 8)); - } + fill(from, to, (byte) random.nextInt(1 << 8)); } @Override @@ -342,9 +340,7 @@ public class MockBigArrays extends BigArrays { @Override protected void randomizeContent(long from, long to) { - for (long i = from; i < to; ++i) { - set(i, random.nextInt()); - } + fill(from, to, random.nextInt()); } @Override @@ -385,9 +381,7 @@ public class MockBigArrays extends BigArrays { @Override protected void randomizeContent(long from, long to) { - for (long i = from; i < to; ++i) { - set(i, random.nextLong()); - } + fill(from, to, random.nextLong()); } @Override @@ -428,9 +422,7 @@ public class MockBigArrays extends BigArrays { @Override protected void randomizeContent(long from, long to) { - for (long i = from; i < to; ++i) { - set(i, (random.nextFloat() - 0.5f) * 1000); - } + fill(from, to, (random.nextFloat() - 0.5f) * 1000); } @Override @@ -471,9 +463,7 @@ public class MockBigArrays extends BigArrays { @Override protected void randomizeContent(long from, long to) { - for (long i = from; i < to; ++i) { - set(i, (random.nextDouble() - 0.5) * 1000); - } + fill(from, to, (random.nextDouble() - 0.5) * 1000); } @Override diff --git a/src/test/java/org/elasticsearch/test/cache/recycler/MockPageCacheRecycler.java b/src/test/java/org/elasticsearch/test/cache/recycler/MockPageCacheRecycler.java index 25145bcb4c9..e01774aaf1b 100644 --- a/src/test/java/org/elasticsearch/test/cache/recycler/MockPageCacheRecycler.java +++ b/src/test/java/org/elasticsearch/test/cache/recycler/MockPageCacheRecycler.java @@ -32,6 +32,7 @@ import org.elasticsearch.test.TestCluster; import org.elasticsearch.threadpool.ThreadPool; import java.lang.reflect.Array; +import java.util.Arrays; import java.util.Map; import java.util.Random; import java.util.concurrent.ConcurrentMap; @@ -85,11 +86,21 @@ public class MockPageCacheRecycler extends PageCacheRecycler { throw new IllegalStateException("Releasing a page that has not been acquired"); } final T ref = v(); - for (int i = 0; i < Array.getLength(ref); ++i) { - if (ref instanceof Object[]) { - Array.set(ref, i, null); - } else { - Array.set(ref, i, (byte) random.nextInt(256)); + if (ref instanceof Object[]) { + Arrays.fill((Object[])ref, 0, Array.getLength(ref), null); + } else if (ref instanceof byte[]) { + Arrays.fill((byte[])ref, 0, Array.getLength(ref), (byte) random.nextInt(256)); + } else if (ref instanceof long[]) { + Arrays.fill((long[])ref, 0, Array.getLength(ref), random.nextLong()); + } else if (ref instanceof int[]) { + Arrays.fill((int[])ref, 0, Array.getLength(ref), random.nextInt()); + } else if (ref instanceof double[]) { + Arrays.fill((double[])ref, 0, Array.getLength(ref), random.nextDouble() - 0.5); + } else if (ref instanceof float[]) { + Arrays.fill((float[])ref, 0, Array.getLength(ref), random.nextFloat() - 0.5f); + } else { + for (int i = 0; i < Array.getLength(ref); ++i) { + Array.set(ref, i, (byte) random.nextInt(256)); } } return v.release(); @@ -112,7 +123,7 @@ public class MockPageCacheRecycler extends PageCacheRecycler { public V bytePage(boolean clear) { final V page = super.bytePage(clear); if (!clear) { - random.nextBytes(page.v()); + Arrays.fill(page.v(), 0, page.v().length, (byte)random.nextInt(1<<8)); } return wrap(page); } @@ -121,9 +132,7 @@ public class MockPageCacheRecycler extends PageCacheRecycler { public V intPage(boolean clear) { final V page = super.intPage(clear); if (!clear) { - for (int i = 0; i < page.v().length; ++i) { - page.v()[i] = random.nextInt(); - } + Arrays.fill(page.v(), 0, page.v().length, random.nextInt()); } return wrap(page); } @@ -132,9 +141,7 @@ public class MockPageCacheRecycler extends PageCacheRecycler { public V longPage(boolean clear) { final V page = super.longPage(clear); if (!clear) { - for (int i = 0; i < page.v().length; ++i) { - page.v()[i] = random.nextLong(); - } + Arrays.fill(page.v(), 0, page.v().length, random.nextLong()); } return wrap(page); } @@ -143,9 +150,7 @@ public class MockPageCacheRecycler extends PageCacheRecycler { public V doublePage(boolean clear) { final V page = super.doublePage(clear); if (!clear) { - for (int i = 0; i < page.v().length; ++i) { - page.v()[i] = random.nextDouble() - 0.5; - } + Arrays.fill(page.v(), 0, page.v().length, random.nextDouble() - 0.5); } return wrap(page); }