最佳性能:void 返回类型与非 void 返回类型的方法?

huangapple go评论75阅读模式
英文:

Best performance for method void vs non void return type?

问题

有一个用于插入/更新的网络服务。我需要返回响应实体,但并非总是需要。总是会有返回值,但用户不一定总是需要使用。因此,是不是更好地编写另一个返回响应实体的服务,或者只有一个服务就足够了?

这个插入/更新服务在集成项目中使用。因此,它用于大约 500,000个或更多实体,这就是我关心性能的原因。

英文:

There is a web service which is for insert/update. I need to return the response entity, but not always. There is always return value, but user don't need to use always. So that, is it better to write another service with returning the response entity or just one service is enough?

This insert/update service is used in integration project. So it is used for like 500k or more entity, thats why I care the performances.

答案1

得分: 1

我相信针对问题的评论(迄今为止共有3条)是关于Java中的内部服务代码,即关于Spring @Controller与Spring @Service之间的调用。

但问题说这是一个web服务,这意味着实际上是在询问有关HTTP POST(或PUT)请求的情况,在这种情况下,并不总是有东西要发送回去。

即使Spring Controller方法是void,也会发送HTTP响应,只是使用204 No Content代替通常的200 OK + 载荷。在性能方面,你永远看不到任何差异,因为用于格式化/发送带有数据的有效负载的任何时间都微不足道,与其他所有操作相比。

请注意,插入的适当响应是201 Created

如果你不想让客户端等待,你可能应该实现对请求的后台处理,例如通过使用@Async,这样HTTP请求就会立即获得响应,即使服务器仍在处理它。在这种情况下,响应应该是202 Accepted

英文:

I believe the comments to the question (3 so far) are talking about internal service code in Java, i.e. about the call between a Spring @Controller and a Spring @Service.

But question said this is a web service, which means it's actually asking about an HTTP POST (or PUT) request, where there isn't always anything to send back.

Even if the Spring Controller method is void, an HTTP response is sent back, just with 204 No Content instead of the usual 200 OK + payload. There is no performance difference that you'll ever see, because any time spent formatting/sending a payload with data is microscopic compared to everything else going on.

Note that the appropriate response to an insert is 201 Created.

If you don't want the client to wait, you should probably implement background processing of the request, e.g. by using @Async, so the HTTP request gets an immediate response, even though the server is still working on it. In that case, the response should be 202 Accepted.

huangapple
  • 本文由 发表于 2020年9月15日 15:49:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63897391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定