Skip to content

How to make handles to cube class both thread safe and efficient? #419

@IAmNotHanni

Description

@IAmNotHanni

We are passing around a lot of pointers, smart pointers or const references to cubes in our code. This is necessary, as it's a major part of our engine. For example I'm working on the new octree collision (see upcoming pull request later) which looks like this:

std::optional<RayCubeCollision<Cube>> ray_cube_collision_check(const std::shared_ptr<Cube> cube, const glm::vec3 pos, const glm::vec3 dir, const std::optional<std::uint32_t> grid_level_counter) 

So in this code I am passing the cube as std::shared_ptr. This is fine, but clang-tidy suggests to pass it as const reference to a shared pointer. Jason Turner and Scott Meyers however say that this is not a good idea.

We have the following options:

  • Pass it as const reference
  • Pass it as a raw pointer
  • Pass it as a std::shared_ptr
  • Pass it as a std::weak_ptr

Notes:

  • We do not want to transfer ownership of the cube, so passing by value and using std::move is not an option in my opinion.
  • Copying data could be costly
  • We need to copy the data though, because if we pass it as const reference, the data could become invalid while the function body is being executed.

I think this is a common question which will is relevant for many code parts.
What do you guys propose?

Metadata

Metadata

Assignees

No one assigned

    Labels

    feat:concurrencymultithreading, asynchronous events, concurrencyfeat:octreeoctree, cube computations

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions