user: replace O(N) loop in GetNext<T> with O(1) id lookup#3336
user: replace O(N) loop in GetNext<T> with O(1) id lookup#3336Pradyuman-aviator wants to merge 3 commits into
Conversation
NextObject() calls mjs_nextElement() which calls GetNext<T>() on flat model lists (actuators, sensors, flexes, meshes, etc.). The previous implementation scanned the list linearly to find child, despite every element already carrying a unique, contiguous id set at insertion time (AddObject/AddObjectDefault). Replace the loop with a direct index using the id field, accessed via static_cast<const mjCBase*>(child)->id. This cast is safe because mjCBase_ publicly inherits from mjsElement as its first base class (same pointer address), and is already used throughout the codebase (e.g. mjs_getId in user_api.cc). Resolves the TODO comment left in the original code.
|
hi @quagla I was looking through the codebase to see where I could make a contribution and noticed your TODO in I have implemented the direct Let me know if there are any specific tests you'd like me to run or add for this! Happy to adjust if needed. |
|
@kevinzakka if you got time please review it Thank You! |
|
I'll check for those failed build check & gonna update soon |
…to fix/getnext-o1-direct-indexing
|
@yuvaltassa i changed logic as validating before using the id and it gonna fall back to original loop if it seems invalid still O(1) in common cases |
|
@yuvaltassa all checks passed now |
|
Hi @quagla, thanks for the review! Just checking in |
|
@Pradyuman-aviator Can you not spam please? |
|
@kevinzakka I sincerely apologize for the multiple pings! I am very new to opensource contribution and got a bit too eager while trying to apply my theoretical knowledge to real world codebases. I'm still learning the standard etiquette and pacing for PR reviews, so I appreciate your patience. |
NextObject() calls mjs_nextElement() which calls GetNext() on flat model lists (actuators, sensors, flexes, meshes, etc.). The previous implementation scanned the list linearly to find child, despite every element already carrying a unique, contiguous id set at insertion time (AddObject/AddObjectDefault).
Replace the loop with a direct index using the id field, accessed via static_cast<const mjCBase*>(child)->id. This cast is safe because mjCBase_ publicly inherits from mjsElement as its first base class (same pointer address), and is already used throughout the codebase (e.g. mjs_getId in user_api.cc).
Resolves the TODO comment left in the original code.