Optimizing Design Script code to make it work faster

origins;
directions;
view;
a = null;
finalPoints = [];
[Imperative]
{
	for (i in 0..Count(origins)-1)
	{
		roomsPoints = origins[i];
		roomsPointsDirections = directions[i];
		for (j in 0..Count(roomsPoints)-1)
		{
			o = roomsPoints[j];
			pointDirections = roomsPointsDirections[j];
			for (d in pointDirections)
			{
				a = RayBounce.ByOriginDirection(o,d,1,view)["points"][0];
			}
		}
	}
return a;
};

I am working on this code in Design script but this is taking very long to run, is there any way I can optimise this code to work faster. I am looking into replication guides but this is very confusing so your help will be appreciated.

What is “very long” in this scenario and how many items are you iterating? The vast majority of execution time comes from the number of permutations, not the “efficiency” of the code, although ray bounce is probably a more resource intensive operation than most. It would still be helpful to see your inputs.

I have 40 origin points and 12 directions for each origin points, these numbers might increase that is why I want this to work faster. I am having trouble setting the lacing in the node so I wanted to use the code block, but depending on the results I do feel the node is working faster (i am not sure about this though) that is why I wanted to know this, and get a few of the concepts cleared as well with designscript (I find replication documentation from the guide hard to understand)

chat gpt says that the following will suffice as a replacement so that each direction for its respective origin point will be dealt with please guide if its correct

results = 
    RayBounce.ByOriginDirection(
        origins<1><2>,       // replicate for rooms and points
        directions<1><2><3>, // replicate for each direction
        1, 
        view
    );

finalPoints =
    List.FilterByBoolMask(
        results["points"],
        results["elements"] == monument
    );

We can’t say without seeing your inputs. I would bet it would be faster to ensure all the correct pairings of inputs and then just use the node without lacing or list levels, but again, we can’t say if we don’t know what your inputs are.

can you identify the problem with the following code

origins;
directions;
view;
monument;
a = null;
finalPoints = [];
[Imperative]
{
	for (i in 0..Count(origins)-1)
	{
		l1 = [];
		roomsPoints = origins[i];
		roomsPointsDirections = directions[i];
		for (j in 0..Count(roomsPoints)-1)
		{
			l2 = [];
			o = roomsPoints[j];
			pointDirections = roomsPointsDirections[j];
			for (d in pointDirections)
			{
				a = RayBounce.ByOriginDirection(o,d,1,view);
				if(a["elements"][0] == monument)
				{
					List.AddItemToEnd(a["points"][0],l2);
				}
			}
			List.AddItemToEnd(l2,l1);
		}
		List.AddItemToEnd(l1,finalPoints);
	}
return finalPoints;
};

its not giving any result, I want to be able to compare results from this and the replication version

Again, we can’t really tell you what’s wrong if we don’t know what you’re feeding the node. It’s also really inconvenient to try and work through code with no values to test. Share a screenshot that includes the inputs to your node and the output, even if it’s null or empty. Just from your original code to this version it seems like you added a new list level to your data. Is that because you changed your data structure? Was the structure correct before and incorrect now or is it the reverse? We have no idea what “correct” is when we have no idea what you’re feeding your code.

It’s probably best to start with a small example model with known conditions that you can build and test against. That way you get the logic working in a manageable environment before trying to scale it to a real project where it may be slow.

I am feeding this as the origin and

this is the input for directions

Raybounce is never going to be ‘fast’, and you’re doing 2278 directions so a total of… well a lot more than 8 directions per point (would be 320 directions).

So Instead of jumping on optimizing the ray tracing, let’s first ensure you’re doing something which needs to be done.

  1. Why are you ray tracing?
  2. Have you confirmed the directions are correct for each point? Instead of using a real project, build a sample which is similar in nature but has 3 points to test (36 directions). That will be a data set where you can conceptualize, visualize, and validate the results in a meaningful way.
1 Like

On top of everything that Jacob has said, I can get the out of the box node to work with your existing list structure (on a much smaller example), so I don’t see the need to complicate this with an imperative block anyway. Try using the node once you have a small working example.