I work on a program that performs matrix and vector operation in Java. The multiple function call and object creation that occurs in my current implementation makes it sluggish and difficult to understand.
For instance I want to update the position of a mechanical point, by speed integration:
void update(Vector3 position, Vector3 speed, float dt){
Vector3 displacement = new Vector3(speed);
displacement.assignMul(dt);
position.assignAdd(displacement);
}
Here the API is not natural, and in addition I need to allocate an build a new Vector3 reference. Obviously i measured a great performance improvement on real uses case when inlining the computation this way:
void update(Vector3 position, Vector3 speed, float dt){
position.x += speed.x * dt;
position.y += speed.y * dt;
position.z += speed.z * dt;
}
Is there any tool that could generate this code from a domain specific language on demand? A Cog like syntax would be nice. (Cog is a code generation tool from Ned Batchelder)
void update(Vector3 position, Vector3 speed, float dt){
// [[[DSL position += speed * dt ]]]
position.x += speed.x * dt;//Generated Code
position.y += speed.y * dt;//Generated Code
position.z += speed.z * dt;//Generated Code
// [[[END]]]
}