Skip to content
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

Perf: Potential performance issue in MappedClassField #5

Closed
EggAllocationService opened this issue May 6, 2024 · 2 comments
Closed
Labels
Needs Exploration Things that should be looked at in the future

Comments

@EggAllocationService
Copy link
Collaborator

EggAllocationService commented May 6, 2024

One performance issue on the horizon is the use of reflection in the default property extractor. Reflection in C# is pretty slow. In our case there's evidence that using FieldInfo as is currently done in the MappedClassField implementation is around sixty times slower than direct assignment [1].

Some steps should be taken to make sure that this is actually a problem:

  • Performance testing with lots of mapped properties exposed in libmapper
  • Running some flamegraphs to see where any problems actually lie

Potential performance improvement paths

Runtime Code Generation

Use DynamicMethod to generate getters/setters at runtime.

Pros:

  • Works for any Component type thrown at us, and is definitely faster than just reflection.

Potential issues are platforms where runtime code generation isn't possible or is very slow, such as iOS

Compile-time Code Generators

Use the Roslyn generators system to automatically create concrete classes implementing IMappedProperty. This is probably the fastest implementation possible

References

[1]: https://medium.com/@veyseler.cs.ist/performance-test-for-setting-a-field-in-c-with-reflection-3e2e41d8a2ab

@EggAllocationService EggAllocationService added the Needs Exploration Things that should be looked at in the future label May 6, 2024
@EggAllocationService
Copy link
Collaborator Author

Initial experiments with generating code via c#'s DynamicMethod seem to yield a ~5x performance increase on the synchronous part of UnityMapper. Will probably want to test on lower-end hardware.

See 97730ea for implementation

@EggAllocationService
Copy link
Collaborator Author

Tested and this is no long an issue on the instances rewrite branch which is going to be merged soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Exploration Things that should be looked at in the future
Projects
None yet
Development

No branches or pull requests

1 participant