Optimize: entrySet is faster than keySet + get to prevent N lookups (#10496)

* Optimize: replace keySet + N get calls with entrySet saving N calls to get method in a few places

* missed one performance optimization

* Rolling back a change that was dependent on Java 11
This commit is contained in:
Larry Diamond 2021-10-04 04:37:34 -04:00 committed by GitHub
parent 768c76ea33
commit 10b310d33f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 107 additions and 71 deletions

View File

@ -721,8 +721,9 @@ public class CodeGenMojo extends AbstractMojo {
getLog().warn("environmentVariables is deprecated and will be removed in version 5.1. Use globalProperties instead.");
}
for (String key : globalProperties.keySet()) {
String value = globalProperties.get(key);
for (Map.Entry<String, String> globalPropertiesEntry : globalProperties.entrySet()) {
String key = globalPropertiesEntry.getKey();
String value = globalPropertiesEntry.getValue();
if (value != null) {
configurator.addGlobalProperty(key, value);
}

View File

@ -49,6 +49,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.regex.Matcher;
@ -541,8 +542,9 @@ public class DefaultCodegen implements CodegenConfig {
}
// Let parent know about all its children
for (String name : allModels.keySet()) {
CodegenModel cm = allModels.get(name);
for (Map.Entry<String, CodegenModel> allModelsEntry : allModels.entrySet()) {
String name = allModelsEntry.getKey();
CodegenModel cm = allModelsEntry.getValue();
CodegenModel parent = allModels.get(cm.getParent());
// if a discriminator exists on the parent, don't add this child to the inheritance hierarchy
// TODO Determine what to do if the parent discriminator name == the grandparent discriminator name
@ -3950,8 +3952,9 @@ public class DefaultCodegen implements CodegenConfig {
if (operation.getResponses() != null && !operation.getResponses().isEmpty()) {
ApiResponse methodResponse = findMethodResponse(operation.getResponses());
for (String key : operation.getResponses().keySet()) {
ApiResponse response = operation.getResponses().get(key);
for (Map.Entry<String, ApiResponse> operationGetResponsesEntry : operation.getResponses().entrySet()) {
String key = operationGetResponsesEntry.getKey();
ApiResponse response = operationGetResponsesEntry.getValue();
addProducesInfo(response, op);
CodegenResponse r = fromResponse(key, response);
if (r.baseType != null &&
@ -5110,14 +5113,14 @@ public class DefaultCodegen implements CodegenConfig {
}
// loop through list to update property name with toVarName
Set<String> renamedMandatory = new TreeSet<String>();
Set<String> renamedMandatory = new ConcurrentSkipListSet<String>();
Iterator<String> mandatoryIterator = m.mandatory.iterator();
while (mandatoryIterator.hasNext()) {
renamedMandatory.add(toVarName(mandatoryIterator.next()));
}
m.mandatory = renamedMandatory;
Set<String> renamedAllMandatory = new TreeSet<String>();
Set<String> renamedAllMandatory = new ConcurrentSkipListSet<String>();
Iterator<String> allMandatoryIterator = m.allMandatory.iterator();
while (allMandatoryIterator.hasNext()) {
renamedAllMandatory.add(toVarName(allMandatoryIterator.next()));
@ -6059,7 +6062,7 @@ public class DefaultCodegen implements CodegenConfig {
return null;
}
Set<String> produces = new TreeSet<String>();
Set<String> produces = new ConcurrentSkipListSet<String>();
for (ApiResponse r : operation.getResponses().values()) {
ApiResponse response = ModelUtils.getReferencedApiResponse(openAPI, r);

View File

@ -62,6 +62,7 @@ import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -335,8 +336,9 @@ public class DefaultGenerator implements Generator {
private void generateModelTests(List<File> files, Map<String, Object> models, String modelName) throws IOException {
// to generate model test files
for (String templateName : config.modelTestTemplateFiles().keySet()) {
String suffix = config.modelTestTemplateFiles().get(templateName);
for (Map.Entry<String, String> configModelTestTemplateFilesEntry : config.modelTestTemplateFiles().entrySet()) {
String templateName = configModelTestTemplateFilesEntry.getKey();
String suffix = configModelTestTemplateFilesEntry.getValue();
String filename = config.modelTestFileFolder() + File.separator + config.toModelTestFilename(modelName) + suffix;
if (generateModelTests) {
@ -1055,8 +1057,9 @@ public class DefaultGenerator implements Generator {
if(paths == null) {
return ops;
}
for (String resourcePath : paths.keySet()) {
PathItem path = paths.get(resourcePath);
for (Map.Entry<String, PathItem> pathsEntry : paths.entrySet()) {
String resourcePath = pathsEntry.getKey();
PathItem path = pathsEntry.getValue();
processOperation(resourcePath, "get", path.getGet(), ops, path);
processOperation(resourcePath, "head", path.getHead(), ops, path);
processOperation(resourcePath, "put", path.getPut(), ops, path);
@ -1199,7 +1202,7 @@ public class DefaultGenerator implements Generator {
operations.put("operations", objs);
operations.put("package", config.apiPackage());
Set<String> allImports = new TreeSet<>();
Set<String> allImports = new ConcurrentSkipListSet<>();
for (CodegenOperation op : ops) {
allImports.addAll(op.imports);
}
@ -1267,8 +1270,9 @@ public class DefaultGenerator implements Generator {
objs.put("package", config.modelPackage());
List<Object> models = new ArrayList<>();
Set<String> allImports = new LinkedHashSet<>();
for (String key : definitions.keySet()) {
Schema schema = definitions.get(key);
for (Map.Entry<String, Schema> definitionsEntry : definitions.entrySet()) {
String key = definitionsEntry.getKey();
Schema schema = definitionsEntry.getValue();
if (schema == null)
throw new RuntimeException("schema cannot be null in processModels");
CodegenModel cm = config.fromModel(key, schema);
@ -1282,7 +1286,7 @@ public class DefaultGenerator implements Generator {
allImports.addAll(cm.imports);
}
objs.put("models", models);
Set<String> importSet = new TreeSet<>();
Set<String> importSet = new ConcurrentSkipListSet<>();
for (String nextImport : allImports) {
String mapping = config.importMapping().get(nextImport);
if (mapping == null) {

View File

@ -79,8 +79,9 @@ public class InlineModelResolver {
return;
}
for (String pathname : paths.keySet()) {
PathItem path = paths.get(pathname);
for (Map.Entry<String, PathItem> pathsEntry : paths.entrySet()) {
String pathname = pathsEntry.getKey();
PathItem path = pathsEntry.getValue();
List<Operation> operations = new ArrayList<>(path.readOperations());
// Include callback operation as well
@ -263,8 +264,9 @@ public class InlineModelResolver {
return;
}
for (String key : responses.keySet()) {
ApiResponse response = responses.get(key);
for (Map.Entry<String, ApiResponse> responsesEntry : responses.entrySet()) {
String key = responsesEntry.getKey();
ApiResponse response = responsesEntry.getValue();
if (ModelUtils.getSchemaFromResponse(response) == null) {
continue;
}
@ -561,8 +563,9 @@ public class InlineModelResolver {
}
Map<String, Schema> propsToUpdate = new HashMap<String, Schema>();
Map<String, Schema> modelsToAdd = new HashMap<String, Schema>();
for (String key : properties.keySet()) {
Schema property = properties.get(key);
for (Map.Entry<String, Schema> propertiesEntry : properties.entrySet()) {
String key = propertiesEntry.getKey();
Schema property = propertiesEntry.getValue();
if (property instanceof ObjectSchema && ((ObjectSchema) property).getProperties() != null
&& ((ObjectSchema) property).getProperties().size() > 0) {
ObjectSchema op = (ObjectSchema) property;

View File

@ -81,8 +81,9 @@ public class XmlExampleGenerator {
// TODO: map objects will not enter this block
Map<String, Schema> properties = schema.getProperties();
if (properties != null && !properties.isEmpty()) {
for (String pName : properties.keySet()) {
Schema property = properties.get(pName);
for (Map.Entry<String, Schema> propertiesEntry : properties.entrySet()) {
String pName = propertiesEntry.getKey();
Schema property = propertiesEntry.getValue();
if (property != null && property.getXml() != null && property.getXml().getAttribute() != null && property.getXml().getAttribute()) {
attributes.put(pName, property);
} else {
@ -93,14 +94,16 @@ public class XmlExampleGenerator {
sb.append(indent(indent)).append(TAG_START);
sb.append(name);
for (String pName : attributes.keySet()) {
Schema s = attributes.get(pName);
for (Map.Entry<String, Schema> attributesEntry : attributes.entrySet()) {
String pName = attributesEntry.getKey();
Schema s = attributesEntry.getValue();
sb.append(" ").append(pName).append("=").append(quote(toXml(null, s, 0, selfPath)));
}
sb.append(CLOSE_TAG);
sb.append(NEWLINE);
for (String pName : elements.keySet()) {
Schema s = elements.get(pName);
for (Map.Entry<String, Schema> elementsEntry : elements.entrySet()) {
String pName = elementsEntry.getKey();
Schema s = elementsEntry.getValue();
final String asXml = toXml(pName, s, indent + 1, selfPath);
if (StringUtils.isEmpty(asXml)) {
continue;

View File

@ -40,6 +40,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.regex.Pattern;
import java.util.stream.Stream;
@ -1338,7 +1339,7 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
List<CodegenOperation> operationList = (List<CodegenOperation>) operations.get("operation");
for (CodegenOperation op : operationList) {
Collection<String> operationImports = new TreeSet<String>();
Collection<String> operationImports = new ConcurrentSkipListSet<String>();
for (CodegenParameter p : op.allParams) {
if (importMapping.containsKey(p.dataType)) {
operationImports.add(importMapping.get(p.dataType));
@ -1356,8 +1357,9 @@ public abstract class AbstractJavaCodegen extends DefaultCodegen implements Code
return;
}
if (openAPI.getPaths() != null) {
for (String pathname : openAPI.getPaths().keySet()) {
PathItem path = openAPI.getPaths().get(pathname);
for (Map.Entry<String, PathItem> openAPIGetPathsEntry : openAPI.getPaths().entrySet()) {
String pathname = openAPIGetPathsEntry.getKey();
PathItem path = openAPIGetPathsEntry.getValue();
if (path.readOperations() == null) {
continue;
}

View File

@ -142,8 +142,9 @@ public abstract class AbstractJavaJAXRSServerCodegen extends AbstractJavaCodegen
}
if (openAPI.getPaths() != null) {
for (String pathname : openAPI.getPaths().keySet()) {
PathItem path = openAPI.getPaths().get(pathname);
for (Map.Entry<String, PathItem> openAPIGetPathsEntry : openAPI.getPaths().entrySet()) {
String pathname = openAPIGetPathsEntry.getKey();
PathItem path = openAPIGetPathsEntry.getValue();
if (path.readOperations() != null) {
for (Operation operation : path.readOperations()) {
if (operation.getTags() != null) {

View File

@ -332,7 +332,7 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
PathItem path = paths.get(pathname);
// Fix path parameters to be in snake_case
if (pathname.contains("{")) {
String fixedPath = new String();
String fixedPath = "";
for (String token : pathname.substring(1).split("/")) {
if (token.startsWith("{")) {
String snake_case_token = "{" + this.toParamName(token.substring(1, token.length() - 1)) + "}";
@ -353,8 +353,9 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
}
Map<HttpMethod, Operation> operationMap = path.readOperationsMap();
if (operationMap != null) {
for (HttpMethod method : operationMap.keySet()) {
Operation operation = operationMap.get(method);
for (Map.Entry<HttpMethod, Operation> operationMapEntry : operationMap.entrySet()) {
HttpMethod method = operationMapEntry.getKey();
Operation operation = operationMapEntry.getValue();
String tag = "default";
if (operation.getTags() != null && operation.getTags().size() > 0) {
tag = operation.getTags().get(0);
@ -424,8 +425,9 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
Components components = openAPI.getComponents();
if (components != null && components.getSecuritySchemes() != null) {
Map<String, SecurityScheme> securitySchemes = components.getSecuritySchemes();
for (String securityName : securitySchemes.keySet()) {
SecurityScheme securityScheme = securitySchemes.get(securityName);
for (Map.Entry<String, SecurityScheme> securitySchemesEntry : securitySchemes.entrySet()) {
String securityName = securitySchemesEntry.getKey();
SecurityScheme securityScheme = securitySchemesEntry.getValue();
String baseFunctionName = controllerPackage + ".security_controller_.";
switch (securityScheme.getType()) {
case APIKEY:
@ -512,8 +514,9 @@ public abstract class AbstractPythonConnexionServerCodegen extends AbstractPytho
Map<HttpMethod, Operation> operationMap = path.readOperationsMap();
if (operationMap != null) {
for (HttpMethod method : operationMap.keySet()) {
Operation operation = operationMap.get(method);
for (Map.Entry<HttpMethod, Operation> operationMapEntry : operationMap.entrySet()) {
HttpMethod method = operationMapEntry.getKey();
Operation operation = operationMapEntry.getValue();
if (operation.getParameters() != null) {
for (Parameter parameter : operation.getParameters()) {
Map<String, Object> parameterExtensions = parameter.getExtensions();

View File

@ -32,6 +32,7 @@ import java.io.File;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Map;
public class JMeterClientCodegen extends DefaultCodegen implements CodegenConfig {
@ -149,8 +150,9 @@ public class JMeterClientCodegen extends DefaultCodegen implements CodegenConfig
@Override
public void preprocessOpenAPI(OpenAPI openAPI) {
if (openAPI != null && openAPI.getPaths() != null) {
for (String pathname : openAPI.getPaths().keySet()) {
PathItem path = openAPI.getPaths().get(pathname);
for (Map.Entry<String, PathItem> openAPIGetPathsEntry : openAPI.getPaths().entrySet()) {
String pathname = openAPIGetPathsEntry.getKey();
PathItem path = openAPIGetPathsEntry.getValue();
if (path.readOperations() != null) {
for (Operation operation : path.readOperations()) {
String pathWithDollars = pathname.replaceAll("\\{", "\\$\\{");

View File

@ -543,8 +543,9 @@ public class JavaPKMSTServerCodegen extends AbstractJavaCodegen {
this.additionalProperties.put("serverPort", URLPathUtils.getPort(url, 8080));
if (openAPI.getPaths() != null) {
for (String pathname : openAPI.getPaths().keySet()) {
PathItem path = openAPI.getPaths().get(pathname);
for (Map.Entry<String, PathItem> openAPIGetPathsEntry : openAPI.getPaths().entrySet()) {
String pathname = openAPIGetPathsEntry.getKey();
PathItem path = openAPIGetPathsEntry.getValue();
if (path.readOperations() != null) {
for (Operation operation : path.readOperations()) {
if (operation.getTags() != null) {

View File

@ -360,12 +360,14 @@ public class NodeJSExpressServerCodegen extends DefaultCodegen implements Codege
// need vendor extensions
Paths paths = openAPI.getPaths();
if (paths != null) {
for (String pathname : paths.keySet()) {
PathItem path = paths.get(pathname);
for (Map.Entry<String, PathItem> pathsEntry : paths.entrySet()) {
String pathname = pathsEntry.getKey();
PathItem path = pathsEntry.getValue();
Map<HttpMethod, Operation> operationMap = path.readOperationsMap();
if (operationMap != null) {
for (HttpMethod method : operationMap.keySet()) {
Operation operation = operationMap.get(method);
for (Map.Entry<HttpMethod, Operation> operationMapEntry : operationMap.entrySet()) {
HttpMethod method = operationMapEntry.getKey();
Operation operation = operationMapEntry.getValue();
String tag = "default";
if (operation.getTags() != null && operation.getTags().size() > 0) {
tag = toApiName(operation.getTags().get(0));

View File

@ -300,8 +300,9 @@ public class OCamlClientCodegen extends DefaultCodegen implements CodegenConfig
@SuppressWarnings("unchecked")
private void collectEnumSchemas(String parentName, Map<String, Schema> schemas) {
for (String sName : schemas.keySet()) {
Schema schema = schemas.get(sName);
for (Map.Entry<String, Schema> schemasEntry : schemas.entrySet()) {
String sName = schemasEntry.getKey();
Schema schema = schemasEntry.getValue();
collectEnumSchemas(parentName, sName, schema);
@ -339,8 +340,9 @@ public class OCamlClientCodegen extends DefaultCodegen implements CodegenConfig
}
}
if (operation.getResponses() != null) {
for (String s : operation.getResponses().keySet()) {
ApiResponse apiResponse = operation.getResponses().get(s);
for (Map.Entry<String, ApiResponse> operationGetResponsesEntry : operation.getResponses().entrySet()) {
String s = operationGetResponsesEntry.getKey();
ApiResponse apiResponse = operationGetResponsesEntry.getValue();
if (apiResponse.getContent() != null) {
Content content = apiResponse.getContent();
for (String p : content.keySet()) {
@ -349,8 +351,9 @@ public class OCamlClientCodegen extends DefaultCodegen implements CodegenConfig
}
if (apiResponse.getHeaders() != null) {
Map<String, Header> headers = apiResponse.getHeaders();
for (String h : headers.keySet()) {
Header header = headers.get(h);
for (Map.Entry<String, Header> headersEntry : headers.entrySet()) {
String h = headersEntry.getKey();
Header header = headersEntry.getValue();
collectEnumSchemas(h, header.getSchema());
}
}
@ -404,8 +407,9 @@ public class OCamlClientCodegen extends DefaultCodegen implements CodegenConfig
Paths paths = openAPI.getPaths();
if (paths != null && !paths.isEmpty()) {
for (String path : paths.keySet()) {
PathItem item = paths.get(path);
for (Map.Entry<String, PathItem> pathsEntry : paths.entrySet()) {
String path = pathsEntry.getKey();
PathItem item = pathsEntry.getValue();
collectEnumSchemas(item.getGet());
collectEnumSchemas(item.getPost());
collectEnumSchemas(item.getPut());

View File

@ -201,12 +201,14 @@ public class PhpDataTransferClientCodegen extends AbstractPhpCodegen {
protected void generateParameterSchemas(OpenAPI openAPI) {
Map<String, PathItem> paths = openAPI.getPaths();
if (paths != null) {
for (String pathname : paths.keySet()) {
PathItem path = paths.get(pathname);
for (Map.Entry<String, PathItem> pathsEntry : paths.entrySet()) {
String pathname = pathsEntry.getKey();
PathItem path = pathsEntry.getValue();
Map<HttpMethod, Operation> operationMap = path.readOperationsMap();
if (operationMap != null) {
for (HttpMethod method : operationMap.keySet()) {
Operation operation = operationMap.get(method);
for (Map.Entry<HttpMethod, Operation> operationMapEntry : operationMap.entrySet()) {
HttpMethod method = operationMapEntry.getKey();
Operation operation = operationMapEntry.getValue();
Map<String, Schema> propertySchemas = new HashMap<>();
if (operation == null || operation.getParameters() == null) {
continue;
@ -424,8 +426,9 @@ public class PhpDataTransferClientCodegen extends AbstractPhpCodegen {
protected void quoteMediaTypes(OpenAPI openAPI) {
Map<String, PathItem> paths = openAPI.getPaths();
if (paths != null) {
for (String pathname : paths.keySet()) {
PathItem path = paths.get(pathname);
for (Map.Entry<String, PathItem> pathsEntry : paths.entrySet()) {
String pathname = pathsEntry.getKey();
PathItem path = pathsEntry.getValue();
List<Operation> operations = path.readOperations();
if (operations != null) {
for (Operation operation : operations) {

View File

@ -197,12 +197,14 @@ public class PhpMezzioPathHandlerServerCodegen extends AbstractPhpCodegen {
protected void generateParameterSchemas(OpenAPI openAPI) {
Map<String, PathItem> paths = openAPI.getPaths();
if (paths != null) {
for (String pathname : paths.keySet()) {
PathItem path = paths.get(pathname);
for (Map.Entry<String, PathItem> pathsEntry : paths.entrySet()) {
String pathname = pathsEntry.getKey();
PathItem path = pathsEntry.getValue();
Map<HttpMethod, Operation> operationMap = path.readOperationsMap();
if (operationMap != null) {
for (HttpMethod method : operationMap.keySet()) {
Operation operation = operationMap.get(method);
for (Map.Entry<HttpMethod, Operation> operationMapEntry : operationMap.entrySet()) {
HttpMethod method = operationMapEntry.getKey();
Operation operation = operationMapEntry.getValue();
Map<String, Schema> propertySchemas = new HashMap<>();
if (operation == null || operation.getParameters() == null) {
continue;

View File

@ -251,8 +251,9 @@ public class ScalaGatlingCodegen extends AbstractScalaCodegen implements Codegen
*/
@Override
public void preprocessOpenAPI(OpenAPI openAPI) {
for (String pathname : openAPI.getPaths().keySet()) {
PathItem path = openAPI.getPaths().get(pathname);
for (Map.Entry<String, PathItem> openAPIGetPathsEntry : openAPI.getPaths().entrySet()) {
String pathname = openAPIGetPathsEntry.getKey();
PathItem path = openAPIGetPathsEntry.getValue();
if (path.readOperations() == null) {
continue;
}

View File

@ -556,8 +556,9 @@ public class SpringCodegen extends AbstractJavaCodegen
}
if (openAPI.getPaths() != null) {
for (String pathname : openAPI.getPaths().keySet()) {
PathItem path = openAPI.getPaths().get(pathname);
for (Map.Entry<String, PathItem> openAPIGetPathsEntry : openAPI.getPaths().entrySet()) {
String pathname = openAPIGetPathsEntry.getKey();
PathItem path = openAPIGetPathsEntry.getValue();
if (path.readOperations() != null) {
for (Operation operation : path.readOperations()) {
if (operation.getTags() != null) {