▼ |  | How to Program Racket

1 | Basic Facts of Life
2 | Testing
3 | Units of Code
4 | Choosing the Right Construct
5 | Textual Matters
6 | Language and Performance
7 | Retiquette: Branch and Commit
8 | Acknowledgment
9 | Todo List, Call for Contributions

► | 6 | Language and Performance

6.1 | Macros: Space and Performance

On this page:

6.1`` Macros: Space and Performance

top ← prev up next →

### 6`` Language and Performance

When you write a module, you first pick a language. In Racket you can choose a lot of languages. The most important choice concerns racket/base vs racket.

For scripts, use racket/base. The racket/base language loads significantly faster than the racket language because it is much smaller than the racket.

If your module is intended as a library, stick to racket/base. That way script writers can use it without incurring the overhead of loading all of racket unknowingly.

Conversely, you should use racket (or even racket/gui) when you just want a convenient language to write some program. The racket language comes with almost all the batteries, and racket/gui adds the rest of the GUI base.

#### 6.1`` Macros: Space and Performance

Macro copy code. Also, Racket is really a tower of macro-implemented languages. Hence, a single line of source code may expand into a rather large core expression. As you and others keep adding macros, even the smallest functions generate huge expressions and consume a lot of space. This kind of space consumption may affect the performance of your project and is therefore to be avoided.

When you design your own macro with a large expansion, try to factor it into a function call that consumes small thunks or procedures.

> > good
> > #lang racket
> > ...
> > (define-syntax (search s)
> > (syntax-parse s
> > [(_ x (e:expr ...)
> > (~datum in)
> > b:expr)
> > #'(sar/λ (list e ...)
> > (λ (x) b))]))
> > (define (sar/λ l p)
> > (for ((a '())) ((y l))
> > (unless (bad? y)
> > (cons (p y) a))))
> > (define (bad? x)
> > ... many lines ...)
> > ...
> > | bad
> > #lang racket
> > ...
> > (define-syntax (search s)
> > (syntax-parse s
> > [(_ x (e:expr ...)
> > (~datum in)
> > b:expr)
> > #'(begin
> > (define (bad? x)
> > ... many lines ...)
> > (define l
> > (list e ...))
> > (for ((a '())) ((x l))
> > (unless (bad? x)
> > (cons b a))))]))

As you can see, the macro on the left calls a function with a list of the searchable values and a function that encapsulates the body. Every expansion is a single function call. In contrast, the macro on the right expands to many nested definitions and expressions every time it is used.

top ← prev up next →