I just noticed looking at profiles of some reactor based benchmarks we run to measure Datadog tracer overhead that HttpStatus.resolve allocates an HttpStatus[] once per invocation, so once per response. In a very modest throughput benchmark (~600rps) this is allocating 1MB/s just to resolve the integer status code to the HttpStatus enum value. Screenshot 2021-04-21 at 22 35 38

I tracked the spring framework code down here.

This could be fixed by caching HttpStatus.values() in an array, which could be iterated over as many times as one likes without any further allocation.

Comment From: poutsma

I think we can do better than a cached array, and might as well start using a Map<Integer, HttpStatus>.

Comment From: richardstartin

Boxing HTTP status codes can be problematic because the common ones all fall outside the range of the Integer cache. We use this data structure for representing mappings between small, sparse finite sets of integers (such as status codes or ports) and arbitrary things, which works very well for tracing, and something similar may be applicable here.

Comment From: poutsma

Thanks you for help.

I am not sure if we want to introduce a new data structure into the framework to resolve this. I'll make a copy of the array instead, as originally suggested.

Comment From: richardstartin

That makes sense - we don't record any time spent in HttpStatus.resolve at all, it only shows up for the allocations.