Skip to content

Commit 09b2afa

Browse files
christophstroblmp911de
authored andcommitted
Initialize lists with size where possible.
Closes #3941 Original pull request: #3974.
1 parent 96b564e commit 09b2afa

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
@@ -172,7 +172,8 @@ public List<IndexInfo> doInCollection(MongoCollection<Document> collection)
172172

173173
private List<IndexInfo> getIndexData(MongoCursor<Document> cursor) {
174174

175-
List<IndexInfo> indexInfoList = new ArrayList<>();
175+
int available = cursor.available();
176+
List<IndexInfo> indexInfoList = available > 0 ? new ArrayList<>(available) : new ArrayList<>();
176177

177178
while (cursor.hasNext()) {
178179

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
@@ -945,7 +945,7 @@ public <T> GeoResults<T> geoNear(NearQuery near, Class<?> domainType, String col
945945
DocumentCallback<GeoResult<T>> callback = new GeoNearResultDocumentCallback<>(distanceField,
946946
new ProjectingReadCallback<>(mongoConverter, projection, collection), near.getMetric());
947947

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

950950
BigDecimal aggregate = BigDecimal.ZERO;
951951
for (Document element : results) {
@@ -1311,7 +1311,7 @@ protected <T> Collection<T> doInsertBatch(String collectionName, Collection<? ex
13111311

13121312
Assert.notNull(writer, "MongoWriter must not be null");
13131313

1314-
List<Document> documentList = new ArrayList<>();
1314+
List<Document> documentList = new ArrayList<>(batchToSave.size());
13151315
List<T> initializedBatchToSave = new ArrayList<>(batchToSave.size());
13161316
for (T uninitialized : batchToSave) {
13171317

@@ -2717,7 +2717,8 @@ private <T> List<T> executeFindMultiInternal(CollectionCallback<FindIterable<Doc
27172717
.initiateFind(getAndPrepareCollection(doGetDatabase(), collectionName), collectionCallback::doInCollection)
27182718
.iterator()) {
27192719

2720-
List<T> result = new ArrayList<>();
2720+
int available = cursor.available();
2721+
List<T> result = available > 0 ? new ArrayList<>(available) : new ArrayList<>();
27212722

27222723
while (cursor.hasNext()) {
27232724
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
@@ -1474,7 +1474,7 @@ protected Flux<ObjectId> insertDocumentList(String collectionName, List<Document
14741474
LOGGER.debug(String.format("Inserting list of Documents containing %d items", dbDocList.size()));
14751475
}
14761476

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

14791479
return execute(collectionName, collection -> {
14801480

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
@@ -649,10 +649,7 @@ private Object resolveCriteria(AggregationOperationContext context, Object value
649649
if (value instanceof CriteriaDefinition) {
650650

651651
Document mappedObject = context.getMappedObject(((CriteriaDefinition) value).getCriteriaObject());
652-
List<Object> clauses = new ArrayList<Object>();
653-
654-
clauses.addAll(getClauses(context, mappedObject));
655-
652+
List<Object> clauses = getClauses(context, mappedObject);
656653
return clauses.size() == 1 ? clauses.get(0) : clauses;
657654
}
658655

@@ -679,7 +676,9 @@ private List<Object> getClauses(AggregationOperationContext context, String key,
679676

680677
if (predicate instanceof List) {
681678

682-
List<Object> args = new ArrayList<Object>();
679+
List<?> predicates = (List<?>) predicate;
680+
List<Object> args = new ArrayList<Object>(predicates.size());
681+
683682
for (Object clause : (List<?>) predicate) {
684683
if (clause instanceof Document) {
685684
args.addAll(getClauses(context, (Document) clause));
@@ -697,14 +696,14 @@ private List<Object> getClauses(AggregationOperationContext context, String key,
697696
continue;
698697
}
699698

700-
List<Object> args = new ArrayList<Object>();
699+
List<Object> args = new ArrayList<Object>(2);
701700
args.add("$" + key);
702701
args.add(nested.get(s));
703702
clauses.add(new Document(s, args));
704703
}
705704
} else if (!isKeyword(key)) {
706705

707-
List<Object> args = new ArrayList<Object>();
706+
List<Object> args = new ArrayList<Object>(2);
708707
args.add("$" + key);
709708
args.add(predicate);
710709
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
@@ -114,7 +114,7 @@ public List<Document> toPipelineStages(AggregationOperationContext context) {
114114
Document command = toDocument(context);
115115
Number limit = (Number) command.get("$geoNear", Document.class).remove("num");
116116

117-
List<Document> stages = new ArrayList<>();
117+
List<Document> stages = new ArrayList<>(3);
118118
stages.add(command);
119119

120120
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
@@ -126,7 +126,7 @@ public List<Document> bulkFetch(List<DBRef> refs) {
126126

127127
List<Document> result = mongoCollection //
128128
.find(new Document(BasicMongoPersistentProperty.ID_FIELD_NAME, new Document("$in", ids))) //
129-
.into(new ArrayList<>());
129+
.into(new ArrayList<>(ids.size()));
130130

131131
return ids.stream() //
132132
.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
@@ -418,7 +418,7 @@ public Document convert(GeoCommand source) {
418418
return null;
419419
}
420420

421-
List<Object> argument = new ArrayList<>();
421+
List<Object> argument = new ArrayList<>(2);
422422

423423
Shape shape = source.getShape();
424424

@@ -438,7 +438,9 @@ public Document convert(GeoCommand source) {
438438

439439
} else if (shape instanceof Polygon) {
440440

441-
for (Point point : ((Polygon) shape).getPoints()) {
441+
List<Point> points = ((Polygon) shape).getPoints();
442+
argument = new ArrayList(points.size());
443+
for (Point point : points) {
442444
argument.add(toList(point));
443445
}
444446

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
@@ -1000,14 +1000,14 @@ protected List<Object> createCollection(Collection<?> collection, MongoPersisten
10001000
.getPointer();
10011001
}).collect(Collectors.toList());
10021002

1003-
return writeCollectionInternal(targetCollection, TypeInformation.of(DocumentPointer.class), new ArrayList<>());
1003+
return writeCollectionInternal(targetCollection, TypeInformation.of(DocumentPointer.class), new ArrayList<>(targetCollection.size()));
10041004
}
10051005

10061006
if (property.hasExplicitWriteTarget()) {
1007-
return writeCollectionInternal(collection, new FieldTypeInformation<>(property), new ArrayList<>());
1007+
return writeCollectionInternal(collection, new FieldTypeInformation<>(property), new ArrayList<>(collection.size()));
10081008
}
10091009

1010-
return writeCollectionInternal(collection, property.getTypeInformation(), new ArrayList<>());
1010+
return writeCollectionInternal(collection, property.getTypeInformation(), new ArrayList<>(collection.size()));
10111011
}
10121012

10131013
List<Object> dbList = new ArrayList<>(collection.size());
@@ -1092,7 +1092,9 @@ private List<Object> writeCollectionInternal(Collection<?> source, @Nullable Typ
10921092
collection.add(getPotentiallyConvertedSimpleWrite(element,
10931093
componentType != null ? componentType.getType() : Object.class));
10941094
} else if (element instanceof Collection || elementType.isArray()) {
1095-
collection.add(writeCollectionInternal(BsonUtils.asCollection(element), componentType, new ArrayList<>()));
1095+
1096+
Collection<?> objects = BsonUtils.asCollection(element);
1097+
collection.add(writeCollectionInternal(objects, componentType, new ArrayList<>(objects.size())));
10961098
} else {
10971099
Document document = new Document();
10981100
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
@@ -373,7 +373,7 @@ protected Document getMappedKeyword(Keyword keyword, @Nullable MongoPersistentEn
373373
if (keyword.isOrOrNor() || (keyword.hasIterableValue() && !keyword.isGeometry())) {
374374

375375
Iterable<?> conditions = keyword.getValue();
376-
List<Object> newConditions = new ArrayList<>();
376+
List<Object> newConditions = conditions instanceof Collection ? new ArrayList<>(((Collection<?>) conditions).size()) : new ArrayList<>();
377377

378378
for (Object condition : conditions) {
379379
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
@@ -165,8 +165,10 @@ public Object nextConverted(MongoPersistentProperty property) {
165165
if (property.isAssociation()) {
166166
if (next.getClass().isArray() || next instanceof Iterable) {
167167

168-
List<DBRef> dbRefs = new ArrayList<DBRef>();
169-
for (Object element : asCollection(next)) {
168+
Collection<?> values = asCollection(next);
169+
170+
List<DBRef> dbRefs = new ArrayList<DBRef>(values.size());
171+
for (Object element : values) {
170172
dbRefs.add(writer.toDBRef(element, property));
171173
}
172174

@@ -196,11 +198,14 @@ private static Collection<?> asCollection(@Nullable Object source) {
196198

197199
if (source instanceof Iterable) {
198200

199-
List<Object> result = new ArrayList<Object>();
201+
if(source instanceof Collection) {
202+
return new ArrayList<>((Collection<?>) source);
203+
}
204+
205+
List<Object> result = new ArrayList<>();
200206
for (Object element : (Iterable<?>) source) {
201207
result.add(element);
202208
}
203-
204209
return result;
205210
}
206211

0 commit comments

Comments
 (0)