Skip to content

Commit 104ceb9

Browse files
authored
Java: Optimize HashSet Initialization (#21205)
* Optimize HashSet Initialization Noticed this while debugging - we can avoid wasting memory/cpu creating 16 buckets when we only need one or a few. * generate samples * use Arrays.asList
1 parent 0389a99 commit 104ceb9

File tree

239 files changed

+488
-1426
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

239 files changed

+488
-1426
lines changed

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/pojo.mustache

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -322,16 +322,20 @@ public class {{classname}} {{#parent}}extends {{{.}}} {{/parent}}{{#vendorExtens
322322

323323
static {
324324
// a set of all properties/fields (JSON key names)
325-
openapiFields = new HashSet<String>();
326-
{{#allVars}}
327-
openapiFields.add("{{baseName}}");
328-
{{/allVars}}
325+
{{#hasVars}}
326+
openapiFields = new HashSet<String>(Arrays.asList({{#allVars}}"{{baseName}}"{{^-last}}, {{/-last}}{{/allVars}}));
327+
{{/hasVars}}
328+
{{^hasVars}}
329+
openapiFields = new HashSet<String>(0);
330+
{{/hasVars}}
329331

330332
// a set of required properties/fields (JSON key names)
331-
openapiRequiredFields = new HashSet<String>();
332-
{{#requiredVars}}
333-
openapiRequiredFields.add("{{baseName}}");
334-
{{/requiredVars}}
333+
{{#hasRequired}}
334+
openapiRequiredFields = new HashSet<String>(Arrays.asList({{#requiredVars}}"{{baseName}}"{{^-last}}, {{/-last}}{{/requiredVars}}));
335+
{{/hasRequired}}
336+
{{^hasRequired}}
337+
openapiRequiredFields = new HashSet<String>(0);
338+
{{/hasRequired}}
335339
}
336340

337341
/**

samples/client/echo_api/java/okhttp-gson-user-defined-templates/modelCopy/Category.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ private String toIndentedString(Object o) {
7171

7272
static {
7373
// a set of all properties/fields (JSON key names)
74-
openapiFields = new HashSet<String>();
74+
openapiFields = new HashSet<String>(0);
7575

7676
// a set of required properties/fields (JSON key names)
77-
openapiRequiredFields = new HashSet<String>();
77+
openapiRequiredFields = new HashSet<String>(0);
7878
}
7979

8080
/**

samples/client/echo_api/java/okhttp-gson-user-defined-templates/modelCopy/ModelApiResponse.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ private String toIndentedString(Object o) {
7171

7272
static {
7373
// a set of all properties/fields (JSON key names)
74-
openapiFields = new HashSet<String>();
74+
openapiFields = new HashSet<String>(0);
7575

7676
// a set of required properties/fields (JSON key names)
77-
openapiRequiredFields = new HashSet<String>();
77+
openapiRequiredFields = new HashSet<String>(0);
7878
}
7979

8080
/**

samples/client/echo_api/java/okhttp-gson-user-defined-templates/modelCopy/Order.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ private String toIndentedString(Object o) {
7171

7272
static {
7373
// a set of all properties/fields (JSON key names)
74-
openapiFields = new HashSet<String>();
74+
openapiFields = new HashSet<String>(0);
7575

7676
// a set of required properties/fields (JSON key names)
77-
openapiRequiredFields = new HashSet<String>();
77+
openapiRequiredFields = new HashSet<String>(0);
7878
}
7979

8080
/**

samples/client/echo_api/java/okhttp-gson-user-defined-templates/modelCopy/Pet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ private String toIndentedString(Object o) {
7171

7272
static {
7373
// a set of all properties/fields (JSON key names)
74-
openapiFields = new HashSet<String>();
74+
openapiFields = new HashSet<String>(0);
7575

7676
// a set of required properties/fields (JSON key names)
77-
openapiRequiredFields = new HashSet<String>();
77+
openapiRequiredFields = new HashSet<String>(0);
7878
}
7979

8080
/**

samples/client/echo_api/java/okhttp-gson-user-defined-templates/modelCopy/Tag.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ private String toIndentedString(Object o) {
7171

7272
static {
7373
// a set of all properties/fields (JSON key names)
74-
openapiFields = new HashSet<String>();
74+
openapiFields = new HashSet<String>(0);
7575

7676
// a set of required properties/fields (JSON key names)
77-
openapiRequiredFields = new HashSet<String>();
77+
openapiRequiredFields = new HashSet<String>(0);
7878
}
7979

8080
/**

samples/client/echo_api/java/okhttp-gson-user-defined-templates/modelCopy/User.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,10 @@ private String toIndentedString(Object o) {
7171

7272
static {
7373
// a set of all properties/fields (JSON key names)
74-
openapiFields = new HashSet<String>();
74+
openapiFields = new HashSet<String>(0);
7575

7676
// a set of required properties/fields (JSON key names)
77-
openapiRequiredFields = new HashSet<String>();
77+
openapiRequiredFields = new HashSet<String>(0);
7878
}
7979

8080
/**

samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/model/Category.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,10 @@ private String toIndentedString(Object o) {
147147

148148
static {
149149
// a set of all properties/fields (JSON key names)
150-
openapiFields = new HashSet<String>();
151-
openapiFields.add("id");
152-
openapiFields.add("name");
150+
openapiFields = new HashSet<String>(Arrays.asList("id", "name"));
153151

154152
// a set of required properties/fields (JSON key names)
155-
openapiRequiredFields = new HashSet<String>();
153+
openapiRequiredFields = new HashSet<String>(0);
156154
}
157155

158156
/**

samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/model/ModelApiResponse.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,10 @@ private String toIndentedString(Object o) {
173173

174174
static {
175175
// a set of all properties/fields (JSON key names)
176-
openapiFields = new HashSet<String>();
177-
openapiFields.add("code");
178-
openapiFields.add("type");
179-
openapiFields.add("message");
176+
openapiFields = new HashSet<String>(Arrays.asList("code", "type", "message"));
180177

181178
// a set of required properties/fields (JSON key names)
182-
openapiRequiredFields = new HashSet<String>();
179+
openapiRequiredFields = new HashSet<String>(0);
183180
}
184181

185182
/**

samples/client/echo_api/java/okhttp-gson-user-defined-templates/src/main/java/org/openapitools/client/model/Order.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -306,16 +306,10 @@ private String toIndentedString(Object o) {
306306

307307
static {
308308
// a set of all properties/fields (JSON key names)
309-
openapiFields = new HashSet<String>();
310-
openapiFields.add("id");
311-
openapiFields.add("petId");
312-
openapiFields.add("quantity");
313-
openapiFields.add("shipDate");
314-
openapiFields.add("status");
315-
openapiFields.add("complete");
309+
openapiFields = new HashSet<String>(Arrays.asList("id", "petId", "quantity", "shipDate", "status", "complete"));
316310

317311
// a set of required properties/fields (JSON key names)
318-
openapiRequiredFields = new HashSet<String>();
312+
openapiRequiredFields = new HashSet<String>(0);
319313
}
320314

321315
/**

0 commit comments

Comments
 (0)