Type and range checking (MIT/GNU Scheme 10.1.1)

Next: Fixnum arithmetic, Previous: Global variables, Up: Efficiency Tips [Contents][Index]

#### 4.3.3 Type and range checking

The compiler inserts type (and range) checks so that e.g. applying `vector-ref` to a string (or an invalid index) signals an error. Without these checks, an in-lined `vector-ref` application will return garbage — an object with random type and address. At best, accessing any part of that object will produce an invalid address trap. At worst, the garbage collector is confused and your world is destroyed.

The compiler punts type and range checks when it can prove they are not necessary. Using “Better Predicates” helps (see Coding style), but many checks will remain. If you know a data structure will be read-only, a certain size, etc. many of the remaining checks can prove unnecessary. To make these decisions for yourself, you can turn off the compiler’s implicit checks. The following procedure definition ensures minimum flonum consing (i.e. none, see Flonum arithmetic) and maximum speed. It’s safe use is entirely up to you.

    (declare (usual-integrations) (integrate-operator %increment!))
    (define (%increment! v i)
      (declare (no-type-checks) (no-range-checks))
      (flo:vector-set! v i (flo:+ (flo:vector-ref v i) 1.)))

Here are the relevant declarations:

declaration: no-type-checks

  In-lined primitives within the block will not check their arguments’ types.

declaration: no-range-checks

  In-lined primitives within the block will not check that indices are valid.