[plt-dev] performance-oriented unsafe operations (v4.2.1.8)

From: Jon Rafkind (rafkind at cs.utah.edu)
Date: Sun Sep 6 15:44:52 EDT 2009

Matthew Flatt wrote:
> Version (now in SVN) starts an experiment with
> performance-oriented unsafe operations.
> The new `scheme/unsafe/ops' library provides operations such as
> `unsafe-vector-ref' and `unsafe-fl+' (for inexact real addition) that
> are inlined by the JIT without contract checks. If a call violates the
> contract of a function from `scheme/unsafe/ops', arbitrarily bad
> things can happen. As long as contracts are satisfied, however,
> performance should be a little better than with corresponding safe
> operations.
> As part of the experiment, the expansion of `for' with `in-range' and
> `in-vector' now uses unsafe operations (in a safe way). For example,
> `in-vector' uses `unsafe-vector-ref' as well as a fixnum comparison
> for detecting when it has reached the end of a vector; it also uses
> unsafe fixnum arithmetic to increment the index as long as the step is
> 1 or -1 (in which case no overflow can occur during the
> increment). These improvements make a 10-20% difference in a tight
> loop over a vector.
I get about a 25% speedup. dual-core amd64 running with a 32-bit ubuntu.

cpu time: 1040 real time: 1046 gc time: 72
cpu time: 1064 real time: 1089 gc time: 132
cpu time: 1000 real time: 1005 gc time: 8
cpu time: 940 real time: 954 gc time: 12
cpu time: 940 real time: 947 gc time: 0
cpu time: 748 real time: 749 gc time: 20
cpu time: 732 real time: 738 gc time: 4
cpu time: 776 real time: 782 gc time: 44
cpu time: 736 real time: 749 gc time: 12
cpu time: 744 real time: 743 gc time: 8

(for/fold ([sum 0]) ([i (in-range 4000000)]) (+ sum i))

Posted on the dev mailing list.