Skip to content

Commit 3c3d013

Browse files
committed
improve all stream analysis performance
1 parent 8de7bea commit 3c3d013

File tree

5 files changed

+100
-44
lines changed

5 files changed

+100
-44
lines changed

.idea/runConfigurations/Bootstrap__No_Plugins_.xml

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

modules/benchmark/micro/src/main/java/org/elasticsearch/benchmark/stress/SingleThreadIndexingStress.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.elasticsearch.node.Node;
2727

2828
import java.io.IOException;
29-
import java.util.Random;
3029
import java.util.concurrent.TimeUnit;
3130

3231
import static org.elasticsearch.client.Requests.*;
@@ -42,8 +41,6 @@
4241
public class SingleThreadIndexingStress {
4342

4443
public static void main(String[] args) throws Exception {
45-
Random random = new Random();
46-
4744
Settings settings = settingsBuilder()
4845
.put("cluster.routing.schedule", 200, TimeUnit.MILLISECONDS)
4946
.put("index.engine.robin.refreshInterval", "-1")
@@ -52,8 +49,10 @@ public static void main(String[] args) throws Exception {
5249
.put(SETTING_NUMBER_OF_REPLICAS, 1)
5350
.build();
5451

55-
Node node1 = nodeBuilder().settings(settingsBuilder().put(settings).put("name", "server1")).node();
56-
Node node2 = nodeBuilder().settings(settingsBuilder().put(settings).put("name", "server2")).node();
52+
Node[] nodes = new Node[1];
53+
for (int i = 0; i < nodes.length; i++) {
54+
nodes[i] = nodeBuilder().settings(settingsBuilder().put(settings).put("name", "node" + i)).node();
55+
}
5756

5857
Node client = nodeBuilder().settings(settingsBuilder().put(settings).put("name", "client")).client(true).node();
5958

@@ -82,11 +81,26 @@ public static void main(String[] args) throws Exception {
8281

8382
client.close();
8483

85-
node1.close();
86-
node2.close();
84+
for (Node node : nodes) {
85+
node.close();
86+
}
8787
}
8888

8989
private static XContentBuilder source(String id, String nameValue) throws IOException {
90-
return jsonBuilder().startObject().field("id", id).field("name", nameValue).endObject();
90+
long time = System.currentTimeMillis();
91+
return jsonBuilder().startObject()
92+
.field("id", id)
93+
.field("numeric1", time)
94+
.field("numeric2", time)
95+
.field("numeric3", time)
96+
.field("numeric4", time)
97+
.field("numeric5", time)
98+
.field("numeric6", time)
99+
.field("numeric7", time)
100+
.field("numeric8", time)
101+
.field("numeric9", time)
102+
.field("numeric10", time)
103+
.field("name", nameValue)
104+
.endObject();
91105
}
92106
}

modules/elasticsearch/src/main/java/org/elasticsearch/common/io/FastStringReader.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,7 @@ private void ensureOpen() throws IOException {
9494
*/
9595
@Override public int read(char cbuf[], int off, int len) throws IOException {
9696
ensureOpen();
97-
if ((off < 0) || (off > cbuf.length) || (len < 0) ||
98-
((off + len) > cbuf.length) || ((off + len) < 0)) {
99-
throw new IndexOutOfBoundsException();
100-
} else if (len == 0) {
97+
if (len == 0) {
10198
return 0;
10299
}
103100
if (next >= length)

modules/elasticsearch/src/main/java/org/elasticsearch/common/lucene/all/AllEntries.java

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.elasticsearch.ElasticSearchIllegalStateException;
2323
import org.elasticsearch.common.collect.Lists;
24-
import org.elasticsearch.common.io.CharSequenceReader;
2524
import org.elasticsearch.common.io.FastCharArrayWriter;
2625
import org.elasticsearch.common.io.FastStringReader;
2726

@@ -40,10 +39,10 @@ public class AllEntries extends Reader {
4039

4140
public static class Entry {
4241
private final String name;
43-
private final CharSequenceReader reader;
42+
private final FastStringReader reader;
4443
private final float boost;
4544

46-
public Entry(String name, CharSequenceReader reader, float boost) {
45+
public Entry(String name, FastStringReader reader, float boost) {
4746
this.name = name;
4847
this.reader = reader;
4948
this.boost = boost;
@@ -57,7 +56,7 @@ public float boost() {
5756
return this.boost;
5857
}
5958

60-
public CharSequenceReader reader() {
59+
public FastStringReader reader() {
6160
return this.reader;
6261
}
6362
}
@@ -70,7 +69,12 @@ public CharSequenceReader reader() {
7069

7170
private boolean itsSeparatorTime = false;
7271

72+
private boolean customBoost = false;
73+
7374
public void addText(String name, String text, float boost) {
75+
if (boost != 1.0f) {
76+
customBoost = true;
77+
}
7478
Entry entry = new Entry(name, new FastStringReader(text), boost);
7579
entries.add(entry);
7680
}
@@ -129,29 +133,52 @@ public Entry current() {
129133
if (current == null) {
130134
return -1;
131135
}
132-
int result = current.reader().read(cbuf, off, len);
133-
if (result == -1) {
134-
if (itsSeparatorTime) {
135-
itsSeparatorTime = false;
136-
cbuf[off] = ' ';
137-
return 1;
136+
if (customBoost) {
137+
int result = current.reader().read(cbuf, off, len);
138+
if (result == -1) {
139+
if (itsSeparatorTime) {
140+
itsSeparatorTime = false;
141+
cbuf[off] = ' ';
142+
return 1;
143+
}
144+
itsSeparatorTime = true;
145+
// close(); No need to close, we work on in mem readers
146+
if (it.hasNext()) {
147+
current = it.next();
148+
} else {
149+
current = null;
150+
}
151+
return read(cbuf, off, len);
138152
}
139-
itsSeparatorTime = true;
140-
advance();
141-
return read(cbuf, off, len);
153+
return result;
154+
} else {
155+
int read = 0;
156+
while (len > 0) {
157+
int result = current.reader().read(cbuf, off, len);
158+
if (result == -1) {
159+
if (it.hasNext()) {
160+
current = it.next();
161+
} else {
162+
current = null;
163+
return read;
164+
}
165+
cbuf[off++] = ' ';
166+
read++;
167+
len--;
168+
} else {
169+
read += result;
170+
off += result;
171+
len -= result;
172+
}
173+
}
174+
return read;
142175
}
143-
return result;
144176
}
145177

146178
@Override public void close() {
147179
if (current != null) {
148-
try {
149-
current.reader().close();
150-
} catch (IOException e) {
151-
// can't happen...
152-
} finally {
153-
current = null;
154-
}
180+
current.reader().close();
181+
current = null;
155182
}
156183
}
157184

@@ -160,16 +187,6 @@ public Entry current() {
160187
return (current != null) && current.reader().ready();
161188
}
162189

163-
/**
164-
* Closes the current reader and opens the next one, if any.
165-
*/
166-
private void advance() {
167-
close();
168-
if (it.hasNext()) {
169-
current = it.next();
170-
}
171-
}
172-
173190
@Override public String toString() {
174191
StringBuilder sb = new StringBuilder();
175192
for (Entry entry : entries) {

modules/elasticsearch/src/test/java/org/elasticsearch/common/lucene/all/SimpleAllTests.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import org.elasticsearch.common.lucene.Lucene;
3232
import org.testng.annotations.Test;
3333

34+
import java.io.IOException;
35+
3436
import static org.hamcrest.MatcherAssert.*;
3537
import static org.hamcrest.Matchers.*;
3638

@@ -40,6 +42,31 @@
4042
@Test
4143
public class SimpleAllTests {
4244

45+
@Test public void testAllEntriesRead() throws Exception {
46+
AllEntries allEntries = new AllEntries();
47+
allEntries.addText("field1", "something", 1.0f);
48+
allEntries.addText("field2", "else", 1.0f);
49+
50+
for (int i = 1; i < 30; i++) {
51+
allEntries.reset();
52+
char[] data = new char[i];
53+
String value = slurpToString(allEntries, data);
54+
assertThat("failed for " + i, value, equalTo("something else"));
55+
}
56+
}
57+
58+
private String slurpToString(AllEntries allEntries, char[] data) throws IOException {
59+
StringBuilder sb = new StringBuilder();
60+
while (true) {
61+
int read = allEntries.read(data, 0, data.length);
62+
if (read == -1) {
63+
break;
64+
}
65+
sb.append(data, 0, read);
66+
}
67+
return sb.toString();
68+
}
69+
4370
@Test public void testSimpleAllNoBoost() throws Exception {
4471
Directory dir = new RAMDirectory();
4572
IndexWriter indexWriter = new IndexWriter(dir, Lucene.STANDARD_ANALYZER, true, IndexWriter.MaxFieldLength.UNLIMITED);

0 commit comments

Comments
 (0)