Skip to content

Commit 27e6b5a

Browse files
christophstroblmp911de
authored andcommitted
Initialize lists with size where possible.
Closes #3941 Original pull request: #3974.
1 parent c9be849 commit 27e6b5a

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
@@ -987,7 +987,7 @@ public <T> GeoResults<T> geoNear(NearQuery near, Class<?> domainType, String col
987987
DocumentCallback<GeoResult<T>> callback = new GeoNearResultDocumentCallback<>(distanceField,
988988
new ProjectingReadCallback<>(mongoConverter, domainType, returnType, collection), near.getMetric());
989989

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

992992
BigDecimal aggregate = BigDecimal.ZERO;
993993
for (Document element : results) {
@@ -1345,7 +1345,7 @@ protected <T> Collection<T> doInsertBatch(String collectionName, Collection<? ex
13451345

13461346
Assert.notNull(writer, "MongoWriter must not be null!");
13471347

1348-
List<Document> documentList = new ArrayList<>();
1348+
List<Document> documentList = new ArrayList<>(batchToSave.size());
13491349
List<T> initializedBatchToSave = new ArrayList<>(batchToSave.size());
13501350
for (T uninitialized : batchToSave) {
13511351

@@ -2852,7 +2852,8 @@ private <T> List<T> executeFindMultiInternal(CollectionCallback<FindIterable<Doc
28522852
.initiateFind(getAndPrepareCollection(doGetDatabase(), collectionName), collectionCallback::doInCollection)
28532853
.iterator()) {
28542854

2855-
List<T> result = new ArrayList<>();
2855+
int available = cursor.available();
2856+
List<T> result = available > 0 ? new ArrayList<>(available) : new ArrayList<>();
28562857

28572858
while (cursor.hasNext()) {
28582859
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
@@ -1632,7 +1632,7 @@ protected Flux<ObjectId> insertDocumentList(String collectionName, List<Document
16321632
LOGGER.debug("Inserting list of Documents containing " + dbDocList.size() + " items");
16331633
}
16341634

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

16371637
return execute(collectionName, collection -> {
16381638

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
@@ -886,14 +886,14 @@ protected List<Object> createCollection(Collection<?> collection, MongoPersisten
886886
}).collect(Collectors.toList());
887887

888888
return writeCollectionInternal(targetCollection, ClassTypeInformation.from(DocumentPointer.class),
889-
new ArrayList<>());
889+
new ArrayList<>(targetCollection.size()));
890890
}
891891

892892
if (property.hasExplicitWriteTarget()) {
893-
return writeCollectionInternal(collection, new FieldTypeInformation<>(property), new ArrayList<>());
893+
return writeCollectionInternal(collection, new FieldTypeInformation<>(property), new ArrayList<>(collection.size()));
894894
}
895895

896-
return writeCollectionInternal(collection, property.getTypeInformation(), new ArrayList<>());
896+
return writeCollectionInternal(collection, property.getTypeInformation(), new ArrayList<>(collection.size()));
897897
}
898898

899899
List<Object> dbList = new ArrayList<>(collection.size());
@@ -978,7 +978,9 @@ private List<Object> writeCollectionInternal(Collection<?> source, @Nullable Typ
978978
collection.add(getPotentiallyConvertedSimpleWrite(element,
979979
componentType != null ? componentType.getType() : Object.class));
980980
} else if (element instanceof Collection || elementType.isArray()) {
981-
collection.add(writeCollectionInternal(BsonUtils.asCollection(element), componentType, new ArrayList<>()));
981+
982+
Collection<?> objects = BsonUtils.asCollection(element);
983+
collection.add(writeCollectionInternal(objects, componentType, new ArrayList<>(objects.size())));
982984
} else {
983985
Document document = new Document();
984986
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
@@ -363,7 +363,7 @@ protected Document getMappedKeyword(Keyword keyword, @Nullable MongoPersistentEn
363363
if (keyword.isOrOrNor() || (keyword.hasIterableValue() && !keyword.isGeometry())) {
364364

365365
Iterable<?> conditions = keyword.getValue();
366-
List<Object> newConditions = new ArrayList<>();
366+
List<Object> newConditions = conditions instanceof Collection ? new ArrayList<>(((Collection<?>) conditions).size()) : new ArrayList<>();
367367

368368
for (Object condition : conditions) {
369369
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
@@ -223,8 +223,10 @@ public Object nextConverted(MongoPersistentProperty property) {
223223
if (property.isAssociation()) {
224224
if (next.getClass().isArray() || next instanceof Iterable) {
225225

226-
List<DBRef> dbRefs = new ArrayList<DBRef>();
227-
for (Object element : asCollection(next)) {
226+
Collection<?> values = asCollection(next);
227+
228+
List<DBRef> dbRefs = new ArrayList<DBRef>(values.size());
229+
for (Object element : values) {
228230
dbRefs.add(writer.toDBRef(element, property));
229231
}
230232

@@ -258,11 +260,14 @@ private static Collection<?> asCollection(@Nullable Object source) {
258260

259261
if (source instanceof Iterable) {
260262

261-
List<Object> result = new ArrayList<Object>();
263+
if(source instanceof Collection) {
264+
return new ArrayList<>((Collection<?>) source);
265+
}
266+
267+
List<Object> result = new ArrayList<>();
262268
for (Object element : (Iterable<?>) source) {
263269
result.add(element);
264270
}
265-
266271
return result;
267272
}
268273

0 commit comments

Comments
 (0)