Finish module consolidation: fold Memo into Pipeline, dedupe Channel/Stopwatch
Completes the in-progress merges (Optional->Either/Opt, Curry+Memo->Pipeline,
ScopeGuard->Scope, String->Str):
- Pipeline/Memo.hpp now carries the full Memo API (memoize with recursion
support, TupleHash, SimpleCache) alongside memoize_fn; old Memo module
and its test dir removed.
- Duplicate Channel module removed; Concurrency::Channel gains empty() and
the unique test cases (drain-after-close, multi-producer, string payloads).
- StopWatch module merged into Time::Stopwatch: one class with explicit
start/stop/laps plus a started() factory for the auto-start RAII style;
ScopedTimer moved along.
- Testing optional dep retargeted String->Str (pad_right lives in Str now).
- Stale empty dirs removed: src/{ScopeGuard,String,Core}, include/Void/{Core,
Filesystem,Memo,Channel,StopWatch}.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
58ff7b38a0
commit
da2e93c405
29 files changed
+752
-1593
No files matched your search
+129
-14
@@ -1,34 +1,90 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
#include <cstddef>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Time
|
||||
{
|
||||
|
||||
/// Minimal RAII wall-clock timer built on std::chrono::steady_clock.
|
||||
/// Wall-clock stopwatch with lap support, built on steady_clock.
|
||||
/// Single-writer; not thread-safe across threads.
|
||||
///
|
||||
/// Usage:
|
||||
/// Explicit style:
|
||||
/// Stopwatch sw;
|
||||
/// sw.start();
|
||||
/// // ... work ...
|
||||
/// sw.lap();
|
||||
/// sw.stop();
|
||||
/// sw.elapsed(); // total elapsed
|
||||
/// sw.laps(); // lap durations
|
||||
///
|
||||
/// RAII style (auto-started):
|
||||
/// auto sw = Stopwatch::started();
|
||||
/// do_work();
|
||||
/// auto ms = sw.elapsed<std::chrono::milliseconds>();
|
||||
/// auto ms = sw.elapsed_as<std::chrono::milliseconds>();
|
||||
class Stopwatch
|
||||
{
|
||||
public:
|
||||
|
||||
using Clock = std::chrono::steady_clock;
|
||||
using TimePoint = Clock::time_point;
|
||||
using Duration = Clock::duration;
|
||||
using Duration = std::chrono::nanoseconds;
|
||||
|
||||
/// Start the stopwatch at construction time.
|
||||
Stopwatch() noexcept : start_(Clock::now()) {}
|
||||
/// Constructed stopped with zero elapsed time.
|
||||
Stopwatch() noexcept = default;
|
||||
|
||||
/// Restart from zero.
|
||||
void reset() noexcept { start_ = Clock::now(); }
|
||||
/// Convenience factory: a stopwatch already running.
|
||||
[[nodiscard]] static Stopwatch started() noexcept
|
||||
{
|
||||
Stopwatch sw;
|
||||
sw.start();
|
||||
return sw;
|
||||
}
|
||||
|
||||
/// Elapsed time since construction or last reset().
|
||||
/// Start (or restart) the timer. Clears recorded laps.
|
||||
void start() noexcept
|
||||
{
|
||||
running_ = true;
|
||||
start_ = Clock::now();
|
||||
laps_.clear();
|
||||
last_lap_ = start_;
|
||||
}
|
||||
|
||||
/// Record a lap split without stopping. Returns the lap duration.
|
||||
Duration lap()
|
||||
{
|
||||
if (!running_) {
|
||||
return Duration::zero();
|
||||
}
|
||||
const auto now = Clock::now();
|
||||
const auto d =
|
||||
std::chrono::duration_cast<Duration>(now - last_lap_);
|
||||
laps_.push_back(d);
|
||||
last_lap_ = now;
|
||||
return d;
|
||||
}
|
||||
|
||||
/// Stop the timer. Returns elapsed time since start.
|
||||
Duration stop() noexcept
|
||||
{
|
||||
if (!running_) {
|
||||
return elapsed_;
|
||||
}
|
||||
elapsed_ =
|
||||
std::chrono::duration_cast<Duration>(Clock::now() - start_);
|
||||
running_ = false;
|
||||
return elapsed_;
|
||||
}
|
||||
|
||||
/// Elapsed time (while running: from start to now; stopped: total).
|
||||
[[nodiscard]] Duration elapsed() const noexcept
|
||||
{
|
||||
return Clock::now() - start_;
|
||||
if (running_) {
|
||||
return std::chrono::duration_cast<Duration>(Clock::now() -
|
||||
start_);
|
||||
}
|
||||
return elapsed_;
|
||||
}
|
||||
|
||||
/// Elapsed time cast to a specific duration type.
|
||||
@@ -38,17 +94,76 @@ class Stopwatch
|
||||
return std::chrono::duration_cast<D>(elapsed());
|
||||
}
|
||||
|
||||
/// Split: return elapsed and reset in one call.
|
||||
/// Elapsed in milliseconds as double.
|
||||
[[nodiscard]] double elapsed_ms() const noexcept
|
||||
{
|
||||
return std::chrono::duration<double, std::milli>(elapsed())
|
||||
.count();
|
||||
}
|
||||
|
||||
/// Split: return elapsed and restart in one call.
|
||||
[[nodiscard]] Duration split() noexcept
|
||||
{
|
||||
auto d = elapsed();
|
||||
reset();
|
||||
const auto d = elapsed();
|
||||
start();
|
||||
return d;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool is_running() const noexcept { return running_; }
|
||||
|
||||
[[nodiscard]] const std::vector<Duration>& laps() const noexcept
|
||||
{
|
||||
return laps_;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t lap_count() const noexcept
|
||||
{
|
||||
return laps_.size();
|
||||
}
|
||||
|
||||
/// Stop and clear all recorded state.
|
||||
void reset() noexcept
|
||||
{
|
||||
running_ = false;
|
||||
elapsed_ = Duration::zero();
|
||||
laps_.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
TimePoint start_;
|
||||
TimePoint start_{};
|
||||
TimePoint last_lap_{};
|
||||
Duration elapsed_{};
|
||||
std::vector<Duration> laps_;
|
||||
bool running_{false};
|
||||
};
|
||||
|
||||
/// RAII timer that records elapsed into a provided Duration on destruction.
|
||||
class ScopedTimer
|
||||
{
|
||||
public:
|
||||
|
||||
using Clock = Stopwatch::Clock;
|
||||
using Duration = Stopwatch::Duration;
|
||||
|
||||
explicit ScopedTimer(Duration& out)
|
||||
: out_{out}, start_{Clock::now()}
|
||||
{
|
||||
}
|
||||
|
||||
~ScopedTimer()
|
||||
{
|
||||
out_ =
|
||||
std::chrono::duration_cast<Duration>(Clock::now() - start_);
|
||||
}
|
||||
|
||||
ScopedTimer(const ScopedTimer&) = delete;
|
||||
ScopedTimer& operator=(const ScopedTimer&) = delete;
|
||||
|
||||
private:
|
||||
|
||||
Duration& out_;
|
||||
Clock::time_point start_;
|
||||
};
|
||||
|
||||
} // namespace Void::Time
|
||||
Reference in new issue
Block a user