-
Notifications
You must be signed in to change notification settings - Fork 38.7k
rest: don't copy data when sending binary response #30321
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
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. Code CoverageFor detailed information about the code coverage, see the test coverage report. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update. |
|
lgtm ACK 07770609afddaa1c730b431c207015f34b3a5f5f A benchmark would be nice (for fun), but this looks good either way. |
|
LGTM ACK 07770609afddaa1c730b431c207015f34b3a5f5f |
src/httpserver.cpp
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if a Span<byte> or string_view is best suited here. In principle we're dealing with binary data here, not necessarily a string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added 9c9375c with a few more fixes.
BrandonOdiwuor
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review ACK 07770609afddaa1c730b431c207015f34b3a5f5f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 07770609afddaa1c730b431c207015f34b3a5f5f
Simple and efficient. Looks good.
Tested with REST calls on two different nodes with signet block 201185 (one running PR 30321 and one running master (5383637 at the time)). Output from both branches matched (hash checked).
PR 30321:
curl -v http://127.0.0.1:38332/rest/block/0000005d9cd746eeae8078c9bdb40866e02ddde50680c4c2d9d396dfdb6d3351.bin --output pr30321.bin
sha256sum pr30321.bin
0f88a3cda3b44b2f0fc66006f5ac1349cc8e6a2e7a70f7d5f756355c61d58c76 pr30321.bin
master:
curl -v http://127.0.0.1:38332/rest/block/0000005d9cd746eeae8078c9bdb40866e02ddde50680c4c2d9d396dfdb6d3351.bin --output master.bin
sha256sum master.bin
0f88a3cda3b44b2f0fc66006f5ac1349cc8e6a2e7a70f7d5f756355c61d58c76 master.bin
Also took a quick look at adjacent code and at the other methods in HTTPRequest to see if there might be other spots benefitting from copy avoidance with string_view. Nothing popped out that wouldn't induce more significant refactoring (i.e. riskier).
|
Added 9c9375c with a few more fixes, following the suggestion above. |
tdb3
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
re ACK 9c9375cf3b581c31f047b87a2c05507dc7b31804
Re-ran the tests in #30321 (review)
Also performed similar PR branch / master (5383637) comparison tests with tx, headers, blockhashbyheight, and getutxos endpoints. All matched.
curl -v http://127.0.0.1:38332/rest/tx/7382c61e570cbc882a3d32b272318d353386981a475f58341fb4268971a6f1ec.bin
curl -v http://127.0.0.1:38332/rest/headers/0000005d9cd746eeae8078c9bdb40866e02ddde50680c4c2d9d396dfdb6d3351.bin
curl -v http://127.0.0.1:38332/rest/blockhashbyheight/201185.bin
curl -v http://127.0.0.1:38332/rest/getutxos/7382c61e570cbc882a3d32b272318d353386981a475f58341fb4268971a6f1ec-0.bin
|
Concept ACK. Can you tidy up the fixup commit please? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code LGTM 9c9375cf3b581c31f047b87a2c05507dc7b31804 - just needs squashing.
nit: could use std::span to avoid merge conflict with #29119?
git diff
diff --git a/src/httpserver.cpp b/src/httpserver.cpp
index adc6040d08..b6c6db8b35 100644
--- a/src/httpserver.cpp
+++ b/src/httpserver.cpp
@@ -26,6 +26,7 @@
#include <deque>
#include <memory>
#include <optional>
+#include <span>
#include <string>
#include <unordered_map>
@@ -634,7 +635,7 @@ void HTTPRequest::WriteHeader(const std::string& hdr, const std::string& value)
* Replies must be sent in the main loop in the main http thread,
* this cannot be done from worker threads.
*/
-void HTTPRequest::WriteReply(int nStatus, Span<const std::byte> reply)
+void HTTPRequest::WriteReply(int nStatus, std::span<const std::byte> reply)
{
assert(!replySent && req);
if (m_interrupt) {
diff --git a/src/httpserver.h b/src/httpserver.h
index c193cf2a7f..907ed1a72b 100644
--- a/src/httpserver.h
+++ b/src/httpserver.h
@@ -7,10 +7,9 @@
#include <functional>
#include <optional>
+#include <span>
#include <string>
-#include <span.h>
-
namespace util {
class SignalInterrupt;
} // namespace util
@@ -132,9 +131,9 @@ public:
*/
void WriteReply(int nStatus, std::string_view reply = "")
{
- WriteReply(nStatus, AsBytes(Span{reply}));
+ WriteReply(nStatus, std::as_bytes(std::span{reply}));
}
- void WriteReply(int nStatus, Span<const std::byte> reply);
+ void WriteReply(int nStatus, std::span<const std::byte> reply);
};
/** Get the query parameter value from request uri for a specified key, or std::nullopt if the key
i think the problem with prematurely porting PRs to |
I think it's fine to just use |
|
Oh, hadn't seen that diff, sorry, somehow expected it to be more involved. |
|
🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the Possibly this is due to a silent merge conflict (the changes in this pull request being Leave a comment here, if you need help tracking down a confusing failure. |
Also, change `HTTPRequest::WriteReply` to accept `std::span`.
|
re-ACK 1556d21 |
stickies-v
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ACK 1556d21
|
Many thanks! |
Also, change `HTTPRequest::WriteReply` to accept `std::span`. Github-Pull: bitcoin#30321 Rebased-From: 1556d21
Also, change
HTTPRequest::WriteReplyto acceptstd::span.