Skip to content

Commit 7b30423

Browse files
christophstroblmp911de
authored andcommitted
Initialize lists with size where possible.
Closes #3941 Original pull request: #3974.
1 parent c700e9d commit 7b30423

File tree

14 files changed

+47
-29
lines changed

14 files changed

+47
-29
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/CountQuery.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,20 @@ private static Document createGeoWithin(String key, Document source, @Nullable O
176176
Document $geoWithinMin = new Document("$geoWithin",
177177
new Document(spheric ? "$centerSphere" : "$center", $centerMin));
178178

179-
List<Document> criteria = new ArrayList<>();
179+
List<Document> criteria;
180180

181181
if ($and != null) {
182182
if ($and instanceof Collection) {
183-
criteria.addAll((Collection) $and);
183+
Collection andElements = (Collection) $and;
184+
criteria = new ArrayList<>(andElements.size() + 2);
185+
criteria.addAll(andElements);
184186
} else {
185187
throw new IllegalArgumentException(
186188
"Cannot rewrite query as it contains an '$and' element that is not a Collection!: Offending element: "
187189
+ $and);
188190
}
191+
} else {
192+
criteria = new ArrayList<>(2);
189193
}
190194

191195
criteria.add(new Document("$nor", Collections.singletonList(new Document(key, $geoWithinMin))));

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/DefaultIndexOperations.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ public List<IndexInfo> doInCollection(MongoCollection<Document> collection)
188188

189189
private List<IndexInfo> getIndexData(MongoCursor<Document> cursor) {
190190

191-
List<IndexInfo> indexInfoList = new ArrayList<>();
191+
int available = cursor.available();
192+
List<IndexInfo> indexInfoList = available > 0 ? new ArrayList<>(available) : new ArrayList<>();
192193

193194
while (cursor.hasNext()) {
194195

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,7 +1022,7 @@ public <T> GeoResults<T> geoNear(NearQuery near, Class<?> domainType, String col
10221022
DocumentCallback<GeoResult<T>> callback = new GeoNearResultDocumentCallback<>(distanceField,
10231023
new ProjectingReadCallback<>(mongoConverter, projection, collection), near.getMetric());
10241024

1025-
List<GeoResult<T>> result = new ArrayList<>();
1025+
List<GeoResult<T>> result = new ArrayList<>(results.getMappedResults().size());
10261026

10271027
BigDecimal aggregate = BigDecimal.ZERO;
10281028
for (Document element : results) {
@@ -1411,7 +1411,7 @@ protected <T> Collection<T> doInsertBatch(String collectionName, Collection<? ex
14111411

14121412
Assert.notNull(writer, "MongoWriter must not be null!");
14131413

1414-
List<Document> documentList = new ArrayList<>();
1414+
List<Document> documentList = new ArrayList<>(batchToSave.size());
14151415
List<T> initializedBatchToSave = new ArrayList<>(batchToSave.size());
14161416
for (T uninitialized : batchToSave) {
14171417

@@ -2965,7 +2965,8 @@ private <T> List<T> executeFindMultiInternal(CollectionCallback<FindIterable<Doc
29652965
.initiateFind(getAndPrepareCollection(doGetDatabase(), collectionName), collectionCallback::doInCollection)
29662966
.iterator()) {
29672967

2968-
List<T> result = new ArrayList<>();
2968+
int available = cursor.available();
2969+
List<T> result = available > 0 ? new ArrayList<>(available) : new ArrayList<>();
29692970

29702971
while (cursor.hasNext()) {
29712972
Document object = cursor.next();

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/ReactiveMongoTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1712,7 +1712,7 @@ protected Flux<ObjectId> insertDocumentList(String collectionName, List<Document
17121712
LOGGER.debug(String.format("Inserting list of Documents containing %d items", dbDocList.size()));
17131713
}
17141714

1715-
List<Document> documents = new ArrayList<>();
1715+
List<Document> documents = new ArrayList<>(dbDocList.size());
17161716

17171717
return execute(collectionName, collection -> {
17181718

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ConditionalOperators.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -675,10 +675,7 @@ private Object resolveCriteria(AggregationOperationContext context, Object value
675675
if (value instanceof CriteriaDefinition) {
676676

677677
Document mappedObject = context.getMappedObject(((CriteriaDefinition) value).getCriteriaObject());
678-
List<Object> clauses = new ArrayList<Object>();
679-
680-
clauses.addAll(getClauses(context, mappedObject));
681-
678+
List<Object> clauses = getClauses(context, mappedObject);
682679
return clauses.size() == 1 ? clauses.get(0) : clauses;
683680
}
684681

@@ -705,7 +702,9 @@ private List<Object> getClauses(AggregationOperationContext context, String key,
705702

706703
if (predicate instanceof List) {
707704

708-
List<Object> args = new ArrayList<Object>();
705+
List<?> predicates = (List<?>) predicate;
706+
List<Object> args = new ArrayList<Object>(predicates.size());
707+
709708
for (Object clause : (List<?>) predicate) {
710709
if (clause instanceof Document) {
711710
args.addAll(getClauses(context, (Document) clause));
@@ -723,14 +722,14 @@ private List<Object> getClauses(AggregationOperationContext context, String key,
723722
continue;
724723
}
725724

726-
List<Object> args = new ArrayList<Object>();
725+
List<Object> args = new ArrayList<Object>(2);
727726
args.add("$" + key);
728727
args.add(nested.get(s));
729728
clauses.add(new Document(s, args));
730729
}
731730
} else if (!isKeyword(key)) {
732731

733-
List<Object> args = new ArrayList<Object>();
732+
List<Object> args = new ArrayList<Object>(2);
734733
args.add("$" + key);
735734
args.add(predicate);
736735
clauses.add(new Document("$eq", args));

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ExposedFields.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public static ExposedFields nonSynthetic(Fields fields) {
110110
private static ExposedFields createFields(Fields fields, boolean synthetic) {
111111

112112
Assert.notNull(fields, "Fields must not be null!");
113-
List<ExposedField> result = new ArrayList<ExposedField>();
113+
List<ExposedField> result = new ArrayList<ExposedField>(fields.size());
114114

115115
for (Field field : fields) {
116116
result.add(new ExposedField(field, synthetic));

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,10 @@ public Fields and(Fields fields) {
167167
return result;
168168
}
169169

170+
public int size() {
171+
return fields.size();
172+
}
173+
170174
@Nullable
171175
public Field getField(String name) {
172176

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GeoNearOperation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public List<Document> toPipelineStages(AggregationOperationContext context) {
126126
Document command = toDocument(context);
127127
Number limit = (Number) command.get("$geoNear", Document.class).remove("num");
128128

129-
List<Document> stages = new ArrayList<>();
129+
List<Document> stages = new ArrayList<>(3);
130130
stages.add(command);
131131

132132
if (nearQuery.getSkip() != null && nearQuery.getSkip() > 0) {

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public List<Document> bulkFetch(List<DBRef> refs) {
138138

139139
List<Document> result = mongoCollection //
140140
.find(new Document(BasicMongoPersistentProperty.ID_FIELD_NAME, new Document("$in", ids))) //
141-
.into(new ArrayList<>());
141+
.into(new ArrayList<>(ids.size()));
142142

143143
return ids.stream() //
144144
.flatMap(id -> documentWithId(id, result)) //

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/GeoConverters.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ public Document convert(GeoCommand source) {
462462
return null;
463463
}
464464

465-
List<Object> argument = new ArrayList<>();
465+
List<Object> argument = new ArrayList<>(2);
466466

467467
Shape shape = source.getShape();
468468

@@ -482,7 +482,9 @@ public Document convert(GeoCommand source) {
482482

483483
} else if (shape instanceof Polygon) {
484484

485-
for (Point point : ((Polygon) shape).getPoints()) {
485+
List<Point> points = ((Polygon) shape).getPoints();
486+
argument = new ArrayList(points.size());
487+
for (Point point : points) {
486488
argument.add(toList(point));
487489
}
488490

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,14 +1018,14 @@ protected List<Object> createCollection(Collection<?> collection, MongoPersisten
10181018
}).collect(Collectors.toList());
10191019

10201020
return writeCollectionInternal(targetCollection, ClassTypeInformation.from(DocumentPointer.class),
1021-
new ArrayList<>());
1021+
new ArrayList<>(targetCollection.size()));
10221022
}
10231023

10241024
if (property.hasExplicitWriteTarget()) {
1025-
return writeCollectionInternal(collection, new FieldTypeInformation<>(property), new ArrayList<>());
1025+
return writeCollectionInternal(collection, new FieldTypeInformation<>(property), new ArrayList<>(collection.size()));
10261026
}
10271027

1028-
return writeCollectionInternal(collection, property.getTypeInformation(), new ArrayList<>());
1028+
return writeCollectionInternal(collection, property.getTypeInformation(), new ArrayList<>(collection.size()));
10291029
}
10301030

10311031
List<Object> dbList = new ArrayList<>(collection.size());
@@ -1110,7 +1110,9 @@ private List<Object> writeCollectionInternal(Collection<?> source, @Nullable Typ
11101110
collection.add(getPotentiallyConvertedSimpleWrite(element,
11111111
componentType != null ? componentType.getType() : Object.class));
11121112
} else if (element instanceof Collection || elementType.isArray()) {
1113-
collection.add(writeCollectionInternal(BsonUtils.asCollection(element), componentType, new ArrayList<>()));
1113+
1114+
Collection<?> objects = BsonUtils.asCollection(element);
1115+
collection.add(writeCollectionInternal(objects, componentType, new ArrayList<>(objects.size())));
11141116
} else {
11151117
Document document = new Document();
11161118
writeInternal(element, document, componentType);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/QueryMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ protected Document getMappedKeyword(Keyword keyword, @Nullable MongoPersistentEn
376376
if (keyword.isOrOrNor() || (keyword.hasIterableValue() && !keyword.isGeometry())) {
377377

378378
Iterable<?> conditions = keyword.getValue();
379-
List<Object> newConditions = new ArrayList<>();
379+
List<Object> newConditions = conditions instanceof Collection ? new ArrayList<>(((Collection<?>) conditions).size()) : new ArrayList<>();
380380

381381
for (Object condition : conditions) {
382382
newConditions.add(isDocument(condition) ? getMappedObject((Document) condition, entity)

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ public Criteria nin(Collection<?> values) {
337337
* @see <a href="https://docs.mongodb.com/manual/reference/operator/query/mod/">MongoDB Query operator: $mod</a>
338338
*/
339339
public Criteria mod(Number value, Number remainder) {
340-
List<Object> l = new ArrayList<Object>();
340+
List<Object> l = new ArrayList<Object>(2);
341341
l.add(value);
342342
l.add(remainder);
343343
criteria.put("$mod", l);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/ConvertingParameterAccessor.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,10 @@ public Object nextConverted(MongoPersistentProperty property) {
229229
if (property.isAssociation()) {
230230
if (next.getClass().isArray() || next instanceof Iterable) {
231231

232-
List<DBRef> dbRefs = new ArrayList<DBRef>();
233-
for (Object element : asCollection(next)) {
232+
Collection<?> values = asCollection(next);
233+
234+
List<DBRef> dbRefs = new ArrayList<DBRef>(values.size());
235+
for (Object element : values) {
234236
dbRefs.add(writer.toDBRef(element, property));
235237
}
236238

@@ -264,11 +266,14 @@ private static Collection<?> asCollection(@Nullable Object source) {
264266

265267
if (source instanceof Iterable) {
266268

267-
List<Object> result = new ArrayList<Object>();
269+
if(source instanceof Collection) {
270+
return new ArrayList<>((Collection<?>) source);
271+
}
272+
273+
List<Object> result = new ArrayList<>();
268274
for (Object element : (Iterable<?>) source) {
269275
result.add(element);
270276
}
271-
272277
return result;
273278
}
274279

0 commit comments

Comments
 (0)