Well,
I have this function which is called in the Unity Job system, I'm profiling it:
The code:
public static void LinearUpscale2DArrayInPlace(NativeArray<double> array, int width, int height, int anchorStepX, int anchorStepY, int min = 0, int max = 0)
{
// Interpolate columns where the anchor points are
var lastYCell = height - anchorStepY;
for (var y = min; y < lastYCell + max; y += anchorStepY)
{
for (var x = min; x < width + max; x += anchorStepX)
{
var index0 = To2D1D(y, x, width);
var index1 = To2D1D(y + anchorStepY, x, width);
if (!IsInBounds(index0, min, max) || !IsInBounds(index1, min, max))
continue;
var startValue = array[index0];
var endValue = array[index1];
var diff = endValue - startValue;
for (var cellY = 1; cellY < anchorStepY; cellY++)
{
var index2 = To2D1D(y + cellY, x, width);
if (!IsInBounds(index2, min, max))
continue;
array[index2] = startValue + diff * cellY / anchorStepY;
}
}
}
// Interpolate in rows, each row has values in the anchor columns
var lastXCell = width - anchorStepX;
for (var y = min; y < height + max; y++)
{
for (var x = min; x < lastXCell + max; x += anchorStepX)
{
var index0 = To2D1D(y, x, width);
var index1 = To2D1D(y, x + anchorStepX, width);
if (!IsInBounds(index0, min, max) || !IsInBounds(index1, min, max))
continue;
var startValue = array[index0];
var endValue = array[index1];
var diff = endValue - startValue;
for (var cellX = 1; cellX < anchorStepX; cellX++)
{
var index2 = To2D1D(y, x + cellX, width);
if (!IsInBounds(index2, min, max))
continue;
array[index2] = startValue + cellX * diff / anchorStepX;
}
}
}
}
public static bool IsInBounds(int v, int min, int max) =>
v >= min && v < max;
public static bool IsInBounds(int v, int length) =>
v >= 0 && v < length;
public static int To2D1D(int x, int y, int width) =>
x + y * width;
I'm unsure what can I do to improve those methods. Can someone guide me?