-
Notifications
You must be signed in to change notification settings - Fork 243
S3 tree optimizations #2913
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
S3 tree optimizations #2913
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8a23dbf
Improve BDD sifting (2x speed, more reduction)
mtdowling 0aaea90
Implement rules engine ITE fn and S3 tree transform
mtdowling cffa45f
Run type checking on BDD so type() works
mtdowling 3f2c363
Improve some loops
mtdowling 789c130
Attempt to fix the windows build
mtdowling 72ffbb5
Optimize rules further
mtdowling d228501
Reduce SSA noise
mtdowling 21cce54
Refactor S3 tree rewriter into composable transforms
mtdowling afb092f
Address PR feedback on gradle and test case
mtdowling 99e402a
Add BDD coverage testing and unref condition tx
mtdowling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.changes/next-release/feature-1c36b965bf2f51d85aa0171be6206ae2029137c4.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "feature", | ||
| "description": "Implement rules engine ITE fn and S3 tree transform", | ||
| "pull_requests": [ | ||
| "[#2903](https://github.com/smithy-lang/smithy/pull/2903)" | ||
| ] | ||
| } |
7 changes: 7 additions & 0 deletions
7
.changes/next-release/feature-5e4b03b74b1054ac2632fcae533d727eace1d26e.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "type": "feature", | ||
| "description": "Improve BDD sifting (2x speed, more reduction)", | ||
| "pull_requests": [ | ||
| "[#2890](https://github.com/smithy-lang/smithy/pull/2890)" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,8 @@ pluginManagement { | |
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
| rootProject.name = "smithy" | ||
|
|
||
| include(":smithy-aws-iam-traits") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...ints/src/it/java/software/amazon/smithy/rulesengine/aws/language/functions/S3BddTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package software.amazon.smithy.rulesengine.aws.language.functions; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.List; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.node.Node; | ||
| import software.amazon.smithy.model.shapes.ModelSerializer; | ||
| import software.amazon.smithy.model.shapes.ServiceShape; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
| import software.amazon.smithy.rulesengine.aws.s3.S3TreeRewriter; | ||
| import software.amazon.smithy.rulesengine.language.EndpointRuleSet; | ||
| import software.amazon.smithy.rulesengine.language.evaluation.TestEvaluator; | ||
| import software.amazon.smithy.rulesengine.logic.bdd.CostOptimization; | ||
| import software.amazon.smithy.rulesengine.logic.bdd.SiftingOptimization; | ||
| import software.amazon.smithy.rulesengine.logic.cfg.Cfg; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointBddTrait; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointTestCase; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointTestsTrait; | ||
|
|
||
| class S3BddTest { | ||
| private static final ShapeId S3_SERVICE_ID = ShapeId.from("com.amazonaws.s3#AmazonS3"); | ||
| private static Model model; | ||
| private static ServiceShape s3Service; | ||
| private static EndpointRuleSet originalRules; | ||
| private static EndpointRuleSet rules; | ||
| private static List<EndpointTestCase> testCases; | ||
|
|
||
| @BeforeAll | ||
| static void loadS3Model() { | ||
| model = Model.assembler() | ||
| .discoverModels() | ||
| .assemble() | ||
| .unwrap(); | ||
|
|
||
| s3Service = model.expectShape(S3_SERVICE_ID, ServiceShape.class); | ||
| originalRules = s3Service.expectTrait(EndpointRuleSetTrait.class).getEndpointRuleSet(); | ||
| rules = S3TreeRewriter.transform(originalRules); | ||
| testCases = s3Service.expectTrait(EndpointTestsTrait.class).getTestCases(); | ||
| } | ||
|
|
||
| @Test | ||
| void compileToBddWithOptimizations() { | ||
| // Verify transforms preserve semantics by running all test cases | ||
| assertFalse(testCases.isEmpty(), "S3 model should have endpoint test cases"); | ||
| for (EndpointTestCase testCase : testCases) { | ||
| TestEvaluator.evaluate(rules, testCase); | ||
| } | ||
|
|
||
| // Build CFG and compile to BDD | ||
| Cfg cfg = Cfg.from(rules); | ||
| EndpointBddTrait trait = EndpointBddTrait.from(cfg); | ||
|
|
||
| StringBuilder sb = new StringBuilder(); | ||
| sb.append("\n=== BDD STATS ===\n"); | ||
| sb.append("Conditions: ").append(trait.getConditions().size()).append("\n"); | ||
| sb.append("Results: ").append(trait.getResults().size()).append("\n"); | ||
| sb.append("Initial BDD nodes: ").append(trait.getBdd().getNodeCount()).append("\n"); | ||
|
|
||
| // Apply sifting optimization | ||
| SiftingOptimization sifting = SiftingOptimization.builder().cfg(cfg).build(); | ||
| EndpointBddTrait siftedTrait = sifting.apply(trait); | ||
| sb.append("After sifting - nodes: ").append(siftedTrait.getBdd().getNodeCount()).append("\n"); | ||
|
|
||
| // Apply cost optimization | ||
| CostOptimization cost = CostOptimization.builder().cfg(cfg).build(); | ||
| EndpointBddTrait optimizedTrait = cost.apply(siftedTrait); | ||
| sb.append("After cost opt - nodes: ").append(optimizedTrait.getBdd().getNodeCount()).append("\n"); | ||
|
|
||
| // Print conditions for analysis | ||
| sb.append("\n=== CONDITIONS ===\n"); | ||
| for (int i = 0; i < trait.getConditions().size(); i++) { | ||
| sb.append(i).append(": ").append(trait.getConditions().get(i)).append("\n"); | ||
| } | ||
|
|
||
| // Print results (endpoints) for analysis | ||
| sb.append("\n=== RESULTS ===\n"); | ||
| for (int i = 0; i < trait.getResults().size(); i++) { | ||
| sb.append(i).append(": ").append(trait.getResults().get(i)).append("\n"); | ||
| } | ||
|
|
||
| System.out.println(sb); | ||
|
|
||
| // Write model with BDD trait to build directory | ||
| writeModelWithBddTrait(optimizedTrait); | ||
| } | ||
|
|
||
| private void writeModelWithBddTrait(EndpointBddTrait bddTrait) { | ||
| String buildDir = System.getProperty("buildDir"); | ||
| if (buildDir == null) { | ||
| System.out.println("buildDir system property not set, skipping model output"); | ||
| return; | ||
| } | ||
|
|
||
| // Create updated service with BDD trait instead of RuleSet trait | ||
| ServiceShape updatedService = s3Service.toBuilder() | ||
| .removeTrait(EndpointRuleSetTrait.ID) | ||
| .addTrait(bddTrait) | ||
| .build(); | ||
|
|
||
| // Build updated model | ||
| Model updatedModel = model.toBuilder() | ||
| .removeShape(s3Service.getId()) | ||
| .addShape(updatedService) | ||
| .build(); | ||
|
|
||
| // Serialize to JSON | ||
| ModelSerializer serializer = ModelSerializer.builder().build(); | ||
| String json = Node.prettyPrintJson(serializer.serialize(updatedModel)); | ||
|
|
||
| // Write to build directory | ||
| Path outputPath = Paths.get(buildDir, "s3-bdd-model.json"); | ||
| try { | ||
| Path parentDir = outputPath.getParent(); | ||
| if (parentDir != null) { | ||
| Files.createDirectories(parentDir); | ||
| } | ||
| Files.writeString(outputPath, json); | ||
| System.out.println("Wrote S3 BDD model to: " + outputPath); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException("Failed to write S3 BDD model", e); | ||
| } | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
...it/java/software/amazon/smithy/rulesengine/aws/language/functions/S3TreeRewriterTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package software.amazon.smithy.rulesengine.aws.language.functions; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
|
|
||
| import java.util.List; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
| import software.amazon.smithy.model.Model; | ||
| import software.amazon.smithy.model.shapes.ServiceShape; | ||
| import software.amazon.smithy.model.shapes.ShapeId; | ||
| import software.amazon.smithy.rulesengine.aws.s3.S3TreeRewriter; | ||
| import software.amazon.smithy.rulesengine.language.EndpointRuleSet; | ||
| import software.amazon.smithy.rulesengine.language.evaluation.TestEvaluator; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointRuleSetTrait; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointTestCase; | ||
| import software.amazon.smithy.rulesengine.traits.EndpointTestsTrait; | ||
|
|
||
| /** | ||
| * Runs the endpoint test cases against the transformed S3 model. We're fixed to a specific version for this test, | ||
| * but could periodically bump the version if needed. | ||
| */ | ||
| class S3TreeRewriterTest { | ||
| private static final ShapeId S3_SERVICE_ID = ShapeId.from("com.amazonaws.s3#AmazonS3"); | ||
|
|
||
| private static EndpointRuleSet originalRules; | ||
| private static List<EndpointTestCase> testCases; | ||
|
|
||
| @BeforeAll | ||
| static void loadS3Model() { | ||
| Model model = Model.assembler() | ||
| .discoverModels() | ||
| .assemble() | ||
| .unwrap(); | ||
|
|
||
| ServiceShape s3Service = model.expectShape(S3_SERVICE_ID, ServiceShape.class); | ||
| originalRules = s3Service.expectTrait(EndpointRuleSetTrait.class).getEndpointRuleSet(); | ||
| testCases = s3Service.expectTrait(EndpointTestsTrait.class).getTestCases(); | ||
| } | ||
|
|
||
| @Test | ||
| void transformPreservesEndpointTestSemantics() { | ||
| assertFalse(testCases.isEmpty(), "S3 model should have endpoint test cases"); | ||
|
|
||
| EndpointRuleSet transformed = S3TreeRewriter.transform(originalRules); | ||
| for (EndpointTestCase testCase : testCases) { | ||
| TestEvaluator.evaluate(transformed, testCase); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.