|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +nav-class: dark |
| 4 | +categories: ruben |
| 5 | +title: "Moving Boost forward: Asio, coroutines, and maybe even modules" |
| 6 | +author-id: ruben |
| 7 | +author-name: Rubén Pérez Hidalgo |
| 8 | +--- |
| 9 | + |
| 10 | +It's been an exciting quarter over here. Both old-but-refreshed and brand new projects |
| 11 | +are flourishing in the Boost ecosystem, making today a really exciting moment to contribute to it. |
| 12 | + |
| 13 | +## using std::cpp 2025: a tale of coroutines, timeouts and cancellation in Asio |
| 14 | + |
| 15 | +I had the pleasure of speaking at [using std::cpp 2025](https://eventos.uc3m.es/119554/detail/using-std-cpp-2025.html), |
| 16 | +the biggest C++ conference in Spain. I even had Bjarne Stroustrup himself among the audience! |
| 17 | + |
| 18 | +The talk was focused on how to write simple but effective |
| 19 | +asynchronous Asio code. Asio is almost two decades old, yet it keeps improving. |
| 20 | +Since Boost 1.86, code like this is legal: |
| 21 | + |
| 22 | +```cpp |
| 23 | +// Write a message over a socket, and set a timeout to the operation |
| 24 | +co_await asio::async_write(sock, asio::buffer(buff), asio::cancel_after(20s)); |
| 25 | +``` |
| 26 | +
|
| 27 | +My talk addressed the mechanisms that power this kind of code, together with many |
| 28 | +tips gathered by making mistakes while working on Boost.MySQL and related projects. |
| 29 | +The slides and code snippets I used can be found [here](https://github.com/anarthal/usingstdcpp-2025), |
| 30 | +and a recording of the talk will be available soon. |
| 31 | +
|
| 32 | +I've also updated the [servertech chat project](https://github.com/anarthal/servertech-chat), |
| 33 | +a chat application with a server powered by Boost libraries. It now uses most of the features |
| 34 | +that I talked about during the talk, including timeouts and C++20 coroutines. |
| 35 | +
|
| 36 | +Since the project was using `asio::yield_context`, I wrote a small benchmark and found |
| 37 | +out that C++20 coroutines were ~10% faster. This might not be the case for all projects, though. |
| 38 | +
|
| 39 | +## C++20 modules and Boost |
| 40 | +
|
| 41 | +I've led an initiative to natively support C++20 modules in Boost. The ultimate goal is |
| 42 | +to enable the user write code like: |
| 43 | +
|
| 44 | +```cpp |
| 45 | +import boost.mp11; |
| 46 | +
|
| 47 | +using L1 = boost::mp11::mp_list<int, float, int, float>; |
| 48 | +``` |
| 49 | + |
| 50 | +The point of the prototype is to be as complete as possible. |
| 51 | +I firmly believe that just writing the module units (i.e. the `boost_mp11.cppm` file) |
| 52 | +is far from enough. This is essentially shipping untested code to the user. |
| 53 | +Additionally, building modules is not trivial, specially when there are |
| 54 | +complex dependency chains. For this reason, the prototype also includes: |
| 55 | + |
| 56 | +- CMake files to build and install the modules, compatible with today's Boost CMake infrastructure. |
| 57 | +- Running the library's entire test suite with modules enabled. |
| 58 | +- CI scripts to automate running these tests. |
| 59 | + |
| 60 | +I've fully migrated Boost.Mp11 (as a representative of header-only libraries) and |
| 61 | +Boost.Charconv (representing compiled ones). I've found a myriad of problems, |
| 62 | +including compiler bugs, but the result is functional. Compile times wins |
| 63 | +are really significant - building mp11's testsuite with modules took only |
| 64 | +a fourth of the time it takes with headers. |
| 65 | + |
| 66 | +We haven't merged any of this yet, since it relies on CMake's experimental |
| 67 | +features, and there are still many compiler bugs to be fixed. |
| 68 | + |
| 69 | +I've summarized my findings in [this article](https://anarthal.github.io/cppblog/modules3). |
| 70 | + |
| 71 | +While working in Charconv, I found some problems unrelated to modules |
| 72 | +and helped the author fix them. |
| 73 | + |
| 74 | +## Boost.MySQL benchmarks |
| 75 | + |
| 76 | +When you use Boost.MySQL, it's logical to ask yourself |
| 77 | +"is this much slower than the official drivers?". Spoiler alert: |
| 78 | +it's as fast as libmysqlclient and libmariadb, and even faster under |
| 79 | +some of the benchmarks. |
| 80 | + |
| 81 | +I didn't have the data to answer this question until recently, |
| 82 | +since there are no standard benchmarks to measure this. I've designed |
| 83 | +four benchmarks that focus on the protocol implementation. |
| 84 | +This includes reading both small and big rows, big batches of rows |
| 85 | +and using statements with big parameters. |
| 86 | +Full information available [here](https://www.boost.org/doc/libs/develop/libs/mysql/doc/html/mysql/benchmarks.html). |
| 87 | + |
| 88 | +While I'm pretty satisfied with the results, I've also identified some |
| 89 | +points where performance could be improved. These will be addressed in the near future. |
| 90 | + |
| 91 | +## Boost.MySQL new features and maintenance |
| 92 | + |
| 93 | +Per user request, I've added `any_connection::connection_id()`, equivalent |
| 94 | +to `mysql_thread_id()` in the official API. This allows using `KILL` statements |
| 95 | +to cancel queries. |
| 96 | + |
| 97 | +I've also improved safety by detecting the violation of some preconditions |
| 98 | +and issuing errors rather than incurring in undefined behavior. For example, |
| 99 | +attempting to start an asynchronous operation in an `any_connection` while |
| 100 | +another one is outstanding is no longer UB: |
| 101 | + |
| 102 | +```cpp |
| 103 | +// Since Boost 1.88, this is no longer UB. |
| 104 | +// The second operation will fail with client_errc::operation_in_progress |
| 105 | +conn.async_execute("SELECT 1", r1, [](...) {}); |
| 106 | +conn.async_execute("SELECT 1", r2, [](...) {}); |
| 107 | +``` |
| 108 | + |
| 109 | +I've fixed some other minor bugs and made some improvements to the docs. |
| 110 | +The most remarkable is the addition of a |
| 111 | +[fully-fledged HTTP server example] (https://github.com/boostorg/mysql/tree/07200f17c28293e910151abbc46d22eeff944384/example/3_advanced/http_server_cpp20) |
| 112 | +that uses C++20 coroutines. I'm in the process of writing the equivalent using callbacks, |
| 113 | +for users that prefer them. |
| 114 | + |
| 115 | +## Boost.Decimal review |
| 116 | + |
| 117 | +Another exciting submission. As usual, I contributed to it by integrating it |
| 118 | +into Boost.MySQL, to parse `DECIMAL` types. |
| 119 | + |
| 120 | +## Next steps |
| 121 | + |
| 122 | +I'm preparing a Postgres library that I aim to submit to Boost on the long run. |
| 123 | +It's still in a very early state. I intend to make it similar to Boost.MySQL, |
| 124 | +but exposing the protocol interface as a public API. More on this next quarter! |
0 commit comments