Skip to content

Commit 3a57a59

Browse files
sbisciglaws-sdk-cpp-automation
authored andcommitted
avoid calculating cpp shape info more than once
1 parent c7f2c5b commit 3a57a59

File tree

7 files changed

+54
-44
lines changed

7 files changed

+54
-44
lines changed

tools/code-generation/generator/src/main/java/com/amazonaws/util/awsclientgenerator/domainmodels/codegeneration/cpp/CppViewHelper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ public static String computeTimestampFormatInXml(Shape shape) {
333333
public static Set<String> computeHeaderIncludes(String projectName, Shape shape) {
334334
Set<String> headers = new LinkedHashSet<>();
335335
Set<String> visited = new LinkedHashSet<>();
336+
if (shape.getMembers() == null) {
337+
return headers;
338+
}
336339
Queue<Shape> toVisit = shape.getMembers().values().stream().map(ShapeMember::getShape).collect(Collectors.toCollection(() -> new LinkedList<>()));
337340
boolean includeUtilityHeader = false;
338341
boolean includeMemoryHeader = false;
@@ -399,6 +402,11 @@ public static Set<String> computeHeaderIncludes(String projectName, Shape shape)
399402
public static Set<String> computeForwardDeclarations(Shape shape) {
400403
Set<String> forwardDeclarations = new LinkedHashSet<>();
401404
Set<String> visited = new LinkedHashSet<>();
405+
406+
if (shape.getMembers() == null) {
407+
return forwardDeclarations;
408+
}
409+
402410
Queue<Shape> toVisit = shape.getMembers().values().stream().map(ShapeMember::getShape).collect(Collectors.toCollection(() -> new LinkedList<>()));
403411

404412
while(!toVisit.isEmpty()) {
@@ -459,6 +467,9 @@ else if(shape.isBlob()) {
459467
public static Set<String> computeSourceIncludes(String projectName, Shape shape) {
460468
Set<String> headers = new LinkedHashSet<>();
461469
Set<String> visited = new LinkedHashSet<>();
470+
if (shape.getMembers() == null) {
471+
return headers;
472+
}
462473
Queue<Shape> toVisit = shape.getMembers().values().stream().map(ShapeMember::getShape).collect(Collectors.toCollection(() -> new LinkedList<>()));
463474

464475
while(!toVisit.isEmpty()) {

tools/code-generation/generator/src/main/java/com/amazonaws/util/awsclientgenerator/generators/cpp/CppClientGenerator.java

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.gson.JsonObject;
2525
import com.google.gson.JsonParser;
2626

27+
import org.apache.commons.lang3.tuple.Pair;
2728
import org.apache.velocity.Template;
2829
import org.apache.velocity.VelocityContext;
2930
import org.apache.velocity.app.VelocityEngine;
@@ -87,8 +88,11 @@ public SdkFileEntry[] generateSourceFiles(ServiceModel serviceModel) throws Exce
8788
addEventStreamInitialResponse(serviceModel);
8889
addRequestIdToResults(serviceModel);
8990
List<SdkFileEntry> fileList = new ArrayList<>();
90-
fileList.addAll(generateModelHeaderFiles(serviceModel));
91-
fileList.addAll(generateModelSourceFiles(serviceModel));
91+
final Map<String, CppShapeInformation> shapeInformationCache = serviceModel.getShapes().values().stream()
92+
.map(shape -> Pair.of(shape.getName(), new CppShapeInformation(shape, serviceModel)))
93+
.collect(Collectors.toMap(Pair::getKey, Pair::getValue));
94+
fileList.addAll(generateModelHeaderFiles(serviceModel, shapeInformationCache));
95+
fileList.addAll(generateModelSourceFiles(serviceModel, shapeInformationCache));
9296
fileList.add(generateClientHeaderFile(serviceModel));
9397
fileList.add(generateServiceClientModelInclude(serviceModel));
9498

@@ -265,16 +269,18 @@ protected final VelocityContext createContext(final ServiceModel serviceModel) {
265269
return context;
266270
}
267271

268-
protected List<SdkFileEntry> generateModelHeaderFiles(final ServiceModel serviceModel) throws Exception {
272+
protected List<SdkFileEntry> generateModelHeaderFiles(final ServiceModel serviceModel,
273+
final Map<String, CppShapeInformation> shapeInformationCache)
274+
{
269275
List<SdkFileEntry> sdkFileEntries = new ArrayList<>();
270276

271277
for (Map.Entry<String, Shape> shapeEntry : serviceModel.getShapes().entrySet()) {
272-
SdkFileEntry sdkFileEntry = generateModelHeaderFile(serviceModel, shapeEntry);
278+
SdkFileEntry sdkFileEntry = generateModelHeaderFile(serviceModel, shapeEntry, shapeInformationCache);
273279
if (sdkFileEntry != null) {
274280
sdkFileEntries.add(sdkFileEntry);
275281
}
276282

277-
sdkFileEntry = generateEventStreamHandlerHeaderFile(serviceModel, shapeEntry);
283+
sdkFileEntry = generateEventStreamHandlerHeaderFile(serviceModel, shapeEntry, shapeInformationCache);
278284
if (sdkFileEntry != null) {
279285
sdkFileEntries.add(sdkFileEntry);
280286
}
@@ -283,7 +289,7 @@ protected List<SdkFileEntry> generateModelHeaderFiles(final ServiceModel service
283289
return sdkFileEntries;
284290
}
285291

286-
protected SdkFileEntry generateModelHeaderFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry) throws Exception {
292+
protected SdkFileEntry generateModelHeaderFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry, Map<String, CppShapeInformation> shapeInformationCache) {
287293
Shape shape = shapeEntry.getValue();
288294
if (!(shape.isRequest() || shape.isEnum() || shape.hasEventPayloadMembers() && shape.hasBlobMembers())) {
289295
return null;
@@ -319,15 +325,15 @@ else if (shape.isEvent() && "blob".equals(shape.getEventPayloadType())) {
319325
shape.getMembers().entrySet().stream().filter(memberEntry -> memberEntry.getKey().equals(shape.getEventPayloadMemberName())).forEach(blobMemberEntry -> context.put("blobMember", blobMemberEntry));
320326
}
321327
context.put("shape", shape);
322-
context.put("typeInfo", new CppShapeInformation(shape, serviceModel));
328+
context.put("typeInfo", shapeInformationCache.get(shape.getName()));
323329
context.put("CppViewHelper", CppViewHelper.class);
324330

325331
String fileName = String.format("include/aws/%s/model/%s.h", serviceModel.getMetadata().getProjectName(),
326332
shapeEntry.getKey());
327333
return makeFile(template, context, fileName, true);
328334
}
329335

330-
protected SdkFileEntry generateEventStreamHandlerHeaderFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry) throws Exception {
336+
protected SdkFileEntry generateEventStreamHandlerHeaderFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry, Map<String, CppShapeInformation> shapeInformationCache) {
331337
Shape shape = shapeEntry.getValue();
332338
if (shape.isRequest()) {
333339
Template template = velocityEngine.getTemplate("/com/amazonaws/util/awsclientgenerator/velocity/cpp/RequestEventStreamHandlerHeader.vm", StandardCharsets.UTF_8.name());
@@ -346,7 +352,7 @@ protected SdkFileEntry generateEventStreamHandlerHeaderFile(ServiceModel service
346352
context.put("eventStreamShape", shapeMemberEntry.getValue().getShape());
347353
context.put("operation", op);
348354
context.put("shape", shape);
349-
context.put("typeInfo", new CppShapeInformation(shape, serviceModel));
355+
context.put("typeInfo", shapeInformationCache.get(shape.getName()));
350356
context.put("CppViewHelper", CppViewHelper.class);
351357

352358
String fileName = String.format("include/aws/%s/model/%sHandler.h", serviceModel.getMetadata().getProjectName(), key);
@@ -361,11 +367,12 @@ protected SdkFileEntry generateEventStreamHandlerHeaderFile(ServiceModel service
361367
return null;
362368
}
363369

364-
protected List<SdkFileEntry> generateModelSourceFiles(final ServiceModel serviceModel) {
370+
protected List<SdkFileEntry> generateModelSourceFiles(final ServiceModel serviceModel, final Map<String, CppShapeInformation> shapeInformationCache) {
365371

366372
return serviceModel.getShapes().entrySet().stream()
367373
.filter(entry -> Objects.nonNull(entry.getValue()))
368-
.map(entry -> Collections.unmodifiableList(Arrays.asList(generateModelSourceFile(serviceModel, entry), generateEventStreamHandlerSourceFile(serviceModel, entry))))
374+
.map(entry -> Collections.unmodifiableList(Arrays.asList(generateModelSourceFile(serviceModel, entry, shapeInformationCache),
375+
generateEventStreamHandlerSourceFile(serviceModel, entry, shapeInformationCache))))
369376
.flatMap(Collection::stream)
370377
.filter(Objects::nonNull)
371378
.collect(Collectors.toList());
@@ -391,7 +398,7 @@ protected SdkFileEntry generateServiceClientModelInclude(ServiceModel serviceMod
391398

392399
protected abstract List<SdkFileEntry> generateClientSourceFile(final List<ServiceModel> serviceModels) throws Exception;
393400

394-
protected SdkFileEntry generateModelSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry) {
401+
protected SdkFileEntry generateModelSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry, final Map<String, CppShapeInformation> shapeInformationCache) {
395402
Shape shape = shapeEntry.getValue();
396403
Template template;
397404
VelocityContext context = createContext(serviceModel);
@@ -402,7 +409,7 @@ protected SdkFileEntry generateModelSourceFile(ServiceModel serviceModel, Map.En
402409
context.put("enumModel", enumModel);
403410

404411
context.put("shape", shape);
405-
context.put("typeInfo", new CppShapeInformation(shape, serviceModel));
412+
context.put("typeInfo", shapeInformationCache.get(shape.getName()));
406413
context.put("CppViewHelper", CppViewHelper.class);
407414

408415
String fileName = String.format("source/model/%s.cpp", shapeEntry.getKey());
@@ -416,7 +423,7 @@ protected SdkFileEntry generateModelSourceFile(ServiceModel serviceModel, Map.En
416423
return null;
417424
}
418425

419-
protected SdkFileEntry generateEventStreamHandlerSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry) {
426+
protected SdkFileEntry generateEventStreamHandlerSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry, final Map<String, CppShapeInformation> shapeInformationCache) {
420427
Shape shape = shapeEntry.getValue();
421428
if (shape.isRequest()) {
422429
Template template = velocityEngine.getTemplate("/com/amazonaws/util/awsclientgenerator/velocity/cpp/xml/XmlRequestEventStreamHandlerSource.vm", StandardCharsets.UTF_8.name());
@@ -432,7 +439,7 @@ protected SdkFileEntry generateEventStreamHandlerSourceFile(ServiceModel service
432439
context.put("eventStreamShape", shapeMemberEntry.getValue().getShape());
433440
context.put("operation", op);
434441
context.put("shape", shape);
435-
context.put("typeInfo", new CppShapeInformation(shape, serviceModel));
442+
context.put("typeInfo", shapeInformationCache.get(shape.getName()));
436443
context.put("CppViewHelper", CppViewHelper.class);
437444

438445
String fileName = String.format("source/model/%sHandler.cpp", key);

tools/code-generation/generator/src/main/java/com/amazonaws/util/awsclientgenerator/generators/cpp/JsonCppClientGenerator.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import org.apache.velocity.VelocityContext;
1717

1818
import java.nio.charset.StandardCharsets;
19-
import java.util.ArrayList;
2019
import java.util.HashMap;
2120
import java.util.List;
2221
import java.util.Map;
@@ -44,7 +43,7 @@ protected SdkFileEntry generateErrorMarshallerHeaderFile(ServiceModel serviceMod
4443
}
4544

4645
@Override
47-
protected SdkFileEntry generateModelHeaderFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry) throws Exception {
46+
protected SdkFileEntry generateModelHeaderFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry, Map<String, CppShapeInformation> shapeInformationCache) {
4847

4948
Shape shape = shapeEntry.getValue();
5049
if (shape.isException() && !shape.isJsonModeledException())
@@ -58,7 +57,7 @@ protected SdkFileEntry generateModelHeaderFile(ServiceModel serviceModel, Map.En
5857

5958
//we only want to override json related stuff.
6059
if (shape.isRequest() || shape.isEnum() || shape.hasEventPayloadMembers() && shape.hasBlobMembers()) {
61-
return super.generateModelHeaderFile(serviceModel, shapeEntry);
60+
return super.generateModelHeaderFile(serviceModel, shapeEntry, shapeInformationCache);
6261
}
6362

6463
if (shape.isStructure() && shape.isReferenced()) {
@@ -89,7 +88,7 @@ protected SdkFileEntry generateModelHeaderFile(ServiceModel serviceModel, Map.En
8988
}
9089

9190
@Override
92-
protected SdkFileEntry generateModelSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry) {
91+
protected SdkFileEntry generateModelSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry, final Map<String, CppShapeInformation> shapeInformationCache) {
9392
Shape shape = shapeEntry.getValue();
9493
if (shape.isResult() && shape.hasEventStreamMembers())
9594
return null;
@@ -110,7 +109,7 @@ protected SdkFileEntry generateModelSourceFile(ServiceModel serviceModel, Map.En
110109

111110
if (shape.isEnum()) {
112111
// event-stream input shapes do their serialization via the encoder; So skip generating code for those.
113-
return super.generateModelSourceFile(serviceModel, shapeEntry);
112+
return super.generateModelSourceFile(serviceModel, shapeEntry, shapeInformationCache);
114113
}
115114

116115
if (shape.isStructure() && shape.isReferenced()) {
@@ -187,7 +186,7 @@ protected List<SdkFileEntry> generateClientSourceFile( List<ServiceModel> servic
187186
}
188187

189188
@Override
190-
protected SdkFileEntry generateEventStreamHandlerSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry) {
189+
protected SdkFileEntry generateEventStreamHandlerSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry, final Map<String, CppShapeInformation> shapeInformationCache) {
191190
Shape shape = shapeEntry.getValue();
192191
if (shape.isRequest()) {
193192
Template template = velocityEngine.getTemplate("/com/amazonaws/util/awsclientgenerator/velocity/cpp/json/JsonEventStreamHandlerSource.vm", StandardCharsets.UTF_8.name());
@@ -203,7 +202,7 @@ protected SdkFileEntry generateEventStreamHandlerSourceFile(ServiceModel service
203202
context.put("eventStreamShape", shapeMemberEntry.getValue().getShape());
204203
context.put("operation", op);
205204
context.put("shape", shape);
206-
context.put("typeInfo", new CppShapeInformation(shape, serviceModel));
205+
context.put("typeInfo", shapeInformationCache.get(shape.getName()));
207206
context.put("CppViewHelper", CppViewHelper.class);
208207

209208
String fileName = String.format("source/model/%sHandler.cpp", key);

tools/code-generation/generator/src/main/java/com/amazonaws/util/awsclientgenerator/generators/cpp/QueryCppClientGenerator.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import org.apache.velocity.VelocityContext;
1616

1717
import java.nio.charset.StandardCharsets;
18-
import java.util.ArrayList;
1918
import java.util.HashMap;
2019
import java.util.List;
2120
import java.util.Map;
@@ -88,14 +87,14 @@ public SdkFileEntry[] generateSourceFiles(ServiceModel serviceModel) throws Exce
8887
}
8988

9089
@Override
91-
protected SdkFileEntry generateModelHeaderFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry) throws Exception {
90+
protected SdkFileEntry generateModelHeaderFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry, Map<String, CppShapeInformation> shapeInformationCache) {
9291
Shape shape = shapeEntry.getValue();
9392
if (shape.isException() && !shape.isXmlModeledException())
9493
return null;
9594

9695
//we only want to handle results and internal structures. We don't want requests or enums.
9796
if (shape.isRequest() || shape.isEnum() || shape.hasEventPayloadMembers() && shape.hasBlobMembers()) {
98-
return super.generateModelHeaderFile(serviceModel, shapeEntry);
97+
return super.generateModelHeaderFile(serviceModel, shapeEntry, shapeInformationCache);
9998
}
10099

101100
if (shape.isStructure() && shape.isReferenced()) {
@@ -121,13 +120,13 @@ protected SdkFileEntry generateModelHeaderFile(ServiceModel serviceModel, Map.En
121120
}
122121

123122
@Override
124-
protected SdkFileEntry generateModelSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry) {
123+
protected SdkFileEntry generateModelSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry, final Map<String, CppShapeInformation> shapeInformationCache) {
125124
Shape shape = shapeEntry.getValue();
126125
if (shape.isException() && !shape.isXmlModeledException())
127126
return null;
128127

129128
if (shape.isEnum()) {
130-
return super.generateModelSourceFile(serviceModel, shapeEntry);
129+
return super.generateModelSourceFile(serviceModel, shapeEntry, shapeInformationCache);
131130
}
132131

133132
Template template = null;

tools/code-generation/generator/src/main/java/com/amazonaws/util/awsclientgenerator/generators/cpp/RestXmlCppClientGenerator.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@
1515
import org.apache.velocity.Template;
1616
import org.apache.velocity.VelocityContext;
1717

18-
import static com.amazonaws.util.awsclientgenerator.generators.cpp.CppClientGenerator.ResolverMapping;
19-
2018
import java.nio.charset.StandardCharsets;
21-
import java.util.ArrayList;
2219
import java.util.List;
2320
import java.util.Map;
2421
import java.util.Optional;
@@ -99,15 +96,15 @@ protected List<SdkFileEntry> generateClientSourceFile(final List<ServiceModel> s
9996
}
10097

10198
@Override
102-
protected SdkFileEntry generateModelHeaderFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry) throws Exception {
99+
protected SdkFileEntry generateModelHeaderFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry, Map<String, CppShapeInformation> shapeInformationCache) {
103100

104101
Shape shape = shapeEntry.getValue();
105102
if (shape.isException() && !shape.isXmlModeledException())
106103
return null;
107104

108105
//we only want to override json related stuff.
109106
if (shape.isRequest() || shape.isEnum() || shape.hasEventPayloadMembers() && shape.hasBlobMembers()) {
110-
return super.generateModelHeaderFile(serviceModel, shapeEntry);
107+
return super.generateModelHeaderFile(serviceModel, shapeEntry, shapeInformationCache);
111108
}
112109

113110
// Will not generate source code if it's a shape of event, with empty member.
@@ -141,13 +138,13 @@ else if (shape.isResult()) {
141138
}
142139

143140
@Override
144-
protected SdkFileEntry generateModelSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry) {
141+
protected SdkFileEntry generateModelSourceFile(ServiceModel serviceModel, Map.Entry<String, Shape> shapeEntry, final Map<String, CppShapeInformation> shapeInformationCache) {
145142
Shape shape = shapeEntry.getValue();
146143
if (shape.isException() && !shape.isXmlModeledException())
147144
return null;
148145

149146
if (shape.isEnum()) {
150-
return super.generateModelSourceFile(serviceModel, shapeEntry);
147+
return super.generateModelSourceFile(serviceModel, shapeEntry, shapeInformationCache);
151148
}
152149

153150
if (shape.isStructure() && shape.isReferenced() &&

0 commit comments

Comments
 (0)