[CSharp-Netcore][Go] Optimize time taken to generate CSharp code (#10152)

* adds memoization to speed up csharp code generation

* takes care of review comments

* memoize adds to toModel for Go generator as well

Co-authored-by: Aanisha Mishra <aanisha.mishra05@gmail.com>
This commit is contained in:
Vikrant Balyan 2021-08-16 09:41:04 +05:30 committed by GitHub
parent 8eb31d0c7c
commit 849fec29b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 3 deletions

View File

@ -256,6 +256,9 @@ public class DefaultCodegen implements CodegenConfig {
// A cache to efficiently lookup a Schema instance based on the return value of `toModelName()`. // A cache to efficiently lookup a Schema instance based on the return value of `toModelName()`.
private Map<String, Schema> modelNameToSchemaCache; private Map<String, Schema> modelNameToSchemaCache;
// A cache to efficiently lookup schema `toModelName()` based on the schema Key
private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
@Override @Override
public List<CliOption> cliOptions() { public List<CliOption> cliOptions() {
return cliOptions; return cliOptions;
@ -2322,7 +2325,13 @@ public class DefaultCodegen implements CodegenConfig {
*/ */
@Override @Override
public String toModelName(final String name) { public String toModelName(final String name) {
return camelize(modelNamePrefix + "_" + name + "_" + modelNameSuffix); if (schemaKeyToModelNameCache.containsKey(name)) {
return schemaKeyToModelNameCache.get(name);
}
String camelizedName = camelize(modelNamePrefix + "_" + name + "_" + modelNameSuffix);
schemaKeyToModelNameCache.put(name, camelizedName);
return camelizedName;
} }
private static class NamedSchema { private static class NamedSchema {

View File

@ -90,6 +90,9 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
// special property keywords not allowed as these are the function names in the model files // special property keywords not allowed as these are the function names in the model files
protected Set<String> propertySpecialKeywords = new HashSet<>(Arrays.asList("ToString", "ToJson", "GetHashCode", "Equals", "ShouldSerializeToString")); protected Set<String> propertySpecialKeywords = new HashSet<>(Arrays.asList("ToString", "ToJson", "GetHashCode", "Equals", "ShouldSerializeToString"));
// A cache to efficiently lookup schema `toModelName()` based on the schema Key
private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
public AbstractCSharpCodegen() { public AbstractCSharpCodegen() {
super(); super();
@ -1035,6 +1038,13 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
if (importMapping.containsKey(name)) { if (importMapping.containsKey(name)) {
return importMapping.get(name); return importMapping.get(name);
} }
// memoization
String origName = name;
if (schemaKeyToModelNameCache.containsKey(origName)) {
return schemaKeyToModelNameCache.get(origName);
}
if (!StringUtils.isEmpty(modelNamePrefix)) { if (!StringUtils.isEmpty(modelNamePrefix)) {
name = modelNamePrefix + "_" + name; name = modelNamePrefix + "_" + name;
} }
@ -1058,9 +1068,12 @@ public abstract class AbstractCSharpCodegen extends DefaultCodegen implements Co
name = "model_" + name; // e.g. 200Response => Model200Response (after camelize) name = "model_" + name; // e.g. 200Response => Model200Response (after camelize)
} }
String camelizedName = camelize(name);
schemaKeyToModelNameCache.put(origName, camelizedName);
// camelize the model name // camelize the model name
// phone_number => PhoneNumber // phone_number => PhoneNumber
return camelize(name); return camelizedName;
} }
@Override @Override

View File

@ -49,6 +49,9 @@ public class GoClientCodegen extends AbstractGoCodegen {
protected boolean isGoSubmodule = false; protected boolean isGoSubmodule = false;
protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup
// A cache to efficiently lookup schema `toModelName()` based on the schema Key
private Map<String, String> schemaKeyToModelNameCache = new HashMap<>();
public GoClientCodegen() { public GoClientCodegen() {
super(); super();
@ -312,8 +315,14 @@ public class GoClientCodegen extends AbstractGoCodegen {
@Override @Override
public String toModelName(String name) { public String toModelName(String name) {
if (schemaKeyToModelNameCache.containsKey(name)) {
return schemaKeyToModelNameCache.get(name);
}
// underscoring would also lowercase the whole name, thus losing acronyms which are in capitals // underscoring would also lowercase the whole name, thus losing acronyms which are in capitals
return camelize(toModel(name, false)); String camelizedName = camelize(toModel(name, false));
schemaKeyToModelNameCache.put(name, camelizedName);
return camelizedName;
} }
public String escapeReservedWord(String name) { public String escapeReservedWord(String name) {