-
Notifications
You must be signed in to change notification settings - Fork 55
[Controller] POC: overall speedup #573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fredroy
wants to merge
7
commits into
sofa-framework:master
Choose a base branch
from
fredroy:speedup_controller
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
67c8474
cache cast, calls
fredroy 9b48fa0
add test
fredroy 811a454
Invalidate Controller method cache on __setattr__ to support runtime …
fredroy 1c41d80
add test on reassigment
fredroy 8579a1b
remove unnessary test
fredroy 10438e3
check if the value is callable
fredroy 486c35c
Unify onEvent into m_methodCache
fredroy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import Sofa | ||
|
|
||
| g_nb_controllers = 10 | ||
| g_nb_steps = 10000 | ||
|
|
||
| class EmptyController(Sofa.Core.Controller): | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| Sofa.Core.Controller.__init__(self, *args, **kwargs) | ||
|
|
||
| # Default Events ********************************************* | ||
| def onAnimateBeginEvent(self, event): # called at each begin of animation step | ||
| # print(f"{self.name.value} : onAnimateBeginEvent") | ||
| pass | ||
|
|
||
| def createScene(root): | ||
| root.dt = 0.01 | ||
| root.bbox = [[-1, -1, -1], [1, 1, 1]] | ||
| root.addObject('DefaultVisualManagerLoop') | ||
| root.addObject('DefaultAnimationLoop') | ||
|
|
||
|
|
||
| for i in range(g_nb_controllers): | ||
| root.addObject(EmptyController(name=f"MyEmptyController{i}")) | ||
|
|
||
|
|
||
| def main(): | ||
| root = Sofa.Core.Node("root") | ||
| createScene(root) | ||
| Sofa.Simulation.initRoot(root) | ||
|
|
||
| # Import the time library | ||
| import time | ||
| start = time.time() | ||
| for iteration in range(g_nb_steps): | ||
| Sofa.Simulation.animate(root, root.dt.value) | ||
| end = time.time() | ||
|
|
||
| print(f"Scene with {g_nb_controllers} controllers and {g_nb_steps} steps took {end - start} seconds.") | ||
|
|
||
| print("End of simulation.") | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import Sofa | ||
|
|
||
| g_nb_controllers = 3 | ||
| g_controllers = [] | ||
|
|
||
| class TestReassignmentController(Sofa.Core.Controller): | ||
|
|
||
| def __init__(self, *args, **kwargs): | ||
| Sofa.Core.Controller.__init__(self, *args, **kwargs) | ||
|
|
||
| def onAnimateBeginEvent(self, event): | ||
| print(f"{self.name.value} : onAnimateBeginEvent") | ||
| pass | ||
|
|
||
| def modifiedAnimateBeginEvent(self, event): | ||
| print(f"{self.name.value} : modifiedAnimateBeginEvent") | ||
| pass | ||
|
|
||
| def createScene(root): | ||
| root.dt = 0.01 | ||
| root.bbox = [[-1, -1, -1], [1, 1, 1]] | ||
| root.addObject('DefaultVisualManagerLoop') | ||
| root.addObject('DefaultAnimationLoop') | ||
|
|
||
| for i in range(g_nb_controllers): | ||
| controller = root.addObject(TestReassignmentController(name=f"Controller{i}")) | ||
| g_controllers.append(controller) | ||
|
|
||
|
|
||
| def main(): | ||
| root = Sofa.Core.Node("root") | ||
| createScene(root) | ||
| Sofa.Simulation.initRoot(root) | ||
|
|
||
| # one step with the "fixed" implementation of onAnimateBeginEvent | ||
| Sofa.Simulation.animate(root, root.dt.value) # should print "ControllerX : onAnimateBeginEvent" | ||
|
|
||
| # reassign onAnimateBeginEvent method | ||
| for controller in g_controllers: | ||
| controller.onAnimateBeginEvent = controller.modifiedAnimateBeginEvent | ||
|
|
||
| Sofa.Simulation.animate(root, root.dt.value) # should print "ControllerX : modifiedAnimateBeginEvent" | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As msg_info() already contains f_printLog.getValue() test this should be implemented with one-liner more or less like:
msg_info() << "on" << event->getClassName() << " " << py::cast<std::string>(py::str(PythonFactory::toPython(event)));I'm a bit puzzled by the conversion chain from event to python, to str, to string. Cannot we have a direct c++ event to std::string ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but even if the msg_info() is not printed,
py::cast<std::string>(py::str(PythonFactory::toPython(event))will still be executed (for nothing as we dont want the print) no ?AFAIK (but I may be wrong) there is no serialization of an Event in Sofa C++ so it needs to be done first.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure the cast part will be executed because the msg_info() has been specifically implemented so that the right side of conditional on-liners messages cost nothing in case the condition is false.
So unless a makes a mistake, the code you are pointing is actually equivalent to:
Side note: I would vote for event serialization in c++ ;)