Skip to content

⚡ Optimize pedcols color store to use a flat vector#238

Open
CanerKaraca23 wants to merge 1 commit intouser-grinch:mainfrom
CanerKaraca23:perf/optimize-pedcols-store-6556191510313153556
Open

⚡ Optimize pedcols color store to use a flat vector#238
CanerKaraca23 wants to merge 1 commit intouser-grinch:mainfrom
CanerKaraca23:perf/optimize-pedcols-store-6556191510313153556

Conversation

@CanerKaraca23
Copy link
Copy Markdown
Contributor

💡 What: Replaced the std::unordered_map<CPed*, std::vector<std::pair<void *, int>>> with a flat std::vector<std::pair<void *, int>> in src/features/pedcols.cpp.
🎯 Why: To improve performance and resolve a memory leak. Rendering in GTA SA via the plugin-sdk is single-threaded and processes entities sequentially. Using a hash map with the ped pointer as a key was unnecessary. Furthermore, unconditionally calling store[pPed].clear() in the after event caused empty vectors to be inserted into the map for every ped rendered, leading to unbounded map growth over time as peds spawn and despawn.
📊 Measured Improvement: In a standalone tight-loop benchmark simulating the render events, replacing the map with a vector improved performance from ~71ms to ~24ms (an approx. 3x speedup). This optimization eliminates hash map overhead and prevents the slow memory leak.

Replaced the `std::unordered_map<CPed*, std::vector<...>>` in `src/features/pedcols.cpp` with a flat `std::vector`. Since GTA SA ped rendering is single-threaded and sequential (`before` -> render -> `after`), a single vector is safe to use and eliminates per-ped hash map lookups, small vector allocations, and a subtle memory leak caused by `store[pPed].clear()` unconditionally inserting empty vectors for peds.
Copilot AI review requested due to automatic review settings May 8, 2026 19:23
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the PedCols rendering path by replacing a per-ped unordered_map material-color store with a single flat vector that is cleared after each pedRenderEvent, aiming to reduce overhead and prevent unbounded growth.

Changes:

  • Replace std::unordered_map<CPed*, std::vector<std::pair<void*, int>>> with std::vector<std::pair<void*, int>> for the temporary material-color store.
  • Update material capture to append into the flat vector during pedRenderEvent.before.
  • Update restoration to iterate the flat vector and clear it during pedRenderEvent.after.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/features/pedcols.cpp
Comment on lines +38 to 41
store.push_back(std::make_pair(&pMaterial->color, *reinterpret_cast<int *>(&pMaterial->color)));
pMaterial->color.red = data.m_Colors[idx].r;
pMaterial->color.green = data.m_Colors[idx].g;
pMaterial->color.blue = data.m_Colors[idx].b;
Comment thread src/features/pedcols.cpp
Comment on lines +108 to 110
for (auto &e : store) {
*static_cast<int *>(e.first) = e.second;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants