bakpakin / Fennel

Lua Lisp Language
https://fennel-lang.org
MIT License
2.42k stars 124 forks source link

Add comparator generator for when all args are idempotent #439

Closed RobertCochran closed 1 year ago

RobertCochran commented 1 year ago

This adds an optimized comparator generator for when all of the arguments are literals or simple (non-multi) symbols (which almost certainly means a simple lexenv lookup). In this version, don't bother wrapping any of the comparison values in an anonymous function since evaluation of the args is idempotent.

This is actually a targeted fix for reducing the memory usage of char-starter?; I'm trying to run Fennel in a low-memory environment (<= 4MB) and when I tried profiling require("fennel") with https://github.com/pmusa/lmprof to see where I could find some optimization opportunities, this was one of the indicated hotspots. Creating and immediately throwing away anonymous functions is not super great for memory use, and I noticed that this was a compiler enhancement opportunity: recognize when you have idempotent values and elide the function.

I ran the tests and they still pass. I'm 99% confident that this won't break other code; I was pretty simplistic and conservative about what values were considered idempotent, but my understanding might be flawed or incomplete.

technomancy commented 1 year ago

Thanks! After re-running the fuzz testing it cleared up, so it's not related to this change. I've merged it on in!