⚡ perf: replace pedcols unordered_map with fast struct array access#239
Open
CanerKaraca23 wants to merge 2 commits intouser-grinch:mainfrom
Open
Conversation
Moves the temporary render color storage from a global std::unordered_map keyed by CPed pointers to the struct-managed PedData. This completely avoids O(N) heap allocations resulting from hash bucket insertions and improves lookup speed to O(1) via the fast data extender pool. Previous map implementation also resulted in an implicit memory leak as map entries were `.clear()`ed rather than `.erase()`d.
Moves the temporary render color storage from a global std::unordered_map keyed by CPed pointers to the struct-managed PedData. This completely avoids O(N) heap allocations resulting from hash bucket insertions and improves lookup speed to O(1) via the fast data extender pool. Previous map implementation also resulted in an implicit memory leak as map entries were `.clear()`ed rather than `.erase()`d.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the PedCols render path by removing a per-render std::unordered_map lookup and instead storing per-ped “original material color” restore data directly in the existing PedData extended-data object, avoiding map growth and improving cache locality.
Changes:
- Added
PedData::m_OriginalColorsto hold original material color state per ped. - Updated the material-editing render hook to append restore entries to
PedDatainstead of a global map. - Updated the post-render hook to restore colors from
PedDataand clear the vector each frame.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/features/pedcols.h | Adds per-ped storage for original material colors in PedData. |
| src/features/pedcols.cpp | Switches render-time save/restore from a global unordered_map to per-ped extended-data vector storage. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
15
to
18
| std::vector<RpMaterial*> materials; | ||
| std::vector<CRGBA> m_Colors; | ||
| std::vector<std::pair<void *, int>> m_OriginalColors; | ||
| bool m_bUsingPedCols = false; |
Comment on lines
34
to
40
| default: | ||
| return pMaterial; | ||
| } | ||
| store[PedColors::m_pCurrentPed].push_back(std::make_pair(&pMaterial->color, *reinterpret_cast<int *>(&pMaterial->color))); | ||
| data.m_OriginalColors.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 on lines
+107
to
+111
| auto &data = PedColors::m_PedData.Get(pPed); | ||
| for (auto &e : data.m_OriginalColors) { | ||
| *static_cast<int *>(e.first) = e.second; | ||
| } | ||
| store[pPed].clear(); | ||
| data.m_OriginalColors.clear(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
💡 What:
Replaced the
std::unordered_map<CPed*, std::vector<std::pair<void*, int>>> storeinsrc/features/pedcols.cppwith a memberstd::vector<std::pair<void*, int>> m_OriginalColorsinside the existingPedDataextended data class.🎯 Why:
The previous implementation performed hash map lookups (
store[pPed]) for every atomic render during the rendering loop. This causes notable CPU overhead due to hash calculation and cache miss penalties. Furthermore, whilestore[pPed].clear()was called after rendering, it only cleared the nested vector and did not erase the map key, leading to a gradual memory leak as new peds spawned over time.📊 Measured Improvement:
A standalone benchmark simulating 10,000 iterations over 100 peds demonstrated the following improvements: