Continuing the discussion from Ifelse vs ternary with tuples:
Julia has a few “pragma macros” already and I agree more would be nice. The current collection, such as @inline
and @inbounds/@boundscheck
, focus on compiler hints/directives, and @nobranch
would be a good addition to that.
I’ve seen some issues suggesting that there’s active work on making the compiler more “pluggable” such that this sort of thing could be provided on a package basis.
I’d love to see a collection of these which function as guarantees of behavior, such that they throw compiler errors if the pragma can’t apply. Things like @typestable
as a method annotation, @jumptable
for a chained if-statement which throws an error if that chain can’t be turned into a switch or computed goto, and an annotation for array/vector types which asserts that they’ll be composed of the data, no pointers (don’t have a great name for this one, @referencefree
can probably be improved upon). @allocationlimit 5
throws an error if that number of allocations is exceeded, I think that should be possible to determine during codegen, at least if we define “compiler can’t put a number on allocations” as failing the test.
Getting Julia to exhibit systems-level performance is a bit of an art form, which it kinda has to be, and it’s a strength of the language that it will do what you ask even when it can’t emit optimal machine code. A consequence of this is that tuned-up code is somewhat fragile to any changes. That’s inherent to the language design space Julia occupies, and it would be nice to have more language-level support for turning performance regressions into errors.
Some of what I want here, maybe all of it, would be easier to provide as an ergonomic set of macros intended for the test suite. Not better imho, the best place to assert that a method is type stable is in the definition of that method, but it would do the job just fine, and I could see it avoiding some compile-time knowledge problems to do it that way.