Skip to content
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

Performance enhancement in execution of ResponseEntity.of() #25183

Closed
brett-walker opened this issue Jun 4, 2020 · 1 comment
Closed

Performance enhancement in execution of ResponseEntity.of() #25183

brett-walker opened this issue Jun 4, 2020 · 1 comment
Assignees
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Milestone

Comments

@brett-walker
Copy link

Hi Team,

I think I have found a very small performance enhancement when using ResponseEntity.of

Currently the code for the method is

public static <T> ResponseEntity<T> of(Optional<T> body) {
	Assert.notNull(body, "Body must not be null");
	return body.map(ResponseEntity::ok).orElse(notFound().build());
}

That is the NOT_FOUND Response Entity is always built, even if it is not required.

The small change would be

public static <T> ResponseEntity<T> of(Optional<T> body) {
	Assert.notNull(body, "Body must not be null");
	return body.map(ResponseEntity::ok).orElseGet(() -> notFound().build());
}

By using the orElseGet method, the construction of the NOT_FOUND Response Entity is delayed until it is required.

There maybe other considerations here, such as the construction of the lambda needs to be balanced against the construction of the NOT_FOUND Response Entity.

In your estimation, is a PR worthwhile for this small enhancement?

Cheers,
Brett

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jun 4, 2020
@jhoeller jhoeller self-assigned this Jun 6, 2020
@jhoeller jhoeller added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jun 6, 2020
@jhoeller jhoeller added this to the 5.2.7 milestone Jun 6, 2020
@jhoeller jhoeller added the for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x label Jun 6, 2020
@spring-projects-issues spring-projects-issues added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.1.x Marks an issue as a candidate for backport to 5.1.x labels Jun 6, 2020
@jhoeller
Copy link
Contributor

jhoeller commented Jun 6, 2020

Good point. I'll roll this into our upcoming 5.2.7 and 5.1.16 directly since we're about to wrap them up already. Thanks for offering to contribute a pull request, in any case, and thanks for raising this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: web Issues in web modules (web, webmvc, webflux, websocket) status: backported An issue that has been backported to maintenance branches type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants