Skip to content

Re-introduce hitbox optimizations that were removed by hitbox rework #1781

@cspotcode

Description

@cspotcode

Enhancement request:

What should be added/changed?

Re-introduce get_adjusted_points optimizations from #1524, which were lost in the hitbox refactor.

What would it help with?

performance of collision detection


#1641 undid some of the get_adjusted_points optimizations from #1524

These code snippets have a lot of extra dictionary lookups and function calls for constant values which are repeated multiple times for every single point.

def _adjust_point(point) -> Point:
x, y = point
x *= self.scale[0]
y *= self.scale[1]
return (x + self.position[0], y + self.position[1])

def _adjust_point(point) -> Point:
x, y = point
x *= self.scale[0]
y *= self.scale[1]
if rad:
rot_x = x * rad_cos - y * rad_sin
rot_y = x * rad_sin + y * rad_cos
x = rot_x
y = rot_y
return (
x + self.position[0],
y + self.position[1],
)

For example, when computing adjusted points for an 8 point hitbox, these dictionary lookups and calls happen a lot:

  • self.position() called 16 times
  • self._position dictionary lookup 16 times
  • self.scale() called 16 times
  • self._scale dictionary lookup 16 times
  • _scale[0] 8 times
  • _scale[1] 8 times
  • _position[0] 8 times
  • _position[1] 8 times

#1524 stored position and scale x and y values in local variables.
https://github.com/pythonarcade/arcade/pull/1524/files#diff-8c15b2f64db68ce7a9f80e835102bdcb0790386bbc2972bdc154a43abcd8a2df

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions