Skip to content

Commit 69ba4d7

Browse files
authored
Fix AutoConstructSpline GC Alloc (#29)
Fix AutoConstructSpline GC Alloc Authored-by: Batuhan Dursun <frutbn@protonmail.com>
1 parent 480f429 commit 69ba4d7

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

Plugins/BezierSolution/BezierSpline.cs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ public SplineAutoConstructMode autoConstructMode
7575
}
7676
}
7777
}
78+
79+
private Vector3[] autoConstructedSplineRhs;
80+
private Vector3[] autoConstructedSplineControlPoints;
81+
private float[] autoConstructedSplineTmp;
7882

7983
[SerializeField, HideInInspector]
8084
[UnityEngine.Serialization.FormerlySerializedAs( "Internal_AutoCalculateNormals" )]
@@ -1048,57 +1052,56 @@ public void AutoConstructSpline()
10481052
return;
10491053
}
10501054

1051-
Vector3[] rhs;
1052-
if( m_loop )
1053-
rhs = new Vector3[n + 1];
1054-
else
1055-
rhs = new Vector3[n];
1055+
int rhsLength = m_loop ? n + 1 : n;
1056+
if( autoConstructedSplineRhs == null || autoConstructedSplineRhs.Length != rhsLength )
1057+
autoConstructedSplineRhs = new Vector3[rhsLength];
1058+
if( autoConstructedSplineControlPoints == null || rhsLength != autoConstructedSplineControlPoints.Length )
1059+
autoConstructedSplineControlPoints = new Vector3[rhsLength]; // Solution vector
1060+
if( autoConstructedSplineTmp == null || rhsLength != autoConstructedSplineTmp.Length )
1061+
autoConstructedSplineTmp = new float[rhsLength]; // Temp workspace
1062+
10561063

10571064
for( int i = 1; i < n - 1; i++ )
1058-
rhs[i] = 4 * endPoints[i].position + 2 * endPoints[i + 1].position;
1065+
autoConstructedSplineRhs[i] = 4 * endPoints[i].position + 2 * endPoints[i + 1].position;
10591066

1060-
rhs[0] = endPoints[0].position + 2 * endPoints[1].position;
1067+
autoConstructedSplineRhs[0] = endPoints[0].position + 2 * endPoints[1].position;
10611068

10621069
if( !m_loop )
1063-
rhs[n - 1] = ( 8 * endPoints[n - 1].position + endPoints[n].position ) * 0.5f;
1070+
autoConstructedSplineRhs[n - 1] = ( 8 * endPoints[n - 1].position + endPoints[n].position ) * 0.5f;
10641071
else
10651072
{
1066-
rhs[n - 1] = 4 * endPoints[n - 1].position + 2 * endPoints[n].position;
1067-
rhs[n] = ( 8 * endPoints[n].position + endPoints[0].position ) * 0.5f;
1073+
autoConstructedSplineRhs[n - 1] = 4 * endPoints[n - 1].position + 2 * endPoints[n].position;
1074+
autoConstructedSplineRhs[n] = ( 8 * endPoints[n].position + endPoints[0].position ) * 0.5f;
10681075
}
10691076

10701077
// Get first control points
1071-
int rhsLength = rhs.Length;
1072-
Vector3[] controlPoints = new Vector3[rhsLength]; // Solution vector
1073-
float[] tmp = new float[rhsLength]; // Temp workspace
1074-
10751078
float b = 2f;
1076-
controlPoints[0] = rhs[0] / b;
1079+
autoConstructedSplineControlPoints[0] = autoConstructedSplineRhs[0] / b;
10771080
for( int i = 1; i < rhsLength; i++ ) // Decomposition and forward substitution
10781081
{
10791082
float val = 1f / b;
1080-
tmp[i] = val;
1083+
autoConstructedSplineTmp[i] = val;
10811084
b = ( i < rhsLength - 1 ? 4f : 3.5f ) - val;
1082-
controlPoints[i] = ( rhs[i] - controlPoints[i - 1] ) / b;
1085+
autoConstructedSplineControlPoints[i] = ( autoConstructedSplineRhs[i] - autoConstructedSplineControlPoints[i - 1] ) / b;
10831086
}
10841087

10851088
for( int i = 1; i < rhsLength; i++ )
1086-
controlPoints[rhsLength - i - 1] -= tmp[rhsLength - i] * controlPoints[rhsLength - i]; // Backsubstitution
1089+
autoConstructedSplineControlPoints[rhsLength - i - 1] -= autoConstructedSplineTmp[rhsLength - i] * autoConstructedSplineControlPoints[rhsLength - i]; // Back substitution
10871090

10881091
for( int i = 0; i < n; i++ )
10891092
{
10901093
// First control point
1091-
endPoints[i].followingControlPointPosition = controlPoints[i];
1094+
endPoints[i].followingControlPointPosition = autoConstructedSplineControlPoints[i];
10921095

10931096
if( m_loop )
1094-
endPoints[i + 1].precedingControlPointPosition = 2 * endPoints[i + 1].position - controlPoints[i + 1];
1097+
endPoints[i + 1].precedingControlPointPosition = 2 * endPoints[i + 1].position - autoConstructedSplineControlPoints[i + 1];
10951098
else
10961099
{
10971100
// Second control point
10981101
if( i < n - 1 )
1099-
endPoints[i + 1].precedingControlPointPosition = 2 * endPoints[i + 1].position - controlPoints[i + 1];
1102+
endPoints[i + 1].precedingControlPointPosition = 2 * endPoints[i + 1].position - autoConstructedSplineControlPoints[i + 1];
11001103
else
1101-
endPoints[i + 1].precedingControlPointPosition = ( endPoints[n].position + controlPoints[n - 1] ) * 0.5f;
1104+
endPoints[i + 1].precedingControlPointPosition = ( endPoints[n].position + autoConstructedSplineControlPoints[n - 1] ) * 0.5f;
11021105
}
11031106
}
11041107

@@ -1501,4 +1504,4 @@ internal void Reset()
15011504
}
15021505
#endif
15031506
}
1504-
}
1507+
}

0 commit comments

Comments
 (0)