tag:blogger.com,1999:blog-5743983044224833668.post7551013360133553948..comments2023-03-30T02:13:37.406-07:00Comments on Fatvat: Ray Tracing in Clojure (Part II)Jeffhttp://www.blogger.com/profile/08195722595923882332noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-5743983044224833668.post-27618653363668324432009-08-15T23:14:58.493-07:002009-08-15T23:14:58.493-07:00Hello again, I added some features and moved my pr...Hello again, I added some features and moved my project to http://cray.googlecode.com/Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5743983044224833668.post-21070478042044954712009-07-25T02:20:19.102-07:002009-07-25T02:20:19.102-07:00Oops, direct link to source:
http://tu.be/graphic...Oops, direct link to source:<br /><br />http://tu.be/graphics/tracer.zip<br /><br />The code is still a little rough, being the first thing I have written in Clojure.<br /><br />You can see my functions that incorporate those techniques in tracer.clj.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-5743983044224833668.post-53488159441050619532009-07-25T00:35:57.542-07:002009-07-25T00:35:57.542-07:00That looks really cool! I got a 404 when I tried ...That looks really cool! I got a 404 when I tried to download the source though?<br /><br />I definitely didn't have performance in mind when I wrote this code (it was more or less a literal translation from a book).<br /><br />Great tips on performance optimization too so thanks for those!<br /><br />I've looked at using <a href="http://www.fatvat.co.uk/2009/05/jvisualvm-and-clojure.html" rel="nofollow">JVisualVM</a> for profiling, it's really good for seeing what's taking the time.<br /><br />Thanks for reading this blog!<br /><br />jeffJeffhttps://www.blogger.com/profile/08195722595923882332noreply@blogger.comtag:blogger.com,1999:blog-5743983044224833668.post-5351975183314117982009-07-24T21:49:11.538-07:002009-07-24T21:49:11.538-07:00Hi there. I have also written a ray tracer in Cloj...Hi there. I have also written a ray tracer in Clojure as a learning exercise. You can see sample images and download the source from http://tu.be/graphics<br /><br />In case you are interested, here are some techniques for numerical performance improvement in Clojure code. These techniques gave my ray tracer a 25-30% speed boost over storing data in structmaps/vectors, and a 5-20x boost for the routines themselves.<br /><br />1. Use double-array or float-array to store the data (points, vectors, etc).<br />2. Use aset to store the data in the arrays (faster than aset-double/float). Write macros that use aget to access to the data.<br />3. Use (double) or (float) to cast values that will be put in a vector into that type (speeds creation of new vectors, allows use of aset instead of aset-double).<br />4. Use type hints for any routine that takes arrays of primitives as arguments e.g. (defn vec-op [#^floats vec1 #^floats vec2] ... )<br />5. Use nested arithmetic e.g. (+ a (+ b c)) instead of (+ a b c)<br /><br />(There are most likely other tricks, like using mutable data to reduce the number of object creations).<br /><br />Using those techniques together avoids unboxing/boxing overhead, reduces vector/point/etc creation time, and allows the use of primitive arithmetic. This should get you about the same speed as writing the code in Java. <br /><br />I also recommend using a profiler, as the bottlenecks are often in non-intuitive places. For example, I found that using macros for functions like dot-product sometimes reduced performance. This may be by making the code larger and less able to fit in the CPU cache, or because the Java runtime can better optimize performance if the code is isolated in one function that is called many times, or some other reason...<br /><br />For multi-threading, I used agents, but I think the results are about the same.<br /><br />These are just my findings so far, and I am interested to learn any improvements on these techniques.Anonymousnoreply@blogger.com