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. The compiler
takes into account memory used by a computation, including values that
the computation keeps reachable, only to the degree that it will not
increase the asymptotic memory use of a program, but it may remove or
reorder computations in a way that reduces memory use. The
black-box function inhibits many of these optimizations
without adding additional overhead.
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.