rest: don't copy data when sending binary response #30321

pull romanz wants to merge 1 commits into bitcoin:master from romanz:http-string-view changing 3 files +17 −17
  1. romanz commented at 8:51 AM on June 22, 2024: contributor

    Also, change HTTPRequest::WriteReply to accept std::span.

  2. DrahtBot commented at 8:51 AM on June 22, 2024: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage

    For detailed information about the code coverage, see the test coverage report.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    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.

  3. DrahtBot added the label RPC/REST/ZMQ on Jun 22, 2024
  4. maflcko commented at 9:53 AM on June 22, 2024: member

    lgtm ACK 07770609afddaa1c730b431c207015f34b3a5f5f

    A benchmark would be nice (for fun), but this looks good either way.

  5. laanwj commented at 2:06 PM on June 22, 2024: member

    LGTM ACK 07770609afddaa1c730b431c207015f34b3a5f5f Nice, simple optimization.

  6. in src/httpserver.cpp:637 in 07770609af outdated
     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)
    


    laanwj commented at 2:07 PM on June 22, 2024:

    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.


    romanz commented at 4:48 AM on June 24, 2024:
  7. BrandonOdiwuor approved
  8. BrandonOdiwuor commented at 6:18 PM on June 22, 2024: contributor

    Code Review ACK 07770609afddaa1c730b431c207015f34b3a5f5f

  9. tdb3 approved
  10. tdb3 commented at 2:20 AM on June 23, 2024: contributor

    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:

    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).

  11. romanz renamed this:
    rest: don't copy block data when sending binary response
    rest: don't copy data when sending binary response
    on Jun 24, 2024
  12. romanz commented at 8:36 PM on June 24, 2024: contributor
  13. tdb3 approved
  14. tdb3 commented at 10:47 PM on June 24, 2024: contributor

    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.

    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
    
  15. DrahtBot requested review from maflcko on Jun 24, 2024
  16. DrahtBot requested review from laanwj on Jun 24, 2024
  17. DrahtBot requested review from BrandonOdiwuor on Jun 24, 2024
  18. stickies-v commented at 10:38 AM on June 25, 2024: contributor

    Concept ACK. Can you tidy up the fixup commit please?

  19. in src/httpserver.h:12 in 9c9375cf3b outdated
       8 | @@ -9,6 +9,8 @@
       9 |  #include <optional>
      10 |  #include <string>
      11 |  
      12 | +#include <span.h>
    


    stickies-v commented at 12:06 PM on June 25, 2024:

    nit: we typically include our own headers first, and include in httpserver.cpp is missing

    <details> <summary>git diff</summary>

    diff --git a/src/httpserver.cpp b/src/httpserver.cpp
    index adc6040d08..6ba8754485 100644
    --- a/src/httpserver.cpp
    +++ b/src/httpserver.cpp
    @@ -13,6 +13,7 @@
     #include <netbase.h>
     #include <node/interface_ui.h>
     #include <rpc/protocol.h> // For HTTP status codes
    +#include <span.h>
     #include <sync.h>
     #include <util/check.h>
     #include <util/signalinterrupt.h>
    diff --git a/src/httpserver.h b/src/httpserver.h
    index c193cf2a7f..ffba4864aa 100644
    --- a/src/httpserver.h
    +++ b/src/httpserver.h
    @@ -5,12 +5,12 @@
     #ifndef BITCOIN_HTTPSERVER_H
     #define BITCOIN_HTTPSERVER_H
     
    +#include <span.h>
    +
     #include <functional>
     #include <optional>
     #include <string>
     
    -#include <span.h>
    -
     namespace util {
     class SignalInterrupt;
     } // namespace util
    
    

    </details>


    romanz commented at 6:52 PM on June 25, 2024:

    Thanks - fixed in 24e7925abb.

  20. stickies-v commented at 12:57 PM on June 25, 2024: contributor

    Code LGTM 9c9375cf3b581c31f047b87a2c05507dc7b31804 - just needs squashing.

    nit: could use std::span to avoid merge conflict with #29119?

    <details> <summary>git diff</summary>

    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
    
    

    </details>

  21. laanwj commented at 1:12 PM on June 25, 2024: member

    nit: could use std::span to avoid merge conflict with #29119?

    i think the problem with prematurely porting PRs to std::span is that utility functions (at least ToBytes) haven't been ported to std::span.

  22. stickies-v commented at 1:18 PM on June 25, 2024: contributor

    i think the problem with prematurely porting PRs to std::span is that utility functions (at least ToBytes) haven't been ported to std::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)

  23. laanwj commented at 1:29 PM on June 25, 2024: member

    Oh, hadn't seen that diff, sorry, somehow expected it to be more involved.

  24. romanz force-pushed on Jun 25, 2024
  25. romanz requested review from stickies-v on Jun 25, 2024
  26. DrahtBot commented at 10:48 PM on June 25, 2024: contributor

    <!--85328a0da195eb286784d51f73fa0af9-->

    🚧 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.

    <sub>Debug: https://github.com/bitcoin/bitcoin/runs/26671061608</sub>

  27. DrahtBot added the label CI failed on Jun 25, 2024
  28. rest: don't copy data when sending binary response
    Also, change `HTTPRequest::WriteReply` to accept `std::span`.
    1556d21599
  29. romanz force-pushed on Jun 26, 2024
  30. DrahtBot removed the label CI failed on Jun 26, 2024
  31. laanwj commented at 8:48 AM on June 26, 2024: member

    re-ACK 1556d21599a250297d5f20e5249c970340ab08bc

  32. DrahtBot requested review from tdb3 on Jun 26, 2024
  33. stickies-v approved
  34. stickies-v commented at 10:08 AM on June 26, 2024: contributor

    ACK 1556d21599a250297d5f20e5249c970340ab08bc

  35. fanquake merged this on Jun 26, 2024
  36. fanquake closed this on Jun 26, 2024

  37. romanz deleted the branch on Jun 26, 2024
  38. romanz commented at 11:33 AM on June 26, 2024: contributor

    Many thanks!

  39. luke-jr referenced this in commit 8a42c1a519 on Aug 1, 2024
  40. bitcoin locked this on Sep 20, 2025

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me