HTTPRequest::WriteReply
to accept std::span
.
HTTPRequest::WriteReply
to accept std::span
.
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
For detailed information about the code coverage, see the test coverage report.
See the guideline for information on the review process.
Type | Reviewers |
---|---|
ACK | laanwj, stickies-v |
Stale ACK | maflcko, BrandonOdiwuor, tdb3 |
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.
633@@ -634,7 +634,7 @@ void HTTPRequest::WriteHeader(const std::string& hdr, const std::string& value)
634 * Replies must be sent in the main loop in the main http thread,
635 * this cannot be done from worker threads.
636 */
637-void HTTPRequest::WriteReply(int nStatus, const std::string& strReply)
638+void HTTPRequest::WriteReply(int nStatus, const std::string_view strReply)
Span<byte>
or string_view is best suited here. In principle we’re dealing with binary data here, not necessarily a string.
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 (538363738e9e30813cf3e76ca4f71c1aaff349e7 at the time)). Output from both branches matched (hash checked).
PR 30321:
0curl -v http://127.0.0.1:38332/rest/block/0000005d9cd746eeae8078c9bdb40866e02ddde50680c4c2d9d396dfdb6d3351.bin --output pr30321.bin
1sha256sum pr30321.bin
20f88a3cda3b44b2f0fc66006f5ac1349cc8e6a2e7a70f7d5f756355c61d58c76 pr30321.bin
master:
0curl -v http://127.0.0.1:38332/rest/block/0000005d9cd746eeae8078c9bdb40866e02ddde50680c4c2d9d396dfdb6d3351.bin --output master.bin
1sha256sum master.bin
20f88a3cda3b44b2f0fc66006f5ac1349cc8e6a2e7a70f7d5f756355c61d58c76 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).
re ACK 9c9375cf3b581c31f047b87a2c05507dc7b31804
Re-ran the tests in #30321#pullrequestreview-2133999354
Also performed similar PR branch / master (538363738e9e30813cf3e76ca4f71c1aaff349e7) comparison tests with tx
, headers
, blockhashbyheight
, and getutxos
endpoints. All matched.
0curl -v http://127.0.0.1:38332/rest/tx/7382c61e570cbc882a3d32b272318d353386981a475f58341fb4268971a6f1ec.bin
1curl -v http://127.0.0.1:38332/rest/headers/0000005d9cd746eeae8078c9bdb40866e02ddde50680c4c2d9d396dfdb6d3351.bin
2curl -v http://127.0.0.1:38332/rest/blockhashbyheight/201185.bin
3curl -v http://127.0.0.1:38332/rest/getutxos/7382c61e570cbc882a3d32b272318d353386981a475f58341fb4268971a6f1ec-0.bin
8@@ -9,6 +9,8 @@
9 #include <optional>
10 #include <string>
11
12+#include <span.h>
nit: we typically include our own headers first, and include in httpserver.cpp
is missing
0diff --git a/src/httpserver.cpp b/src/httpserver.cpp
1index adc6040d08..6ba8754485 100644
2--- a/src/httpserver.cpp
3+++ b/src/httpserver.cpp
4@@ -13,6 +13,7 @@
5 #include <netbase.h>
6 #include <node/interface_ui.h>
7 #include <rpc/protocol.h> // For HTTP status codes
8+#include <span.h>
9 #include <sync.h>
10 #include <util/check.h>
11 #include <util/signalinterrupt.h>
12diff --git a/src/httpserver.h b/src/httpserver.h
13index c193cf2a7f..ffba4864aa 100644
14--- a/src/httpserver.h
15+++ b/src/httpserver.h
16@@ -5,12 +5,12 @@
17 #ifndef BITCOIN_HTTPSERVER_H
18 #define BITCOIN_HTTPSERVER_H
19
20+#include <span.h>
21+
22 #include <functional>
23 #include <optional>
24 #include <string>
25
26-#include <span.h>
27-
28 namespace util {
29 class SignalInterrupt;
30 } // namespace util
Code LGTM 9c9375cf3b581c31f047b87a2c05507dc7b31804 - just needs squashing.
nit: could use std::span
to avoid merge conflict with #29119?
0diff --git a/src/httpserver.cpp b/src/httpserver.cpp
1index adc6040d08..b6c6db8b35 100644
2--- a/src/httpserver.cpp
3+++ b/src/httpserver.cpp
4@@ -26,6 +26,7 @@
5 #include <deque>
6 #include <memory>
7 #include <optional>
8+#include <span>
9 #include <string>
10 #include <unordered_map>
11
12@@ -634,7 +635,7 @@ void HTTPRequest::WriteHeader(const std::string& hdr, const std::string& value)
13 * Replies must be sent in the main loop in the main http thread,
14 * this cannot be done from worker threads.
15 */
16-void HTTPRequest::WriteReply(int nStatus, Span<const std::byte> reply)
17+void HTTPRequest::WriteReply(int nStatus, std::span<const std::byte> reply)
18 {
19 assert(!replySent && req);
20 if (m_interrupt) {
21diff --git a/src/httpserver.h b/src/httpserver.h
22index c193cf2a7f..907ed1a72b 100644
23--- a/src/httpserver.h
24+++ b/src/httpserver.h
25@@ -7,10 +7,9 @@
26
27 #include <functional>
28 #include <optional>
29+#include <span>
30 #include <string>
31
32-#include <span.h>
33-
34 namespace util {
35 class SignalInterrupt;
36 } // namespace util
37@@ -132,9 +131,9 @@ public:
38 */
39 void WriteReply(int nStatus, std::string_view reply = "")
40 {
41- WriteReply(nStatus, AsBytes(Span{reply}));
42+ WriteReply(nStatus, std::as_bytes(std::span{reply}));
43 }
44- void WriteReply(int nStatus, Span<const std::byte> reply);
45+ void WriteReply(int nStatus, std::span<const std::byte> reply);
46 };
47
48 /** 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
std::span
is that utility functions (at leastToBytes
) haven’t been ported tostd::span
.
I think it’s fine to just use std::as_bytes
(as done in my diff)? No other utilities needed here. Anyway, it’s a nit - either is fine for me. (And I’m not 100% sure if std::as_bytes
is supported on all compilers we use)
🚧 At least one of the CI tasks failed. Make sure to run all tests locally, according to the documentation.
Possibly this is due to a silent merge conflict (the changes in this pull request being incompatible with the current code in the target branch). If so, make sure to rebase on the latest commit of the target branch.
Leave a comment here, if you need help tracking down a confusing failure.
Also, change `HTTPRequest::WriteReply` to accept `std::span`.