Skip to content

Commit 89314f2

Browse files
committed
improve multi value field cache handling both in terms of memory usage and GC behavior
1 parent 233ed1f commit 89314f2

File tree

1 file changed

+86
-48
lines changed

1 file changed

+86
-48
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/index/mapper/xcontent/geo/MultiValueGeoPointFieldData.java

Lines changed: 86 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -86,98 +86,136 @@ public MultiValueGeoPointFieldData(String fieldName, int[][] ordinals, double[]
8686
}
8787

8888
@Override public boolean hasValue(int docId) {
89-
return ordinals[docId] != null;
89+
for (int[] ordinal : ordinals) {
90+
if (ordinal[docId] != 0) {
91+
return true;
92+
}
93+
}
94+
return false;
9095
}
9196

9297
@Override public void forEachValueInDoc(int docId, StringValueInDocProc proc) {
93-
int[] docOrders = ordinals[docId];
94-
if (docOrders == null) {
95-
return;
96-
}
97-
for (int docOrder : docOrders) {
98-
proc.onValue(docId, GeoHashUtils.encode(lat[docOrder], lon[docOrder]));
98+
for (int[] ordinal : ordinals) {
99+
int loc = ordinal[docId];
100+
if (loc != 0) {
101+
proc.onValue(docId, GeoHashUtils.encode(lat[loc], lon[loc]));
102+
}
99103
}
100104
}
101105

102106
@Override public GeoPoint value(int docId) {
103-
int[] docOrders = ordinals[docId];
104-
if (docOrders == null) {
105-
return null;
106-
}
107-
GeoPoint point = valuesCache.get().get();
108-
int loc = docOrders[0];
109-
point.latlon(lat[loc], lon[loc]);
110-
return point;
107+
for (int[] ordinal : ordinals) {
108+
int loc = ordinal[docId];
109+
if (loc != 0) {
110+
GeoPoint point = valuesCache.get().get();
111+
point.latlon(lat[loc], lon[loc]);
112+
return point;
113+
}
114+
}
115+
return null;
111116
}
112117

113118
@Override public GeoPoint[] values(int docId) {
114-
int[] docOrders = ordinals[docId];
115-
if (docOrders == null) {
119+
int length = 0;
120+
for (int[] ordinal : ordinals) {
121+
if (ordinal[docId] != 0) {
122+
length++;
123+
}
124+
}
125+
if (length == 0) {
116126
return EMPTY_ARRAY;
117127
}
118128
GeoPoint[] points;
119-
if (docOrders.length < VALUE_CACHE_SIZE) {
120-
points = valuesArrayCache.get().get()[docOrders.length];
121-
for (int i = 0; i < docOrders.length; i++) {
122-
int loc = docOrders[i];
123-
points[i].latlon(lat[loc], lon[loc]);
129+
if (length < VALUE_CACHE_SIZE) {
130+
points = valuesArrayCache.get().get()[length];
131+
int i = 0;
132+
for (int[] ordinal : ordinals) {
133+
int loc = ordinal[docId];
134+
if (loc != 0) {
135+
points[i++].latlon(lat[loc], lon[loc]);
136+
}
124137
}
125138
} else {
126-
points = new GeoPoint[docOrders.length];
127-
for (int i = 0; i < docOrders.length; i++) {
128-
int loc = docOrders[i];
129-
points[i] = new GeoPoint(lat[loc], lon[loc]);
139+
points = new GeoPoint[length];
140+
int i = 0;
141+
for (int[] ordinal : ordinals) {
142+
int loc = ordinal[docId];
143+
if (loc != 0) {
144+
points[i++] = new GeoPoint(lat[loc], lon[loc]);
145+
}
130146
}
131147
}
132148
return points;
133149
}
134150

135151
@Override public double latValue(int docId) {
136-
int[] docOrders = ordinals[docId];
137-
if (docOrders == null) {
138-
return 0;
152+
for (int[] ordinal : ordinals) {
153+
int loc = ordinal[docId];
154+
if (loc != 0) {
155+
return lat[loc];
156+
}
139157
}
140-
return lat[docOrders[0]];
158+
return 0;
141159
}
142160

143161
@Override public double lonValue(int docId) {
144-
int[] docOrders = ordinals[docId];
145-
if (docOrders == null) {
146-
return 0;
162+
for (int[] ordinal : ordinals) {
163+
int loc = ordinal[docId];
164+
if (loc != 0) {
165+
return lon[loc];
166+
}
147167
}
148-
return lon[docOrders[0]];
168+
return 0;
149169
}
150170

151171
@Override public double[] latValues(int docId) {
152-
int[] docOrders = ordinals[docId];
153-
if (docOrders == null) {
172+
int length = 0;
173+
for (int[] ordinal : ordinals) {
174+
if (ordinal[docId] != 0) {
175+
length++;
176+
}
177+
}
178+
if (length == 0) {
154179
return DoubleFieldData.EMPTY_DOUBLE_ARRAY;
155180
}
156181
double[] doubles;
157-
if (docOrders.length < VALUE_CACHE_SIZE) {
158-
doubles = valuesLatCache.get().get()[docOrders.length];
182+
if (length < VALUE_CACHE_SIZE) {
183+
doubles = valuesLatCache.get().get()[length];
159184
} else {
160-
doubles = new double[docOrders.length];
185+
doubles = new double[length];
161186
}
162-
for (int i = 0; i < docOrders.length; i++) {
163-
doubles[i] = lat[docOrders[i]];
187+
int i = 0;
188+
for (int[] ordinal : ordinals) {
189+
int loc = ordinal[docId];
190+
if (loc != 0) {
191+
doubles[i++] = lat[loc];
192+
}
164193
}
165194
return doubles;
166195
}
167196

168197
@Override public double[] lonValues(int docId) {
169-
int[] docOrders = ordinals[docId];
170-
if (docOrders == null) {
198+
int length = 0;
199+
for (int[] ordinal : ordinals) {
200+
if (ordinal[docId] != 0) {
201+
length++;
202+
}
203+
}
204+
if (length == 0) {
171205
return DoubleFieldData.EMPTY_DOUBLE_ARRAY;
172206
}
173207
double[] doubles;
174-
if (docOrders.length < VALUE_CACHE_SIZE) {
175-
doubles = valuesLonCache.get().get()[docOrders.length];
208+
if (length < VALUE_CACHE_SIZE) {
209+
doubles = valuesLonCache.get().get()[length];
176210
} else {
177-
doubles = new double[docOrders.length];
211+
doubles = new double[length];
178212
}
179-
for (int i = 0; i < docOrders.length; i++) {
180-
doubles[i] = lon[docOrders[i]];
213+
int i = 0;
214+
for (int[] ordinal : ordinals) {
215+
int loc = ordinal[docId];
216+
if (loc != 0) {
217+
doubles[i++] = lon[loc];
218+
}
181219
}
182220
return doubles;
183221
}

0 commit comments

Comments
 (0)