From a2c067cc49b96968da81031275de9c44c4ba2ee9 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 12 May 2026 22:36:11 +0300 Subject: introduce RuntimeBuilder to separate construction from orchestration in ior.go RuntimeBuilder encapsulates allocation of per-trace-session components (statsengine.Engine, streamrow.RingBuffer, Sequencer, flamegraph.LiveTrie); buildTUIRuntime, buildTestFlamesRuntime, and buildTestLiveFlamesRuntime now delegate construction to RuntimeBuilder.Build() and focus only on wiring. wireRuntimeBindings is extracted to isolate the context-binding wiring step. Co-Authored-By: Claude Sonnet 4.6 --- internal/runtime_builder.go | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 internal/runtime_builder.go (limited to 'internal/runtime_builder.go') diff --git a/internal/runtime_builder.go b/internal/runtime_builder.go new file mode 100644 index 0000000..bc1c228 --- /dev/null +++ b/internal/runtime_builder.go @@ -0,0 +1,49 @@ +package internal + +import ( + "ior/internal/flags" + "ior/internal/flamegraph" + "ior/internal/statsengine" + "ior/internal/streamrow" +) + +const defaultEngineCapacity = 64 + +// runtimeComponents holds the freshly allocated trace-session components +// produced by RuntimeBuilder.Build. All fields are non-nil after a successful +// build. The caller is responsible for wiring these into the runtime bindings +// and event-loop callbacks. +type runtimeComponents struct { + engine *statsengine.Engine + streamBuf *streamrow.RingBuffer + streamSeq *streamrow.Sequencer + liveTrie *flamegraph.LiveTrie +} + +// RuntimeBuilder encapsulates the allocation of per-trace-session runtime +// components. Construct one with newRuntimeBuilder, then call Build to +// produce a fresh runtimeComponents struct ready for wiring. +type RuntimeBuilder struct { + // cfg holds the configuration used to size and configure the components. + cfg flags.Config +} + +// newRuntimeBuilder creates a RuntimeBuilder configured from cfg. +// The builder captures cfg at construction time; later cfg changes do not affect +// a builder already created. +func newRuntimeBuilder(cfg flags.Config) RuntimeBuilder { + return RuntimeBuilder{cfg: cfg} +} + +// Build allocates a fresh set of runtime components for one trace session. +// It creates a new stats engine, stream ring buffer, sequencer, and live trie +// each time it is called; callers must not share the returned components across +// concurrent trace sessions. +func (b RuntimeBuilder) Build() runtimeComponents { + return runtimeComponents{ + engine: statsengine.NewEngine(defaultEngineCapacity), + streamBuf: streamrow.NewRingBuffer(), + streamSeq: streamrow.NewSequencer(0), + liveTrie: flamegraph.NewLiveTrie(b.cfg.CollapsedFields, b.cfg.CountField), + } +} -- cgit v1.2.3