Skip to content

Commit 3adf1cb

Browse files
thcedclaude
andcommitted
perf: Cache Spec.basePath at construction (W1)
basePath() was previously recomputed via URI.create on every request through RequestPreparationFilter.stripBasePath. The result is spec-static, so promote it to a record component populated in Spec.from(...). Eliminates ~100 MB of URI allocations and ~26 CPU samples per JFR run; k6 throughput +2.6% (44.2k -> 45.3k rps), p95 -40 µs. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 9f42c2a commit 3adf1cb

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/main/java/com/retailsvc/http/spec/Spec.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public record Spec(
1616
List<Server> servers,
1717
List<Operation> operations,
1818
Map<String, Schema> componentSchemas,
19-
Map<String, Parameter> componentParameters) {
19+
Map<String, Parameter> componentParameters,
20+
String basePath) {
2021

2122
private static final String SCHEMA_KEY = "schema";
2223

@@ -32,14 +33,21 @@ public static Spec from(Map<String, Object> raw) {
3233
List<Operation> operations =
3334
parseOperations(
3435
(Map<String, Object>) raw.getOrDefault("paths", Map.of()), componentParameters);
35-
return new Spec(openapi, info, servers, operations, componentSchemas, componentParameters);
36+
return new Spec(
37+
openapi,
38+
info,
39+
servers,
40+
operations,
41+
componentSchemas,
42+
componentParameters,
43+
computeBasePath(servers));
3644
}
3745

38-
public String basePath() {
46+
private static String computeBasePath(List<Server> servers) {
3947
if (servers.isEmpty()) {
4048
throw new IllegalStateException("no servers declared");
4149
}
42-
return Optional.ofNullable(URI.create(servers.get(0).url()).getPath()).orElse("");
50+
return Optional.ofNullable(URI.create(servers.getFirst().url()).getPath()).orElse("");
4351
}
4452

4553
public Schema resolveSchema(String ref) {

src/test/java/com/retailsvc/http/internal/RequestPreparationFilterTest.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,13 @@ private HttpExchange exchange(String method, String path, byte[] body) {
4444

4545
private Spec specWith(Operation... ops) {
4646
return new Spec(
47-
"3.1.0", new Info("t", "1"), List.of(new Server("/")), List.of(ops), Map.of(), Map.of());
47+
"3.1.0",
48+
new Info("t", "1"),
49+
List.of(new Server("/")),
50+
List.of(ops),
51+
Map.of(),
52+
Map.of(),
53+
"");
4854
}
4955

5056
private Filter newFilter(Spec spec) {

0 commit comments

Comments
 (0)