-
Notifications
You must be signed in to change notification settings - Fork 131
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
Floating Point vs Integer #27
Comments
You're right about some of the lines being unoptimized — things like min/max lines were a direct JS ports, certainly they can be made faster with a C++-specific approach. Regarding integer casts — we'd like to keep the library minimal, avoiding dependencies like boost. If you have any particular quick fixes in mind, we'll be happy to see more PRs! |
It is already depending on boost https://github.com/mapbox/earcut.hpp/blob/master/include/earcut.hpp#L12 |
Oops, right :) I'm not knowledgeable enough to comment meaningfully here. |
Yes, the manual min/maxing should be replaced with I'd like to figure out a way to eliminate the |
@mrgreywater is this issue still relevant? |
Yes, I think it is. Not converting from integer to floating point, and only doing integer arithmetic when working with integer vertices should increase both precision and performance. It's not strictly required though. I may look into it the next week or so, when I got more time. |
I added integer math test-wise here: To be honest; performance seems to be the same on a modern cpu (might be a different story on a embedded systems). So since this would only increase code complexity, I'd rather not push it to master, unless there's some other reason to have it (e.g numerical stability issues). Benchmark before, i5-5200U:
edit: re-executed bench with high-performance profile |
So on my main rig, I actually see some performance benefit across the board with integer-math: Here's the bench of an i5 3570K, release build compiled with gcc-5.1. Still not sure if the increase in complexity is worth it though...
|
I'm not sure, the fraction logic indeed complicates the code considerably... @jfirebaugh have an opinion? |
The thing about the fraction logic is that theoretically it could be mostly removed here. Instead one could simply do the (floored) division, and work on the integer grid while finding a hole connection (basically bresenham's line rasterization). With the tanCur/tanMin calculation, it could either stay as is, since it doesn't seem overly complicated, or could be converted into some approximated atan2 angle instead. Alternatively one could also adapt the javascript library to use integer math depending on the input, but optimization would highly depend on the used jit engine and it's type tagging. |
Closing this, as there doesn't seem to be much performance gain to be had. It's especially not worth it given the increased code complexity. There may be a use case for micro-controllers without FPU, but unless somebody requires triangulation on a device like that, this isn't worth it. |
earcut.hpp supports both floating point and integer as vertices type, but isn't using the fastest approach in either case.
When using floating point data, lots of the min/max calculations are done using conditions, which slows them down, as x86/AMD64 both have branchless floating point min/max instructions which won't be used in that case. (see https://github.com/mapbox/earcut.hpp/blob/master/include/earcut.hpp#L286)
When using integer data, the code is casting to double quite frequently which slows things down aswell. Instead of casting to double, when expecting an overflow, it should cast to the next bigger integer type, for example with
boost::int_t<size>
The text was updated successfully, but these errors were encountered: