# Macro optimisation in Racket

**URL:** <https://racket.discourse.group/t/macro-optimisation-in-racket/2342>\
**Category:** General\
**Created:** [September 27, 2023, 8:50am UTC](https://racket.discourse.group/t/macro-optimisation-in-racket/2342 "2023-09-27T08:50:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [September 27, 2023, 8:50am UTC](https://racket.discourse.group/t/macro-optimisation-in-racket/2342/1 "2023-09-27T08:50:51Z")

</div>

hello,

i need to optimise some scheme code i wrote.  
I can do that by "inlining macro expansion" and in other case by doing both and substituing procedure symbolic result instead of re-calculus all the numeric values at each step of loop for example.

About macros my question with Racket is :  
if a macro is expanding to a result, is it done before computation only one time in the code execution ,at beginning, or is it done each time (for example in a loop) the macro call is encountered?

it changes a lot, if it is done first, i have no need of optimise that, execept for macro that call a procedure that can be symbolically optimise! that another case too...

example of the general case:

```scheme
{x <+ 7}

```

is expanded by SRFI 105 reader as:

```scheme
(<+ x 7)

```

this is done before execution by Raket SRFI-105 curly infix reader

now

the above expression is expanded as:

```scheme
(define x 7)

```

by evaluation of \<+ macro but if (\<+ x 7) is in a loop is it expanded at each iteration of the loop or as it been done definitively at start of the program?

note : for \<+ this is a simple case but i have case more complex (macro evaluating to procedure calls)

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [September 27, 2023, 9:00am UTC](https://racket.discourse.group/t/macro-optimisation-in-racket/2342/2 "2023-09-27T09:00:41Z")

</div>

Macro expansion happens at compile time.

See the big picture section:  
[https://soegaard.github.io/mythical-macros/#1.3](https://soegaard.github.io/mythical-macros/#1.3)

---

<div class="post-metadata">

**Author:** ![damien\_mattei](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/damien_mattei/32/2706_2.png) [@damien\_mattei](https://racket.discourse.group/u/damien_mattei)\
**Post date:** [September 27, 2023, 9:24am UTC](https://racket.discourse.group/t/macro-optimisation-in-racket/2342/3 "2023-09-27T09:24:43Z")

</div>

thanks. great explanation. it is what i was thinking but i had doubt.  
and furthermore i find interesting API Racket function to reuse on your page.

i will skip time doing what is automatically done by racket as optimisation and focus on the infix precedence optimisation in my Scheme+ code that are done at runtime and should be done symbolically at first time because the calculus does not change in a loop ,only numerical values changes.
