On Mon, Oct 14, 2013 at 5:59 AM, Phillip Lord <phillip.l...@newcastle.ac.uk>wrote:
> <lop...@gmail.com> writes: > > Same trick as > Java -- optimise the check away at compile time. > > Indeed, this is how Clojure's assert works. > This is the way Clojure's assert works (by compiling away the assertion). This is not the way Java assertions work. Java assertions can be turned off at runtime with zero performance penalty using a command line flag. The way they work is: the body of the assert is made conditional on a static final field of the class, the static initializer for said class will set that static final field to true or false depending on the command line flag, at runtime the JIT compiler notices that the static final field is set to false and could never be set to true and it eliminates the body of the assert as dead code. Java assertions are not compiled away by the Java compiler at compile time. They are compiled away by the JIT compiler at runtime. Compiling assertions away at compile time isn't so bad, but there are two problems: 1) it would be nice to be able to enable/disable assertions at runtime like Java does, 2) there isn't actually a good way to tell the compiler to turn assertions off. About the best you can do is set! *assert* to false in your namespace, but this causes problems when the code is AOT'ed or loaded in any context where *assert* does not have a binding established. You could also alter-var-root *assert* in a user.clj file, which also has problems (like the wholesale global disabling of asserts for every namespace not to mention the non-composability of user.clj). > > I think that you are worried about the overhead unnecessarily, though. > The assert status is checked at macro expansion time. If per module > switching on and off is what then I would suggest that you build on top > of the existing assert. > There is a huge performance penalty for Clojure assertions, if you don't compile them away, and there isn't really an easy way to compile them away. I know this because I wrote a persistent datastructure in Java and rewrote it in Clojure comparing their performance (using assertions both times). It was because of the performance penalty of Clojure's assertions that I wrote the message to the clojure-dev mailing list (and also wrote my own assert macro that I could toggle easily). -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.