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

cache MediaType.toString #331

Merged
merged 1 commit into from
Mar 4, 2024

Conversation

fwbrasil
Copy link
Contributor

@fwbrasil fwbrasil commented Feb 29, 2024

Problem

On each request, Tapir uses the toString in MediaType to produce the header. In my benchmark, the instance of MediaType is always the same but the string has to be produced again on each request.

Solution

Make toString a lazy val so the value can be reused.

Notes

  • This is part of Reduce allocation rate tapir#3552
  • Are MediaType meant to be singletons? If yes, we could make toString a val to avoid the lazy val volatile read. It's likely a quite small overhead, though.

@adamw
Copy link
Member

adamw commented Feb 29, 2024

Are MediaType meant to be singletons? If yes, we could make toString a val to avoid the lazy val volatile read. It's likely a quite small overhead, though.

There's a number of pre-defined instances, but during parsing in server interpreters, a new instance is created. We could do pooling of the instances (so that these are true singletons - I think akka-http does it), but I have no idea if that would have any significant value. If in your tests lazy val removes this bottleneck, let's go with that.

@fwbrasil
Copy link
Contributor Author

Thanks for the quick review! Yes, lazy val is fine with me. The overhead of the volatile read is very small, especially on x86 due to TSO.

@adamw adamw merged commit 869b16c into softwaremill:master Mar 4, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants