Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Transform speedup  (Read 2877 times)

0 Members and 1 Guest are viewing this topic.

DavidKarlas

  • Newbie
  • *
  • Posts: 5
    • View Profile
Transform speedup
« on: May 28, 2013, 09:00:55 pm »
Hi all,

i started making 2D engine based on SFML.Net and i came to point where i started handling Mouse events.
Soon i ended up in Transform.cs file and saw that is calling C++ implementation. And i was wondering how costy is Managed <-> Unmanaged barrier for something like TransformPoint so i started checking and found out following:

Transform TransformPoint:1111
MyTransform TransformPoint:119
Transform TransformRect:1312
MyTransform TransformRect:419
Transform Multiply(Identity):1603
MyTransform Multiply(Identity):401
Transform Multiply:553
MyTransform Multiply:545
Transform Rotate:1823
MyTransform Rotate:1043
Transform Translate:1389
MyTransform Translate:262
Transform Scale:524
MyTransform Scale:513
Transform Inverse:1913
MyTransform Inverse:897

Program used for comparison:
https://gist.github.com/DavidKarlas/c3abb1fcb5a26fd1c5e4

MyTransform:
https://gist.github.com/DavidKarlas/931b090e2c8a05c52aa9

What i found out during testing this code is that C# is slower then C++ when it comes to wierd float values like mutiplying NaN or Infinity values. That is why Scale and Multiply are so close. Because after few loops Infinity numbers appear and C# goes slower but that probably wont happend in real case but speedup wont be better then 4x(Multiply(Identity)) either.

Most important to me is TransformPoint 10x speed up because i will have alot of nested drawables and will have to calculate mouse hit point.

I also tested what this means in general rendering...
I created 188.000 small sprites with random rotation, scale and positions(doesn't matter but wanted abit more complex matrix) and i came from 16 fps to 20fps just by switching Transform to MyTransform inside SFML.net.

I also noticed that GetInverse and Rotate could use double instead of float for better accurarcy without any speed loss.

Laurent is there any chance you could take a look at this and implement it?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Transform speedup
« Reply #1 on: May 28, 2013, 09:06:45 pm »
Implement what? You mean rewrite the Transform class in pure C#? I can't, since other functions take a SFML Transform, they wouldn't be able to use an unknown managed structure.

By the way, it's even worse than what you think: SFML.Net calls CSFML, not SFML directly ;)
Laurent Gomila - SFML developer

DavidKarlas

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Transform speedup
« Reply #2 on: May 28, 2013, 09:18:24 pm »
Well from what i see Transform struct is already pure C# struct but it just calls C++ functions to mainpulate it. I don't think its decleared inside C++. But instead always passed from C# to C++ when needed.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Transform speedup
« Reply #3 on: May 28, 2013, 10:15:30 pm »
Quote
Well from what i see Transform struct is already pure C# struct but it just calls C++ functions to mainpulate it.
Ah, you're right, sorry. I changed the implementation several times, I should have checked before answering.

So you would like me to rewrite the functions in plain C# instead of calling the C functions? This would be possible, but I'm not a fan or rewriting parts of the API in a binding, because it becomes less maintainable and more error-prone.
Laurent Gomila - SFML developer

DavidKarlas

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Transform speedup
« Reply #4 on: May 29, 2013, 04:29:30 am »
This is why I didn't go straigth for pull request and made forum topic instead. So you could evaluate performance boost vs "problems" and check source code before commiting. Btw that class is copy paste ready just needs massive replace MyTransformable to Transformable.

Well i hope you add  this because im rly not fan of having fork :)
« Last Edit: May 29, 2013, 05:43:46 am by DavidKarlas »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Transform speedup
« Reply #5 on: May 29, 2013, 08:04:26 am »
Before forking... complete your engine first, and when it's finished, do some benchmarks with real use cases (i.e. with the huge amount of other functions that are executed) and see if it's still significant. You shouldn't focus on such details if you just started to write your engine. Implement features, and optimize later.
Laurent Gomila - SFML developer