[racket] Small and short-lived data structures
Hi everyone,
I am facing a situation quite frequent in scientific computing: the
use of small and often short-lived data structures, where the overhead
of construction, element access, and garbage collection can easily
dwarf the cost of computation.
The classical example is Geometry in 3D space. Define a 3d vector as
(struct v3d (x y z))
and then a few elementary operations such as
(define (vadd p1 p2)
(v3d (+ (v3d-x p1) (v3d-x p2))
(+ (v3d-y p1) (v3d-y p2))
(+ (v3d-z p1) (v3d-z p2))))
Geometric formulas written in terms of these operations then create
lots of short-lived intermediate results, and probably waste many CPU
cycles in wrapping and unwrapping number triples. Ideally, a compiler
would inline all the functions and then optimize away the intermediate
results.
I wonder how to handle such computations most efficiently in Racket.
The internal representation would not be exposed to clients, so it can
be chosen for performance. What's the best option? Structures? Vectors?
Maybe even lists? Or yet something else? Would it help to move to
Typed Racket and declare the fields as numbers, or even flonums?
Konrad.