B
BGB
sorry if I am missing something obvious here, but I am hoping for
suggestions on a relatively efficient general-purpose way to handle
vector math and similar (in the sense of geometric vectors...).
I will likely be writing any code myself, so the request is for ideas
for a general design.
this is for a personal project (most of the rest of the project uses C,
but this will be for the Java part of the project). Java isn't really my
main language FWIW...
purposes and requirements:
performance should be good, and interface should also be nice (if possible);
the app will be soft real-time on a custom JVM (JavaME style), and the
GC is slow and so avoiding GC (by minimizing garbage) is preferable (so
user will not experience stalls);
it doesn't need to deal with arbitrary N-sized vectors, only 2, 3, and 4
element vectors (XYZW), and also Quaternions;
likely vector size and usage will be considered as part of the type (a
Vec2 is not a Vec3 which is not a Vec4 which is not a Quat);
the vectors will potentially be recalculated frequently and in large
numbers;
....
so, a few possibilities:
A, immutable vectors:
pros: clean interface, supports merging and sharing vectors, ...
cons: many vectors may be created, likely creating lots of garbage.
B, mutable vectors:
pros: likely to reduce garbage (most recalculation can reuse existing
vectors);
cons: vectors can't be shared (since they may change without warning),
and the interface is more awkward (vector values are copied, meaning
vectors need to be provided to use for storing temporary values, ...).
C, supporting both (as different types):
pros: can mix and match as needed;
cons: 2x or 3x as many types (mutable and immutable versions of each
size, 3x if both versions are subclasses of a common abstract type),
more awkward class names to distinguish them, more methods, ...
D, supporting both (as different operations on the same type):
pros: allows mixing and matching use-patterns (general);
cons: use-case ambiguity (need to infer intended use from context).
arrays and indices ("float[] vecs=new float[nvecs*4];" or similar):
pros: errm... likely compact memory...
cons: likely overly ugly/awkward.
I have been considering this matter over the past several days.
leaning at the moment is for option D.
example classes:
basic: Vec2, Vec3, Vec4, Quat
multi-typed (option C):
Vec2a, Vec3a, Vec4a, Quata //abstract
Vec2c, Vec3c, Vec4c, Quadc //constant/immutable
Vec2n, Vec3n, Vec4n, Quatn //mutable
Vec#c and Vec#n would be subtypes of Vec#a, so methods accepting the
abstract forms would accept both, with the mutable and immutable forms
differing in terms of their external behavior.
options B and D would likely use a "pool" for freed vectors of a given
type (garbage reduced by manually "freeing" spare vectors, whereas A
would likely try to do a lookup and on failure create a new vector).
matrix math may also be considered.
suggestions, opinions, or other options?...
suggestions on a relatively efficient general-purpose way to handle
vector math and similar (in the sense of geometric vectors...).
I will likely be writing any code myself, so the request is for ideas
for a general design.
this is for a personal project (most of the rest of the project uses C,
but this will be for the Java part of the project). Java isn't really my
main language FWIW...
purposes and requirements:
performance should be good, and interface should also be nice (if possible);
the app will be soft real-time on a custom JVM (JavaME style), and the
GC is slow and so avoiding GC (by minimizing garbage) is preferable (so
user will not experience stalls);
it doesn't need to deal with arbitrary N-sized vectors, only 2, 3, and 4
element vectors (XYZW), and also Quaternions;
likely vector size and usage will be considered as part of the type (a
Vec2 is not a Vec3 which is not a Vec4 which is not a Quat);
the vectors will potentially be recalculated frequently and in large
numbers;
....
so, a few possibilities:
A, immutable vectors:
pros: clean interface, supports merging and sharing vectors, ...
cons: many vectors may be created, likely creating lots of garbage.
B, mutable vectors:
pros: likely to reduce garbage (most recalculation can reuse existing
vectors);
cons: vectors can't be shared (since they may change without warning),
and the interface is more awkward (vector values are copied, meaning
vectors need to be provided to use for storing temporary values, ...).
C, supporting both (as different types):
pros: can mix and match as needed;
cons: 2x or 3x as many types (mutable and immutable versions of each
size, 3x if both versions are subclasses of a common abstract type),
more awkward class names to distinguish them, more methods, ...
D, supporting both (as different operations on the same type):
pros: allows mixing and matching use-patterns (general);
cons: use-case ambiguity (need to infer intended use from context).
arrays and indices ("float[] vecs=new float[nvecs*4];" or similar):
pros: errm... likely compact memory...
cons: likely overly ugly/awkward.
I have been considering this matter over the past several days.
leaning at the moment is for option D.
example classes:
basic: Vec2, Vec3, Vec4, Quat
multi-typed (option C):
Vec2a, Vec3a, Vec4a, Quata //abstract
Vec2c, Vec3c, Vec4c, Quadc //constant/immutable
Vec2n, Vec3n, Vec4n, Quatn //mutable
Vec#c and Vec#n would be subtypes of Vec#a, so methods accepting the
abstract forms would accept both, with the mutable and immutable forms
differing in terms of their external behavior.
options B and D would likely use a "pool" for freed vectors of a given
type (garbage reduced by manually "freeing" spare vectors, whereas A
would likely try to do a lookup and on failure create a new vector).
matrix math may also be considered.
suggestions, opinions, or other options?...