Initialize lists with size where possible.
Closes #3941 Original pull request: #3974.
This commit is contained in:
committed by
Mark Paluch
parent
96b564eb9a
commit
09b2afa79d
@@ -176,16 +176,20 @@ class CountQuery {
|
||||
Document $geoWithinMin = new Document("$geoWithin",
|
||||
new Document(spheric ? "$centerSphere" : "$center", $centerMin));
|
||||
|
||||
List<Document> criteria = new ArrayList<>();
|
||||
List<Document> criteria;
|
||||
|
||||
if ($and != null) {
|
||||
if ($and instanceof Collection) {
|
||||
criteria.addAll((Collection) $and);
|
||||
Collection andElements = (Collection) $and;
|
||||
criteria = new ArrayList<>(andElements.size() + 2);
|
||||
criteria.addAll(andElements);
|
||||
} else {
|
||||
throw new IllegalArgumentException(
|
||||
"Cannot rewrite query as it contains an '$and' element that is not a Collection: Offending element: "
|
||||
+ $and);
|
||||
}
|
||||
} else {
|
||||
criteria = new ArrayList<>(2);
|
||||
}
|
||||
|
||||
criteria.add(new Document("$nor", Collections.singletonList(new Document(key, $geoWithinMin))));
|
||||
|
||||
@@ -172,7 +172,8 @@ public class DefaultIndexOperations implements IndexOperations {
|
||||
|
||||
private List<IndexInfo> getIndexData(MongoCursor<Document> cursor) {
|
||||
|
||||
List<IndexInfo> indexInfoList = new ArrayList<>();
|
||||
int available = cursor.available();
|
||||
List<IndexInfo> indexInfoList = available > 0 ? new ArrayList<>(available) : new ArrayList<>();
|
||||
|
||||
while (cursor.hasNext()) {
|
||||
|
||||
|
||||
@@ -945,7 +945,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
DocumentCallback<GeoResult<T>> callback = new GeoNearResultDocumentCallback<>(distanceField,
|
||||
new ProjectingReadCallback<>(mongoConverter, projection, collection), near.getMetric());
|
||||
|
||||
List<GeoResult<T>> result = new ArrayList<>();
|
||||
List<GeoResult<T>> result = new ArrayList<>(results.getMappedResults().size());
|
||||
|
||||
BigDecimal aggregate = BigDecimal.ZERO;
|
||||
for (Document element : results) {
|
||||
@@ -1311,7 +1311,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
|
||||
Assert.notNull(writer, "MongoWriter must not be null");
|
||||
|
||||
List<Document> documentList = new ArrayList<>();
|
||||
List<Document> documentList = new ArrayList<>(batchToSave.size());
|
||||
List<T> initializedBatchToSave = new ArrayList<>(batchToSave.size());
|
||||
for (T uninitialized : batchToSave) {
|
||||
|
||||
@@ -2717,7 +2717,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware,
|
||||
.initiateFind(getAndPrepareCollection(doGetDatabase(), collectionName), collectionCallback::doInCollection)
|
||||
.iterator()) {
|
||||
|
||||
List<T> result = new ArrayList<>();
|
||||
int available = cursor.available();
|
||||
List<T> result = available > 0 ? new ArrayList<>(available) : new ArrayList<>();
|
||||
|
||||
while (cursor.hasNext()) {
|
||||
Document object = cursor.next();
|
||||
|
||||
@@ -1474,7 +1474,7 @@ public class ReactiveMongoTemplate implements ReactiveMongoOperations, Applicati
|
||||
LOGGER.debug(String.format("Inserting list of Documents containing %d items", dbDocList.size()));
|
||||
}
|
||||
|
||||
List<Document> documents = new ArrayList<>();
|
||||
List<Document> documents = new ArrayList<>(dbDocList.size());
|
||||
|
||||
return execute(collectionName, collection -> {
|
||||
|
||||
|
||||
@@ -649,10 +649,7 @@ public class ConditionalOperators {
|
||||
if (value instanceof CriteriaDefinition) {
|
||||
|
||||
Document mappedObject = context.getMappedObject(((CriteriaDefinition) value).getCriteriaObject());
|
||||
List<Object> clauses = new ArrayList<Object>();
|
||||
|
||||
clauses.addAll(getClauses(context, mappedObject));
|
||||
|
||||
List<Object> clauses = getClauses(context, mappedObject);
|
||||
return clauses.size() == 1 ? clauses.get(0) : clauses;
|
||||
}
|
||||
|
||||
@@ -679,7 +676,9 @@ public class ConditionalOperators {
|
||||
|
||||
if (predicate instanceof List) {
|
||||
|
||||
List<Object> args = new ArrayList<Object>();
|
||||
List<?> predicates = (List<?>) predicate;
|
||||
List<Object> args = new ArrayList<Object>(predicates.size());
|
||||
|
||||
for (Object clause : (List<?>) predicate) {
|
||||
if (clause instanceof Document) {
|
||||
args.addAll(getClauses(context, (Document) clause));
|
||||
@@ -697,14 +696,14 @@ public class ConditionalOperators {
|
||||
continue;
|
||||
}
|
||||
|
||||
List<Object> args = new ArrayList<Object>();
|
||||
List<Object> args = new ArrayList<Object>(2);
|
||||
args.add("$" + key);
|
||||
args.add(nested.get(s));
|
||||
clauses.add(new Document(s, args));
|
||||
}
|
||||
} else if (!isKeyword(key)) {
|
||||
|
||||
List<Object> args = new ArrayList<Object>();
|
||||
List<Object> args = new ArrayList<Object>(2);
|
||||
args.add("$" + key);
|
||||
args.add(predicate);
|
||||
clauses.add(new Document("$eq", args));
|
||||
|
||||
@@ -110,7 +110,7 @@ public final class ExposedFields implements Iterable<ExposedField> {
|
||||
private static ExposedFields createFields(Fields fields, boolean synthetic) {
|
||||
|
||||
Assert.notNull(fields, "Fields must not be null");
|
||||
List<ExposedField> result = new ArrayList<ExposedField>();
|
||||
List<ExposedField> result = new ArrayList<ExposedField>(fields.size());
|
||||
|
||||
for (Field field : fields) {
|
||||
result.add(new ExposedField(field, synthetic));
|
||||
|
||||
@@ -167,6 +167,10 @@ public final class Fields implements Iterable<Field> {
|
||||
return result;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return fields.size();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public Field getField(String name) {
|
||||
|
||||
|
||||
@@ -114,7 +114,7 @@ public class GeoNearOperation implements AggregationOperation {
|
||||
Document command = toDocument(context);
|
||||
Number limit = (Number) command.get("$geoNear", Document.class).remove("num");
|
||||
|
||||
List<Document> stages = new ArrayList<>();
|
||||
List<Document> stages = new ArrayList<>(3);
|
||||
stages.add(command);
|
||||
|
||||
if (nearQuery.getSkip() != null && nearQuery.getSkip() > 0) {
|
||||
|
||||
@@ -126,7 +126,7 @@ public class DefaultDbRefResolver extends DefaultReferenceResolver implements Db
|
||||
|
||||
List<Document> result = mongoCollection //
|
||||
.find(new Document(BasicMongoPersistentProperty.ID_FIELD_NAME, new Document("$in", ids))) //
|
||||
.into(new ArrayList<>());
|
||||
.into(new ArrayList<>(ids.size()));
|
||||
|
||||
return ids.stream() //
|
||||
.flatMap(id -> documentWithId(id, result)) //
|
||||
|
||||
@@ -418,7 +418,7 @@ abstract class GeoConverters {
|
||||
return null;
|
||||
}
|
||||
|
||||
List<Object> argument = new ArrayList<>();
|
||||
List<Object> argument = new ArrayList<>(2);
|
||||
|
||||
Shape shape = source.getShape();
|
||||
|
||||
@@ -438,7 +438,9 @@ abstract class GeoConverters {
|
||||
|
||||
} else if (shape instanceof Polygon) {
|
||||
|
||||
for (Point point : ((Polygon) shape).getPoints()) {
|
||||
List<Point> points = ((Polygon) shape).getPoints();
|
||||
argument = new ArrayList(points.size());
|
||||
for (Point point : points) {
|
||||
argument.add(toList(point));
|
||||
}
|
||||
|
||||
|
||||
@@ -1000,14 +1000,14 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
.getPointer();
|
||||
}).collect(Collectors.toList());
|
||||
|
||||
return writeCollectionInternal(targetCollection, TypeInformation.of(DocumentPointer.class), new ArrayList<>());
|
||||
return writeCollectionInternal(targetCollection, TypeInformation.of(DocumentPointer.class), new ArrayList<>(targetCollection.size()));
|
||||
}
|
||||
|
||||
if (property.hasExplicitWriteTarget()) {
|
||||
return writeCollectionInternal(collection, new FieldTypeInformation<>(property), new ArrayList<>());
|
||||
return writeCollectionInternal(collection, new FieldTypeInformation<>(property), new ArrayList<>(collection.size()));
|
||||
}
|
||||
|
||||
return writeCollectionInternal(collection, property.getTypeInformation(), new ArrayList<>());
|
||||
return writeCollectionInternal(collection, property.getTypeInformation(), new ArrayList<>(collection.size()));
|
||||
}
|
||||
|
||||
List<Object> dbList = new ArrayList<>(collection.size());
|
||||
@@ -1092,7 +1092,9 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
collection.add(getPotentiallyConvertedSimpleWrite(element,
|
||||
componentType != null ? componentType.getType() : Object.class));
|
||||
} else if (element instanceof Collection || elementType.isArray()) {
|
||||
collection.add(writeCollectionInternal(BsonUtils.asCollection(element), componentType, new ArrayList<>()));
|
||||
|
||||
Collection<?> objects = BsonUtils.asCollection(element);
|
||||
collection.add(writeCollectionInternal(objects, componentType, new ArrayList<>(objects.size())));
|
||||
} else {
|
||||
Document document = new Document();
|
||||
writeInternal(element, document, componentType);
|
||||
|
||||
@@ -373,7 +373,7 @@ public class QueryMapper {
|
||||
if (keyword.isOrOrNor() || (keyword.hasIterableValue() && !keyword.isGeometry())) {
|
||||
|
||||
Iterable<?> conditions = keyword.getValue();
|
||||
List<Object> newConditions = new ArrayList<>();
|
||||
List<Object> newConditions = conditions instanceof Collection ? new ArrayList<>(((Collection<?>) conditions).size()) : new ArrayList<>();
|
||||
|
||||
for (Object condition : conditions) {
|
||||
newConditions.add(isDocument(condition) ? getMappedObject((Document) condition, entity)
|
||||
|
||||
@@ -337,7 +337,7 @@ public class Criteria implements CriteriaDefinition {
|
||||
* @see <a href="https://docs.mongodb.com/manual/reference/operator/query/mod/">MongoDB Query operator: $mod</a>
|
||||
*/
|
||||
public Criteria mod(Number value, Number remainder) {
|
||||
List<Object> l = new ArrayList<Object>();
|
||||
List<Object> l = new ArrayList<Object>(2);
|
||||
l.add(value);
|
||||
l.add(remainder);
|
||||
criteria.put("$mod", l);
|
||||
|
||||
@@ -165,8 +165,10 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor {
|
||||
if (property.isAssociation()) {
|
||||
if (next.getClass().isArray() || next instanceof Iterable) {
|
||||
|
||||
List<DBRef> dbRefs = new ArrayList<DBRef>();
|
||||
for (Object element : asCollection(next)) {
|
||||
Collection<?> values = asCollection(next);
|
||||
|
||||
List<DBRef> dbRefs = new ArrayList<DBRef>(values.size());
|
||||
for (Object element : values) {
|
||||
dbRefs.add(writer.toDBRef(element, property));
|
||||
}
|
||||
|
||||
@@ -196,11 +198,14 @@ public class ConvertingParameterAccessor implements MongoParameterAccessor {
|
||||
|
||||
if (source instanceof Iterable) {
|
||||
|
||||
List<Object> result = new ArrayList<Object>();
|
||||
if(source instanceof Collection) {
|
||||
return new ArrayList<>((Collection<?>) source);
|
||||
}
|
||||
|
||||
List<Object> result = new ArrayList<>();
|
||||
for (Object element : (Iterable<?>) source) {
|
||||
result.add(element);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user