Racket
10.8 Black-Box Procedure🔗ℹ
As Racket programs are compiled (see Controlling and Inspecting Compilation), the
compiler may reorder or even remove pure computations that
have no visible effect. For compilation purposes, the time needed to
perform a computation is not considered a visible effect, so even a
long-running computation may be eliminated if its result is not used.
The compiler may also reorder expression evaluation in a way that
reduces asymptotic memory use. The black-box function
inhibits many of these optimizations without adding additional
overhead.The compiler may reorder evaluation in a way
that increases memory use by a small constant factor, but it will not
reorder in a way that increases asymptotic memory use (e.g., causing a
recursion of variable depth N to use O(N) memory when it
should an amount of memory that is independent of N).
Returns v.
As far as the Racket compiler is concerned, black-box returns
an unknown value, and it has a side effect involving v, which
means that a call to black-box or its argument cannot be
eliminated at compile time, and its evaluation cannot be reordered
with respect to other side effects.
Examples:
| > (let ([to-power 100]) | | (let loop ([i 1000]) | | (unless (zero? i) | | ; call to `expt` is optimized away entirely, since | | ; there's no effect and the result is unused: | | (expt 2 to-power) | | (loop (sub1 i))))) |
|
| > (let ([to-power 100]) | | (let loop ([i 1000]) | | (unless (zero? i) | | ; call to `expt` is optimized to just returning a folded | | ; constant, instead of calling `expt` each iteration: | | (black-box (expt 2 to-power)) | | (loop (sub1 i))))) |
|
| > (let ([to-power (black-box 100)]) | | (let loop ([i 1000]) | | (unless (zero? i) | | ; in safe mode, calls `expt`, because `to-power` is not | | ; known to be a number; optimized away in unsafe mode: | | (expt 2 to-power) | | (loop (sub1 i))))) |
|
| > (let ([to-power (black-box 100)]) | | (let loop ([i 1000]) | | (unless (zero? i) | | ; arithmetic really performed every iteration, since the | | ; `to-power` value is assumed unknown, and the `expt` | | ; result is assumed to be used, even in unsafe mode: | | (black-box (expt 2 to-power)) | | (loop (sub1 i))))) |
|
Added in version 8.18.0.17 of package base.