Post by thhe on Mar 21, 2022 16:03:35 GMT
Hey,
i currently finished Chapter 11 and I wanted to share some tips.
I am using Typescript/Javascript for the challenge, one thing to know about Javascript that its a garbage collected
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)
- The javascript array has some performance pitfalls. There are a lot of functions that are convenient but slow, filter() and map() for example return a new array, which is costly in terms of garbage collection, so it is better to avoid. It is the same for other programming languages.
- 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.
Greetings,
Thomas