{{description>Optimize Unity spline performance with Curvy Splines! Modify cache density, use threading, pooling, and CG tweaks for a smoother experience. Learn more here.}} {{indexmenu_n>7}} ====== Performance Tips ====== Curvy Splines tries to cater to the widest audience possible. Doing so, it can make some choices performance wise that are not the optimal for your unique use case. In this page you will learn about those choices and the way you can tweak Curvy Splines to get the most of it. And don't forget, Unity's profiler is your best friend. ===== Caching ===== === The concept === The data of a spline point is defined through mathematical equations. In addition to the possibility of solving those equations each time a point's data is needed, which will take valuable CPU time, Curvy Splines offer an alternative through caching. Caching stores the data of a group of points in memory for further need. That group of points is called cache points. For example, a one meter long spline can be cached as 100 cache points, one every centimeter. If you need the data of a point in between two cache points, a linear interpolation will be done between those two cache points. This mechanism allows gains in performance when getting data from a spline, but it comes with a cost: * Caching uses memory * Constructing the cache takes time * The linear interpolation between cache points returns values that diverge from the actual spline In summary, the more cache points there are, the more the cached data is precise, but the slower it is to build them, and in some case to access them. === How to increase performance === == Activate it when optional == The [[:documentation:controllers:start|Spline Controllers]], [[documentation:generator:modules:inputsplinepath]] and [[documentation:generator:modules:inputsplineshape]] have optional "Use Cache" options. Activate them if you want to make them use the spline's cache. == Use the right API methods == If using the Curvy Splines through the API, some methods have a "Fast" alternative that uses the cache. For example, CurvySpline.InterpolateFast uses cache while CurvySpline.Interpolate doesn't. == Set the correct number of cache points == The cache density set by default to new splines should be good for most users, but if you work with very long or very short splines, you might need to tweak the cache density such as to use the lower satisfying amount of cache points possible. You can define that number thought those two parameters: * [[documentation:splines:curvyspline:#Max Points Per Unit]]: This defines the number of sample points per world distance unit. * [[documentation:splines:curvyspline:#Cache Density]]: This is a value between 1 and 100. The higher the number, the more sample points are per distance unit. When it is equal to 100, the spline will have [[documentation:splines:curvyspline:#Max Points Per Unit]] sample points per unit. For example, if you set Cache Density to 100 and [[documentation:splines:curvyspline:#Max Points Per Unit]] to 8, a spline segment with a length of 20 units will calculate 160 cache points.

===== Curvy Generator ===== === Split splines === When a spline is used as an input for a Curvy Generator, the CG will update every time that spline is modified. That means that if you modify a kilometer long spline used for mesh extrusion, the whole mesh will be updated even if only the last meter of the spline is modified. A way to avoid this scenario would be to have a CG extruding the static part of the spline, and another one extruding the part that will be updated (that last meter) === Generate only needed data === Mesh generated by CG can have data that you don't need, like collider, tangents, UV2. Disable its generation to reduce generation's time === Allow parallel mesh baking === When baking mesh colliders, the Create Mesh module will try to do that baking in parallel. Unfortunately, due to limitations on Unity's side, the parallel baking will be ignored if you set the cooking options to any value different than the default value. For more details, and any possible future fix, please refer to this Unity forum thread: https://forum.unity.com/threads/physics-bakemesh-question.741257/#post-4999400 === Experiment and measure === Enable the Debug option in a CG's toolbar, and watch the time a module needs to calculate. Some settings can drastically increase the processing time for its module. Optimize settings and try to design your graph in a clever way === Shape Extrusion vs Mesh Deformation === Sometimes, you can achieve very similar results through either the Shape Extrusion module, or the Deform Mesh module. In such cases, prefer using the Shape Extrusion module if you aim for maximum performance, Deform Mesh being more CPU consuming. ===== Use the right Spline features ===== === Types === Splines have different [[documentation:splines:curvyspline:#Interpolation]] types, and [[documentation:splines:curvyspline:#Orientation]] types, some being more expensive than others. Use the simplest one that fits your needs. === Check Transform === When a Control Point's transform is modified, the spline is updated accordingly. Part of the CPU time used by a spline goes in checking if one of its CPs' transform was modified. If you know for sure that such modification will not happen, you can disable that behaviour by setting [[documentation:splines:curvyspline:#check_transform|Check Transform]] to false === Multi-threading === [[documentation:splines:curvyspline:#Use Threading]] ===== Gizmos ===== Displaying a spline's [[documentation:toolbar:start#view_setings|gizmo]] takes CPU time, especially if you show cache points' data when you have a lot of them. Consider disabling unnecessarily displayed gizmo data. You can control also define for each spline if it should [[documentation:splines:curvyspline:#show_gizmos|show its gizmo]] or not. ===== Pooling ===== === The concept === Creating and destroying objects frequently can have a significant performance impact. To fix this, we use pools, where we keep unneeded objects and reuse them later instead of making new ones. Pooling uses more memory but saves on CPU usage. === How to increase performance === By default, Curvy Splines use pools for three types of objects: [[documentation:components:componentpool|components]], [[documentation:components:prefabpool|GameObjects/prefabs]], and [[documentation:components:arraypoolsettings|data structures]]. The default settings of these pools should be enough for most cases, but you can change them if it helps your project. ===== Beyond Curvy Splines ===== We also publish [[https://assetstore.unity.com/packages/tools/utilities/frame-rate-booster-120660?aid=1101l3N9P|Frame Rate Booster]], a Unity asset that automatically reduces your CPU usage. Give it a try, it's easy to use.