Skip to content

Conversation

roookeee
Copy link

@roookeee roookeee commented Feb 6, 2019

Currently the Series value for a HttpStatus is always evaluated which in turn costs an allocation of a Series array by using Series.values() which does a defensive copy.
I moved the Series value into HttpStatus and calculate it in the constructor as manually passing it for every constant seems unnecessary.
Furthermore I deleted a JavaDoc about an IllegalArgumentException which cannot occur for any valid HttpStatus. If need be I would volunteer to add a unit test so this doesn't fail in the future.

The method Series.valueOf(HttpStatus) could be removed / deprecated by this change but I don't think that's for me to decide :)

@pivotal-issuemaster
Copy link

@roookeee Please sign the Contributor License Agreement!

Click here to manually synchronize the status of this Pull Request.

See the FAQ for frequently asked questions.

@pivotal-issuemaster
Copy link

@roookeee Thank you for signing the Contributor License Agreement!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 6, 2019
@sdeleuze
Copy link
Contributor

sdeleuze commented Feb 7, 2019

Computing and storing the Series which is far from always used in the constructor leads to another kind of inefficiency. Couldn't we combine both approach by keep lazy computation of Series in series() but change private final Series series to private Series series and use that field to cache the result for subsequent invocations?

@sdeleuze sdeleuze self-assigned this Feb 7, 2019
@roookeee
Copy link
Author

roookeee commented Feb 7, 2019

We trade dynamic allocation and dynamic computation vs. static allocation and a one-time evaluation at class-loading-time. I for one think the static memory overhead by storing the value in the HttpStatus is negligible as there are < 50 different status-codes which equates to 50 x ~ 8 bytes = 400 byte static memory on 64-bit machines (give or take some for field-paddnig).

Caching the results in a field of HttpStatus seems like a pretty bad code-smell as enums are often compared to static value lists. Changing enums after their initialization is pretty surprising and introduces multiple issues in a multithreaded context like spring. We don't get these kind of issues with my approach.

Another approach would be to include the Series field directly in the HttpStatus constructor, we don't compute anything then. If you consider this to be a better alternative I would change this PR and include a unit-test that checks every HttpStatus Series to be correct (e.g. if its 500, it must be 5). This way we can avoid copy&paste errors when adding new statuses (e.g. new HttpStatus(900, Series.EIGHT))

I use a fluent error-handling class with makes heavy use of is5xx checks which suffers unnecessarily from the dynamic computation that is currently used to determine the Series for a HttpStatus.

@roookeee
Copy link
Author

Any update on this?

@sbrannen sbrannen changed the title Remove inefficiency in HttpStatus.getSeries() Remove inefficiency in HttpStatus.series() Sep 25, 2020
@sbrannen sbrannen added the in: web Issues in web modules (web, webmvc, webflux, websocket) label Sep 25, 2020
@sdeleuze sdeleuze added this to the 5.3 RC2 milestone Sep 25, 2020
@sdeleuze sdeleuze removed the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 25, 2020
@sdeleuze
Copy link
Contributor

@roookeee Could you please update the PR with your proposal?

Another approach would be to include the Series field directly in the HttpStatus constructor, we don't compute anything then. If you consider this to be a better alternative I would change this PR and include a unit-test that checks every HttpStatus Series to be correct (e.g. if its 500, it must be 5). This way we can avoid copy&paste errors when adding new statuses (e.g. new HttpStatus(900, Series.EIGHT))

@sdeleuze sdeleuze removed their assignment Sep 25, 2020
@roookeee
Copy link
Author

Alright, will do so in the near future

@roookeee
Copy link
Author

roookeee commented Sep 26, 2020

I have adjusted the PR to reflect your implementation wishes. In my opinion the other variant was more elegant, but I see that it's a subjective topic. I added a comment in the newly added test to justify it's surprising existence.

@sbrannen sbrannen added the type: enhancement A general enhancement label Sep 26, 2020
@sbrannen sbrannen self-assigned this Sep 26, 2020
@sbrannen sbrannen closed this in 97cc896 Sep 26, 2020
@sbrannen
Copy link
Member

This has been merged into master in 97cc896 and revised in ba94a12.

Thanks

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) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants