⚡ Optimize material color provider lookup#236
Open
CanerKaraca23 wants to merge 1 commit intouser-grinch:mainfrom
Open
⚡ Optimize material color provider lookup#236CanerKaraca23 wants to merge 1 commit intouser-grinch:mainfrom
CanerKaraca23 wants to merge 1 commit intouser-grinch:mainfrom
Conversation
The previous implementation of `ModelInfoMgr::FetchMaterialCol` used a linear search through all registered `matColProviders`, regardless of the material type being requested. This led to unnecessary iterations and function calls, especially for features like sirens that register providers for specific material types. This optimization implements a type-indexed lookup by: 1. Changing `matColProviders` from a single `std::vector` to an array of vectors indexed by `eMaterialType`. 2. Updating `RegisterMaterialColProvider` to accept a material type, defaulting to a "global" bucket (`eMaterialType::TotalMaterial`). 3. Modifying `FetchMaterialCol` to only iterate through providers registered for the specific type, followed by global providers if no match is found. 4. Updating `Sirens::Init` to use type-specific registration for siren materials. Benchmark results on a simulated environment showed a performance improvement of approximately 3x in hits and 9x in misses.
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes ModelInfoMgr material color provider selection by storing providers in per-eMaterialType buckets and checking the relevant bucket before falling back to a general bucket, avoiding a full linear scan across all providers per material.
Changes:
- Replaced the single
matColProvidersvector with a fixed array of vectors indexed byeMaterialType(plus a general/fallback bucket). - Updated
RegisterMaterialColProviderto accept an optionaleMaterialTypeto register against. - Updated the siren material color provider to register specifically for
eMaterialType::SirenLight.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/utils/modelinfomgr.h |
Changes storage and API to support type-indexed provider registration. |
src/utils/modelinfomgr.cpp |
Implements type-indexed registration and lookup with a fallback provider bucket. |
src/features/sirens.cpp |
Registers siren color provider under the siren material type bucket. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| void ModelInfoMgr::RegisterMaterialColProvider(MaterialColProviderCallback_t mat, eMaterialType type) | ||
| { | ||
| matColProviders.push_back(mat); | ||
| matColProviders[type].push_back(mat); |
| @@ -607,22 +607,20 @@ void Sirens::Init() | |||
|
|
|||
| ModelInfoMgr::RegisterMaterialColProvider([](CVehicle *pVeh, RpMaterial *pMat, eMaterialType type) | |||
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.
⚡ [performance improvement] Optimize material color provider lookup
💡 What:
Optimized the material color provider lookup in
ModelInfoMgrby replacing a linear search with a type-indexed lookup.🎯 Why:
The previous implementation iterated through every registered provider for every material being rendered, even if the provider was only intended for a specific material type (e.g., sirens). This was particularly inefficient as the number of providers grew.
📊 Measured Improvement:
In a simulated environment:
These improvements were achieved by reducing the number of lambda calls and iterations from O(N) to O(1) in the common case.