title: reducing c++ asio timer scheduling latency
body: <p>ASIO timer sheduling code seems to have a latency around 1us (a cost of a syscall).</p>
  <p>Benchmark</p>
  <pre><code>void asio_timer_scheduling_benchmark(benchmark::State&amp; state) {
    auto ctx = asio::io_context{1};
    auto timer = asio::system_timer{ctx};
  
    for ([[maybe_unused]] auto _ : state) {
      timer.expires_after(5ms);
      timer.async_wait([&amp;](auto&amp;&amp;){}); // &lt;-- this one
      timer.cancel();
      ctx.poll();
    }
  }
  
  BENCHMARK(asio_timer_scheduling_benchmark);
  </code></pre>
  <p>Says</p>
  <pre><code>------------------------------------------------------------------------------
  Benchmark                                    Time             CPU   Iterations
  ------------------------------------------------------------------------------
  asio_async_op_post_benchmark              84.2 ns         84.1 ns      8286940
  asio_timer_scheduling_benchmark            400 ns          400 ns      1758090
  </code></pre>
  <p>And perf says</p>
  <pre><code> 45.23%  bdx_benchmarks  libc-2.28.so         [.] timerfd_settime
    8.18%  bdx_benchmarks  [vdso]               [.] __vdso_clock_gettime
  </code></pre>
  <p>Questions</p>
  <ol>
  <li>Curiosity - why syscalls for timers?</li>
  <li>Practical - Is rolling my own timer wheel on a dedicated thread is the only option to lower this latency or something can be done within ASIO to improve it? I am happy with microsecond precision, but not scheduling latency.</li>
  </ol>