# Is typed racket compilation slow when using prefab structs?

**URL:** https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904
**Category:** Questions & Answers
**Tags:** typed-racket
**Created:** [May 1, 2023, 6:45pm UTC](https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904 "2023-05-01T18:45:55Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![sebas](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@sebas](https://racket.discourse.group/u/sebas)
#### Post date: [May 1, 2023, 6:45pm UTC](https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904/1 "2023-05-01T18:45:55Z")

</div>

I was trying to write an AST using something like this:

(define-type Exp (U Const Op ...))  
(struct Const (...) #:prefab)

without prefab it is ok, but when I added prefab it takes more than a minute from clicking Run to getting a cursor in the interactions window. I tried typed/racket/optional but it is the same.

Does this look like a bug or is it normal because of the stuff that prefab adds?

---

<div class="post-metadata">

### Author: ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)
#### Post date: [May 1, 2023, 7:37pm UTC](https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904/2 "2023-05-01T19:37:25Z")

</div>

Can you give the code that is slow so I can test it out?

---

<div class="post-metadata">

### Author: ![sebas](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@sebas](https://racket.discourse.group/u/sebas)
#### Post date: [May 1, 2023, 7:50pm UTC](https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904/3 "2023-05-01T19:50:38Z")

</div>

Sure, just this file after adding the prefabs and clicking run on the editor goes from 2 secs to minutes.

```plaintext
#lang typed/racket

(provide J-Exp J-Num J-Boolean J-Var J-Plus J-Eq J-Not J-Read
         J-Com J-If J-While J-Assign J-Output J-Seq)

(define-type J-Exp (U J-Num J-Boolean J-Plus J-Read J-Eq J-Not J-Var))
(struct J-Num ([value : Integer]) #:prefab)
(struct J-Boolean ([value : Boolean]) #:prefab)
(struct J-Var ([value : Symbol]) #:prefab)
(struct J-Plus ([left : J-Exp] [right : J-Exp]) #:prefab)
(struct J-Eq ([left : J-Exp] [right : J-Exp]) #:prefab)
(struct J-Not ([value : J-Exp]) #:prefab)
(struct J-Read () #:prefab)

(define-type J-Com (U J-If J-While J-Assign J-Output J-Seq))
(struct J-If ([cond : J-Exp] [then : J-Com] [else : J-Com]) #:prefab)
(struct J-While ([cond : J-Exp] [action : J-Com]) #:prefab)
(struct J-Assign ([var : Symbol] [value : J-Exp]) #:prefab)
(struct J-Output ([value : J-Exp]) #:prefab)
(struct J-Seq ([left : J-Com] [right : J-Com]) #:prefab)

```

What is slow is the compilation itself, not the execution. I was not very clear on the post.

---

<div class="post-metadata">

### Author: ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)
#### Post date: [May 1, 2023, 8:23pm UTC](https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904/4 "2023-05-01T20:23:34Z")

</div>

Runs out of memory for me before it finishes compiling in DrRacket. Ouch.

---

<div class="post-metadata">

### Author: ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)
#### Post date: [May 1, 2023, 8:30pm UTC](https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904/5 "2023-05-01T20:30:09Z")

</div>

Thanks. It looks like the compilation time is dominated by generating contracts for the functions you're providing. If you provide fewer structs compilation is much faster. (Obviously that's not good, but it might be a useful workaround for you.)

---

<div class="post-metadata">

### Author: ![sebas](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@sebas](https://racket.discourse.group/u/sebas)
#### Post date: [May 1, 2023, 8:49pm UTC](https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904/6 "2023-05-01T20:49:12Z")

</div>

yeah, what I'm doing is removing the "#:prefab"s, so it compiles fast again, and I'll write a separate printer function when I need it. It was cool to have an auto-magical serializer, but it's not a problem.

---

<div class="post-metadata">

### Author: ![benknoble](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/benknoble/32/16_2.png) [@benknoble](https://racket.discourse.group/u/benknoble)
#### Post date: [May 1, 2023, 9:12pm UTC](https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904/7 "2023-05-01T21:12:46Z")

</div>

Perhaps `serializable-struct` works in Typed Racket?

---

<div class="post-metadata">

### Author: ![sebas](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@sebas](https://racket.discourse.group/u/sebas)
#### Post date: [May 2, 2023, 6:11am UTC](https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904/8 "2023-05-02T06:11:02Z")

</div>

I think not. It doesn't have a typed module and it doesn't support the normal (for typed racket) field typing syntax.

---

<div class="post-metadata">

### Author: ![greghendershott](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/greghendershott/32/98_2.png) [@greghendershott](https://racket.discourse.group/u/greghendershott)
#### Post date: [May 2, 2023, 6:09pm UTC](https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904/9 "2023-05-02T18:09:42Z")

</div>

What's special about `#:prefab` here?

At first I guessed you mean contracts for all the struct accessor functions -- in which case `#:transparent` would also be a problem.

But `#:transparent` expands quickly. And either way there is no `struct-out` involved so I'm wondering contracts for what purpose?

(Obviously I should just look at the fully expanded code, but.... 😄)

---

<div class="post-metadata">

### Author: ![samth](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/samth/32/3_2.png) [@samth](https://racket.discourse.group/u/samth)
#### Post date: [May 2, 2023, 6:24pm UTC](https://racket.discourse.group/t/is-typed-racket-compilation-slow-when-using-prefab-structs/1904/10 "2023-05-02T18:24:12Z")

</div>

The problem is that contracts for prefabs have to check the field types, rather than just relying on the predicates. Since anyone can construct an instance of those prefab types, the contracts have to walk the entire value to check that it matches the type.

I think what's going on is that Typed Racket effectively inlines the entire types into each contract so the generated code (and the runtime to generate that code) is very large. Typed Racket could definitely do better here, but the relevant code is pretty tricky.
