ultimaille icon indicating copy to clipboard operation
ultimaille copied to clipboard

code-review vec2 and vec3, are there performance issues for operator[] ?

Open ssloy opened this issue 5 years ago • 1 comments

ssloy avatar Jan 31 '21 11:01 ssloy

Current implementation is entirely standard compliant; however accessing vec.z needs two comparisons.

Alternative A

glm-style return (&x)[i]; https://github.com/Groovounet/glm/blob/e1afbc9ceaacbc9aed7bb896d26c0872f8a2bf29/glm/core/type_vec3.inl#L49

vec3 can be converted to "plain old data" (POD) struct (need to remove custom constructors though). Conditions to qualify for POD:

  1. All data members are public and themselves POD or fundamental types (but not reference or pointer-to-member types), or arrays of such
  2. It has no user-defined constructors, assignment operators or destructors
  3. It has no virtual functions
  4. It has no base classes

Problem: padding bytes.

Alternative B

Another option is union. Technically it is UB, but fine in most compilers.

ssloy avatar Jan 31 '21 11:01 ssloy