chickadee » special-case

Premature optimization is the root of all evil!

These macros will double your code size! Use at your own risk!

_But_ if you need speed enough that you're going to write special cases anyway, using these macros will at least make that a little cleaner and more manageable.

(with-special-cased-procedures ((var predefined) ...) body ...)syntax


Defines the SRFI-1 `member' procedure with optional equality argument, but duplicates the inner loop conditioned on eq, special-casing one on the eq? case.

(define (member key ls . o)
  (let ((eq (if (pair? o) (car o) equal?)))
    (with-special-cased-procedures ((eq eq?))
      (let lp ((ls ls))
         ((null? ls)
         ((eq key (car ls))
          (lp (cdr ls))))))))

Defines a for-each variation that optionally writes trace output every 100 elements. Two versions of the inner loop get created, one with and one without the tracing call, so that there's no extra overhead when not tracing.

(define (for-each/trace proc ls . o)
  (with-special-cased-conditional (trace? (and (pair? o) (car o)))
    (let lp ((ls ls) (i 0))
      (if (and (trace?) (zero? (modulo i 100)))
          (print "iteration: " i))
       ((pair? ls)
        (proc (car ls))
        (lp (cdr ls) (+ i 1)))))))

Contents »