Post by thhe on Mar 21, 2022 16:03:35 GMT
i currently finished Chapter 11 and I wanted to share some tips.
language. Garbage collection is a resource heavy process that should be avoided if possible. Some solutions:
- Reuse objects whenever possible, instead of creating new ones. One pattern that comes in handy is object pool (https://en.wikipedia.org/wiki/Object_pool_pattern)
- I refactored the Matrix class, so that ist possible to reuse matrixes that arent needed any more. I pass them via an additional parameter to the functions in the Matrix class.
Some other tips:
- Its good to know the time complexity of the data structures you are using. On example is the java script array. Removing items and checking if an object is contained in the array, is slow. Sometimes a Set is more appropriate.
- The method in the book for calculation of the inverse is slow, it involves recursion and copying submatrixes. Its not necessary, because the book uses 4*4 matrixes exclusively, so the algorithm uses a static size anyway, which makes optimization possible. What I did is turning the algorithm into a code generator (https://github.com/P3N9U1N/TheRaytracerChallenge/blob/master/spikes/chapter3/test.ts), which will generate formulas for fixed size matrixes.
I hope these tips are useful.