16 Unsafe Operations
All fuctions and forms provided by racket/base and
racket check their arguments to ensure that the
arguments conform to contracts and other constraints. For example,
vector-ref checks its arguments to ensure that the first
argument is a vector, that the second argument is an exact integer,
and that the second argument is between 0 and one less than
the vector’s length, inclusive.
Functions provided by racket/unsafe/ops are
unsafe. They have certain constraints, but the constraints
are not checked, which allows the system to generate and execute
faster code. If arguments violate an unsafe function’s constraints,
the function’s behavior and result is unpredictable, and the entire
system can crash or become corrupted.
All of the exported bindings of racket/unsafe/ops are
protected in the sense of protect-out, so access to unsafe
operations can be prevented by adjusting the code inspector (see
Code Inspectors).
16.1 Unsafe Numeric Operations
For
fixnums: Like
+,
-,
*,
quotient,
remainder,
modulo, and
abs, but constrained to consume
fixnums and produce a
fixnum result. The mathematical operation on
a and
b must be representable as a
fixnum. In the case of
unsafe-fxquotient,
unsafe-fxremainder, and
unsafe-fxmodulo,
b must not be
0.
For
fixnums: Like
bitwise-and,
bitwise-ior,
bitwise-xor,
bitwise-not, and
arithmetic-shift, but constrained to consume
fixnums;
the result is always a
fixnum. The
unsafe-fxlshift and
unsafe-fxrshift operations correspond to
arithmetic-shift, but require non-negative arguments;
unsafe-fxlshift is a positive (i.e., left) shift, and
unsafe-fxrshift is a negative (i.e., right) shift, where the
number of bits to shift must be less than the number of bits used to
represent a
fixnum. In the case of
unsafe-fxlshift,
bits in the result beyond the the number of bits used to represent a
fixnum are effectively replaced with a copy of the high bit.
Unchecked version of
->fl.
16.2 Unsafe Data Extraction
Unsafe field access and update for an instance of a structure
type. The index
k must be between
0 (inclusive) and
the number of fields in the struture (exclusive). In the case of
unsafe-struct-set!, the field must be mutable.