Skip to content

user: replace O(N) loop in GetNext<T> with O(1) id lookup#3336

Open
Pradyuman-aviator wants to merge 3 commits into
google-deepmind:mainfrom
Pradyuman-aviator:fix/getnext-o1-direct-indexing
Open

user: replace O(N) loop in GetNext<T> with O(1) id lookup#3336
Pradyuman-aviator wants to merge 3 commits into
google-deepmind:mainfrom
Pradyuman-aviator:fix/getnext-o1-direct-indexing

Conversation

@Pradyuman-aviator

Copy link
Copy Markdown

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.

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.
@Pradyuman-aviator

Pradyuman-aviator commented Jun 13, 2026

Copy link
Copy Markdown
Author

hi @quagla I was looking through the codebase to see where I could make a contribution and noticed your TODO in src/user/user_model.cc for GetNext<T>

I have implemented the direct id lookup you suggested to replace the O(N) loop. To do this, I safely cast the mjsElement* child to mjCBase* using the same pattern that is already used for mjs_getId in user_api.cc.

Let me know if there are any specific tests you'd like me to run or add for this! Happy to adjust if needed.

@Pradyuman-aviator

Copy link
Copy Markdown
Author

@kevinzakka if you got time please review it Thank You!

@yuvaltassa yuvaltassa requested a review from quagla June 15, 2026 12:15
@Pradyuman-aviator

Copy link
Copy Markdown
Author

I'll check for those failed build check & gonna update soon

@Pradyuman-aviator

Copy link
Copy Markdown
Author

@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

@Pradyuman-aviator

Pradyuman-aviator commented Jun 15, 2026

Copy link
Copy Markdown
Author

@yuvaltassa all checks passed now

@Pradyuman-aviator

Copy link
Copy Markdown
Author

Hi @quagla, thanks for the review! Just checking in
is there anything blocking the merge, or is it waiting
on something from my end?

@kevinzakka

Copy link
Copy Markdown
Collaborator

@Pradyuman-aviator Can you not spam please?

@Pradyuman-aviator

Copy link
Copy Markdown
Author

@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.
I will definitely hold back on the pings going forward. As a small peace offering 😅 I just opened a new, small PR that adds the missing asymmetric ellipsoid shell inertia test (resolving the taylorhowell TODO) here: #3336. Whenever the team has free time, I'd love for you to take a look!

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.

3 participants