# GDScript functions calls slow

Attention Topic was automatically imported from the old Question2Answer platform.

Hi everyone!

I have an algorithm that requires performance. Firstly I wrote it with GDScript and noticed that hiding operations in helper functions makes it much slower.
I understand that functions calls have an additional overhead, but with functions, the algorithm completes in ~ 60 milliseconds, without them in just ~ 17.

Maybe there are some ways to get rid of such a strong influence of functions calls on performance?

Now it depresses me every time I want to put some piece of code in a function to make it more readable and reusable.

Here is an example:

``````func _get_next(point: Vector2, dir: Vector2) -> Vector2:
var next = point + dir
if (
dir == Vector2.RIGHT and _get_state(point, RIGHT) and _get_state(next, LEFT)
or dir == Vector2.LEFT and _get_state(point, LEFT) and _get_state(next, RIGHT)
or dir == Vector2.DOWN and _get_state(point, DOWN) and _get_state(next, UP)
or dir == Vector2.UP and _get_state(point, UP) and _get_state(next, DOWN)
):
return next
return Vector2.ZERO
``````

and without _get_state():

``````func _get_next(point: Vector2, dir: Vector2) -> Vector2:
var next = point + dir
if (
dir == Vector2.RIGHT and matrix[point.y][point.x] & (1 << RIGHT) and matrix[next.y][next.x] & (1 << LEFT)
or dir == Vector2.LEFT and matrix[point.y][point.x] & (1 << LEFT) and matrix[next.y][next.x] & (1 << RIGHT)
or dir == Vector2.DOWN and matrix[point.y][point.x] & (1 << UP) and matrix[next.y][next.x] & (1 << DOWN)
or dir == Vector2.UP and matrix[point.y][point.x] & (1 << DOWN) and matrix[next.y][next.x] & (1 << UP)
):
return next
return Vector2.ZERO
``````