Skip to content

Commit

Permalink
Optimize helidon optionals - io.helidon.common.mapper.OptionalValue (#…
Browse files Browse the repository at this point in the history
…505)

* 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.

* fix build

---------

Co-authored-by: Josiah Noel <[email protected]>
  • Loading branch information
re-thc and SentryMan authored Oct 9, 2024
1 parent 99aae32 commit 1c27559
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,16 @@ private void addRoleImports(List<String> 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, 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, paramName);

case FORMPARAM -> writer.append("formParams.first(\"%s\").orElse(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().first(\"%s\").orElse(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);
}
Expand All @@ -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, paramName, paramDefault);

case QUERYPARAM -> writer.append(
"req.query().first(\"%s\").orElse(\"%s\")", paramName, paramDefault);
"req.query().contains(\"%s\") ? req.query().get(\"%s\") : \"%s\"", paramName, paramName, paramDefault);

case FORMPARAM -> writer.append(
"formParams.first(\"%s\").orElse(\"%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().first(\"%s\").orElse(\"%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);
}
Expand Down

0 comments on commit 1c27559

Please sign in to comment.