Renderer: published frames are recycled (their vectors' capacity kept); the draw-list digest only when a record wants it

Each game frame was built in fresh vectors (the previous frame's moved into the published one): a busy frame's
vertices, several MB, grew by doubling and touched new pages every frame, and were freed by whichever thread let go
last (Renderer::draw was 2.8% of the game thread, against the fork's 0.2%). Frames now come back to a small pool when
their last holder lets go. noteDrawList hashed every vertex of every frame for the record's `draws` field: the free
host turns it off when it writes no record (Renderer::setDrawDigest); the call-log checks keep it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Thomas MartinandClaude Opus 5.5 committed 2026-11-04 23:15:23 +01:00
1 parent 70217841b5
commit 171389f470
3 files changed
+63 -1

No files matched your search

+2
View File
@@ -118,6 +118,8 @@ namespace bozo::host
h.elfName = "SCUS_971.01";
writing_ = writer_.open(opt_.out, h);
}
// The draw lists' digest is the record's `draws`: not computed when no record is written.
render::Renderer::instance().setDrawDigest(writing_);
return true;
}
+17
View File
@@ -238,6 +238,10 @@ namespace bozo::render
// The draw lists published since the last call, folded (the reference record's `draws`), and restarted: the
// host takes it at each game frame's end.
FrameDraws takeFrameDraws();
// Whether the draw lists are folded into that digest (on by default: the call-log checks and a record need it).
// A host that never takes it (free play, no record written) turns it off: hashing every vertex a frame is
// ~0.25 ms of a busy 1P frame.
void setDrawDigest(bool on) { m_digestDraws = on; }
private:
Renderer();
@@ -250,6 +254,19 @@ namespace bozo::render
Frame m_building;
Frame m_overlayBuilding;
bool m_digestDraws = true;
// Published frames come back here when their last holder lets go (their vectors' capacity kept), and the next
// frame is built in one: a busy frame's vertices (several MB) were a fresh allocation, grown by doubling and
// its pages touched for the first time, every frame. Shared with the frames' deleters (any thread).
struct FramePool
{
std::mutex mutex;
std::vector<Frame *> free;
~FramePool();
};
std::shared_ptr<FramePool> m_pool = std::make_shared<FramePool>();
// A published frame holding m_building's content (m_building gets a recycled frame's emptied vectors).
std::shared_ptr<Frame> takeBuilt(Frame &building);
bool m_enabled = false;
bool m_presentsAlone = false;
bool m_streamDraws = false;
+44 -1
View File
@@ -72,8 +72,48 @@ namespace bozo::render
return renderer;
}
Renderer::FramePool::~FramePool()
{
for (Frame *f : free)
delete f;
}
std::shared_ptr<Renderer::Frame> Renderer::takeBuilt(Frame &building)
{
Frame *out = nullptr;
{
std::lock_guard<std::mutex> lock(m_pool->mutex);
if (!m_pool->free.empty())
{
out = m_pool->free.back();
m_pool->free.pop_back();
}
}
if (!out)
out = new Frame;
std::swap(*out, building); // building now holds the recycled frame's old content (emptied by startBuilding)
std::shared_ptr<FramePool> pool = m_pool;
return std::shared_ptr<Frame>(out, [pool](Frame *f)
{
// Emptied here, by whichever thread let go last (as the frame's destruction was); the capacity stays.
f->vertices.clear();
f->batches.clear();
f->triangleIds.clear();
f->keys.clear();
f->bases.clear();
f->tracked.clear();
std::lock_guard<std::mutex> lock(pool->mutex);
if (pool->free.size() < 8u)
pool->free.push_back(f);
else
delete f;
});
}
void Renderer::noteDrawList(const Frame &frame, uint64_t kind)
{
if (!m_digestDraws)
return;
static_assert(sizeof(Vertex) == 32u, "Vertex: eight 4-byte fields, no padding");
uint64_t h = hash::xxh64(&kind, sizeof(kind), 0);
for (const Batch &batch : frame.batches)
@@ -114,12 +154,15 @@ namespace bozo::render
{
std::vector<Vertex> vertices = std::move(m_building.vertices);
std::vector<Batch> batches = std::move(m_building.batches);
std::vector<uint64_t> triangleIds = std::move(m_building.triangleIds);
vertices.clear();
batches.clear();
triangleIds.clear();
m_building = Frame{};
m_building.screen = screen;
m_building.vertices = std::move(vertices);
m_building.batches = std::move(batches);
m_building.triangleIds = std::move(triangleIds);
}
void Renderer::draw(const Material &material, const float matrix[16], const Vertex *vertices, uint32_t count)
@@ -246,7 +289,7 @@ namespace bozo::render
void Renderer::endFrame()
{
noteDrawList(m_building, 1u);
auto frame = std::make_shared<Frame>(std::move(m_building));
std::shared_ptr<Frame> frame = takeBuilt(m_building);
startBuilding(Screen{});
m_key = 0;
frame->time = secondsNow();