10.8 Black-Box Procedure

► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

▼ | 10 | Control Flow

10.1 | Multiple Values
10.2 | Exceptions
10.3 | Delayed Evaluation
10.4 | Continuations
10.5 | Continuation Marks
10.6 | Breaks
10.7 | Exiting
10.8 | Black-Box Procedure
10.9 | Unreachable Expressions

On this page:

black-box

Racket
top contents ← prev up next →

### 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.

> procedure (black-box v) → any/c
> v : any/c

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.

top contents ← prev up next →