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

Optimize HTTP messages with empty payload body #1479

Merged
merged 9 commits into from
Apr 14, 2021

Conversation

idelpivnitskiy
Copy link
Member

@idelpivnitskiy idelpivnitskiy commented Apr 13, 2021

Motivation:

In many cases users do not set payload body when they create an HTTP
request (like GET) or response (like, 204). We can optimize flush
strategy and HTTP/1.1 encoding in these cases.

Modifications:

  • Introduce PayloadInfo#isEmpty() flag that tells internal layers when
    an HTTP message contains payload body;
  • Use this flag to choose a flush strategy and set content-length header;
  • Adjust existing content-length tests to verify new use-cases with
    empty payload body;
  • Enhance existing FlushStrategyOnServerTest to verify empty responses
    flush on end;
  • Filter out empty payload body buffer when converting between types;
  • Add more tests to verify users can keep using different ways to set payload;

Behavior changes:

  • Users cannot write more data into a payload body Buffer of new
    request/response objects after types conversion:
HttpRequest request = streamingHttpClient().asClient().post(SVC_ECHO);
StreamingHttpRequest streamingRequest = request.toStreamingRequest();
// Too late to take the buffer from the original aggregated request object, modifications won't be visible:
Buffer payload = request.payloadBody();
payload.writeAscii(CONTENT);
assertResponse(makeRequest(streamingRequest), request.version(), OK, 0);
  • Streaming HTTP/1.1 requests and responses with empty payload body are
    encoded with content-length: 0 instead of transfer-encoding: chunked;

Result:

Less flushes + non-chunked HTTP/1.1 encoding when there is no payload body
results in better throughput (+30% RPS) for these use-cases, latency improves
by ~15%.

Motivation:

In many cases users do not set payload body when they create an HTTP
request (like `GET`) or response (like, 204). We can optimize flush
strategy and HTTP/1.1 encoding in these cases.

Modification:

- Introduce `PayloadInfo#isEmpty()` flag that tells internal layers when
an HTTP message contains payload body;
- Use this flag to choose flush strategy and set `content-length` header;
- Adjust existing `content-length` tests to verify new use-cases with
empty payload body;
- Enhance existing `FlushStrategyOnServerTest` to verify empty responses
flush on end;

Result:

Less flushes + non-chunked HTTP/1.1 encoding when there is no payload body
results in better performance for these use-cases.
@idelpivnitskiy idelpivnitskiy requested a review from bondolo April 13, 2021 21:37
Copy link
Contributor

@bondolo bondolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a quite worthwhile improvement!

* Returns {@code true} if and only if, the payload body {@link Publisher} was never assigned, changed, or modified,
* and therefore remains empty.
*
* @return {@code true} if and only if, the payload body {@link Publisher} was never assigned, changed, or modified
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would be result for an explicitly empty payload such as empty String or empty byte array?

I am thinking of the case were app has their own empty payload response and they use it as the payload for a response they intend to be empty.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, in order to support this use-case, we can not apply the optimization in the scenarios you highlighted, because users can expand it later. If in future we will agree to drop support for this way, we will be able to optimize any empty payload body.

However, if users set a read-only empty buffer, we can support it. Fixed: 69e962f

@idelpivnitskiy idelpivnitskiy merged commit 97b45c6 into apple:main Apr 14, 2021
@idelpivnitskiy idelpivnitskiy deleted the empty-pb branch April 14, 2021 16:59
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.

3 participants