I recently added a macro profiler to the macro-debugger package. The
macro profiler measures the increase in code size due to macro
expansion. (It doesn't measure expansion time.) See the docs for an
explanation of how the profiler counts direct and indirect costs.

The profiler is a raco subcommand; run it on a module with

  raco macro-profiler path/to/module.rkt

The macro profiler shows what macros contribute the most to the size
of the expanded program. The basic principles of profiling apply:
Improvements have the greatest impact for high-cost macros; on the
other hand, cost does not necessarily imply waste.

Reducing a macro's generated code size generally involves extracting
parts of the macro template as helper functions. For example, the
following macro definition

  (define-syntax-rule (capture-output body ...)
    (let ([sp (open-output-string)])
      (parameterize ((current-output-port sp))
        body ...
        (get-output-string sp))))

can be rewritten as

  (define (call/capture-output proc)
    (let ([sp (open-output-string)])
      (parameterize ((current-output-port sp))
        (proc)
        (get-output-string sp))))
  (define-syntax-rule (capture-output body ...)
    (call/capture-output (lambda () body ...)))

Macro-defining macros can sometimes be rewritten to use compile-time
helper functions too, if the generated macro's patterns don't
change. For example:

  (define-syntax-rule (define-method method-id method-impl)
    (define-syntax (method-id stx)
      (syntax-case stx ()
        [(_ arg ...)
         #'(method-impl this arg ...)])))

can be rewritten as

  (begin-for-syntax
    (define ((make-method-transformer method-impl-stx) call-stx)
      (syntax-case call-stx ()
        [(_ arg ...)
         (with-syntax ([method-impl method-impl-stx])
           #'(method-impl this arg ...))])))
  (define-syntax-rule (define-method method-id method-impl)
    (define-syntax method-id (make-method-transformer #'method-impl)))

If you have macros that make programs compile to large zo files, try
the macro profiler and see if it helps you find opportunities to
reduce code size. Please post any questions, suggestions, or success
(or failure) stories (or send them directly to me if you prefer). I'd
like to incorporate the responses into a macro performance guide.

Thanks,
Ryan

--
You received this message because you are subscribed to the Google Groups "Racket 
Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-dev/5756455B.40802%40ccs.neu.edu.
For more options, visit https://groups.google.com/d/optout.

Reply via email to