From d19fd833a50c417b3b87ccba6ce0e3b4130d3a60 Mon Sep 17 00:00:00 2001 From: Harry Chan Date: Tue, 8 Oct 2024 21:45:25 +1100 Subject: [PATCH 1/2] Optimize helidon optionals Helidon docs says that `Optional` is expensive and don't use it unless you need to. In the generator, optionals are more of an implementation detail and null or the default value is returned. For better performance, this PR replaces optionals with contains / get as Helidon suggests. --- .../generator/helidon/nima/ControllerWriter.java | 6 +++++- .../helidon/nima/NimaPlatformAdapter.java | 16 ++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/ControllerWriter.java b/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/ControllerWriter.java index c2a39d2e..78f90f97 100644 --- a/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/ControllerWriter.java +++ b/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/ControllerWriter.java @@ -202,7 +202,11 @@ private void writeClassStart() { if (reader.isIncludeValidator()) { writer.append(" private String language(ServerRequest req) {").eol(); - writer.append(" return req.headers().first(HEADER_ACCEPT_LANGUAGE).orElse(null);").eol(); + writer.append(" var headers = req.headers();").eol(); + writer.append(" if (headers.contains(HEADER_ACCEPT_LANGUAGE)) {").eol(); + writer.append(" return headers.get(HEADER_ACCEPT_LANGUAGE).get();").eol(); + writer.append(" }").eol(); + writer.append(" return null;").eol(); writer.append(" }").eol().eol(); } } diff --git a/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/NimaPlatformAdapter.java b/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/NimaPlatformAdapter.java index 83790aef..c5c0cf9d 100644 --- a/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/NimaPlatformAdapter.java +++ b/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/NimaPlatformAdapter.java @@ -67,16 +67,16 @@ private void addRoleImports(List roles, ControllerReader controller) { @Override public void writeReadParameter(Append writer, ParamType paramType, String paramName) { switch (paramType) { - case PATHPARAM -> writer.append("pathParams.first(\"%s\").get()", paramName); + case PATHPARAM -> writer.append("pathParams.contains(\"%s\") ? pathParams.get(\"%s\") : null", paramName); - case QUERYPARAM -> writer.append("req.query().first(\"%s\").orElse(null)", paramName); + case QUERYPARAM -> writer.append("req.query().contains(\"%s\") ? req.query().get(\"%s\") : null", paramName); - case FORMPARAM -> writer.append("formParams.first(\"%s\").orElse(null)", paramName); + case FORMPARAM -> writer.append("formParams.contains(\"%s\") ? formParams.get(\"%s\") : null", paramName); case HEADER -> writer.append( "req.headers().value(HeaderNames.create(\"%s\")).orElse(null)", paramName); - case COOKIE -> writer.append("req.headers().cookies().first(\"%s\").orElse(null)", paramName); + case COOKIE -> writer.append("req.headers().cookies().contains(\"%s\") ? req.headers().cookies().get(\"%s\") : null", paramName); default -> writer.append("null // TODO req.%s().param(\"%s\")", paramType.type(), paramName); } @@ -86,19 +86,19 @@ public void writeReadParameter(Append writer, ParamType paramType, String paramN public void writeReadParameter(Append writer, ParamType paramType, String paramName, String paramDefault) { switch (paramType) { case PATHPARAM -> writer.append( - "pathParams.first(\"%s\").orElse(\"%s\")", paramName, paramDefault); + "pathParams.contains(\"%s\") ? pathParams.get(\"%s\") : \"%s\"", paramName, paramDefault); case QUERYPARAM -> writer.append( - "req.query().first(\"%s\").orElse(\"%s\")", paramName, paramDefault); + "req.query().contains(\"%s\") ? req.query().get(\"%s\") : \"%s\"", paramName, paramDefault); case FORMPARAM -> writer.append( - "formParams.first(\"%s\").orElse(\"%s\")", paramName, paramDefault); + "formParams.contains(\"%s\") ? formParams.get(\"%s\") : \"%s\"", paramName, paramDefault); case HEADER -> writer.append( "req.headers().value(Http.Header.create(\"%s\").orElse(\"%s\")", paramName, paramDefault); case COOKIE -> writer.append( - "req.headers().cookies().first(\"%s\").orElse(\"%s\")", paramName, paramDefault); + "req.headers().cookies().contains(\"%s\") ? req.headers().cookies().get(\"%s\") : \"%s\"", paramName, paramDefault); default -> writer.append("null // TODO req.%s().param(\"%s\")", paramType.type(), paramName); } From bfdaa2aa00c1abb70d830c70c49fa5dc9f76c102 Mon Sep 17 00:00:00 2001 From: Harry Chan Date: Tue, 8 Oct 2024 22:41:42 +1100 Subject: [PATCH 2/2] fix build --- .../helidon/nima/NimaPlatformAdapter.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/NimaPlatformAdapter.java b/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/NimaPlatformAdapter.java index c5c0cf9d..03d21189 100644 --- a/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/NimaPlatformAdapter.java +++ b/http-generator-helidon/src/main/java/io/avaje/http/generator/helidon/nima/NimaPlatformAdapter.java @@ -67,16 +67,16 @@ private void addRoleImports(List roles, ControllerReader controller) { @Override public void writeReadParameter(Append writer, ParamType paramType, String paramName) { switch (paramType) { - case PATHPARAM -> writer.append("pathParams.contains(\"%s\") ? pathParams.get(\"%s\") : null", paramName); + case PATHPARAM -> writer.append("pathParams.contains(\"%s\") ? pathParams.get(\"%s\") : null", paramName, paramName); - case QUERYPARAM -> writer.append("req.query().contains(\"%s\") ? req.query().get(\"%s\") : null", paramName); + case QUERYPARAM -> writer.append("req.query().contains(\"%s\") ? req.query().get(\"%s\") : null", paramName, paramName); - case FORMPARAM -> writer.append("formParams.contains(\"%s\") ? formParams.get(\"%s\") : null", paramName); + case FORMPARAM -> writer.append("formParams.contains(\"%s\") ? formParams.get(\"%s\") : null", paramName, paramName); case HEADER -> writer.append( "req.headers().value(HeaderNames.create(\"%s\")).orElse(null)", paramName); - case COOKIE -> writer.append("req.headers().cookies().contains(\"%s\") ? req.headers().cookies().get(\"%s\") : null", paramName); + case COOKIE -> writer.append("req.headers().cookies().contains(\"%s\") ? req.headers().cookies().get(\"%s\") : null", paramName, paramName); default -> writer.append("null // TODO req.%s().param(\"%s\")", paramType.type(), paramName); } @@ -86,19 +86,19 @@ public void writeReadParameter(Append writer, ParamType paramType, String paramN public void writeReadParameter(Append writer, ParamType paramType, String paramName, String paramDefault) { switch (paramType) { case PATHPARAM -> writer.append( - "pathParams.contains(\"%s\") ? pathParams.get(\"%s\") : \"%s\"", paramName, paramDefault); + "pathParams.contains(\"%s\") ? pathParams.get(\"%s\") : \"%s\"", paramName, paramName, paramDefault); case QUERYPARAM -> writer.append( - "req.query().contains(\"%s\") ? req.query().get(\"%s\") : \"%s\"", paramName, paramDefault); + "req.query().contains(\"%s\") ? req.query().get(\"%s\") : \"%s\"", paramName, paramName, paramDefault); case FORMPARAM -> writer.append( - "formParams.contains(\"%s\") ? formParams.get(\"%s\") : \"%s\"", paramName, paramDefault); + "formParams.contains(\"%s\") ? formParams.get(\"%s\") : \"%s\"", paramName, paramName, paramDefault); case HEADER -> writer.append( "req.headers().value(Http.Header.create(\"%s\").orElse(\"%s\")", paramName, paramDefault); case COOKIE -> writer.append( - "req.headers().cookies().contains(\"%s\") ? req.headers().cookies().get(\"%s\") : \"%s\"", paramName, paramDefault); + "req.headers().cookies().contains(\"%s\") ? req.headers().cookies().get(\"%s\") : \"%s\"", paramName, paramName, paramDefault); default -> writer.append("null // TODO req.%s().param(\"%s\")", paramType.type(), paramName); }