- Home /
[Linux] Bad PInvoke performance in the built application
Hi
We've written some wrappers around CGAL mesh boolean operations. It works perfectly in Windows, without any clues that something goes wrong.
There are some benchmarks (dotnet benchmark) connected to these wrappers. The main one checks union of two meshes in a single thread. The result using this tool is ok, from 8 to 9 seconds for an operation in Linux using the Embedded mono for 2021.1.26.f1.
Then we move demo meshes along with .cs wrapper code to the Unity3d application and create an empty scene with a single script object that unites meshes during the Start()
After pressing play, we measure the time it took to execute the method. 10 seconds. A bit more, but still ok. Switch scripts mode from Debug to Release. Wait a bit and play the same scene again. 8.5 seconds per operation. The same performance we've got in benchmarks.
Now, build the application using Mono backend and all default player settings. It's ready, the native .so library has been put to the Plugins directory and preloaded when the app is started.
When we run it, the single operation (there is nothing else in the app at all) of calling the native method takes not 8, but 100 seconds.
Approximately 10 times degradation. (the time is taken by the System.Diagnostic.Stopwatch)
[DllImport("cgal_native", EntryPoint = "mesh_boolean")]
private static extern MeshBooleanOut MeshBooleanNativeMethod(MeshBooleanParams parameters);
Both parameters MeshBooleanOut
and MeshBooleanParams
are simple structs and work just fine that is proved by benchmarks. These structures include simple fields int, double, int*, double*
The CLI is identical for the regular dotnet console app and built Assembly-CSharp.dll The LDD_DEBUG=libs shows the same path for libcgal_native.so dependencies. [SuppressUnmanagedCodeSecurity}
is put to all methods, but I'm aware it's uselessness
What else can be a problem? What makes Unity3d work completely fine in Editor and so slow in the standalone application using the same scripting backend?
Thank you
OS: Ubuntu 20.04, reproduces in Ubuntu 18.04 and Ubuntu 21.11