# Speed benefits of submodules?

**URL:** https://racket.discourse.group/t/speed-benefits-of-submodules/986
**Category:** Questions & Answers
**Created:** [May 8, 2022, 9:20pm UTC](https://racket.discourse.group/t/speed-benefits-of-submodules/986 "2022-05-08T21:20:08Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![joeld](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/joeld/32/38_2.png) [@joeld](https://racket.discourse.group/u/joeld)
#### Post date: [May 8, 2022, 9:20pm UTC](https://racket.discourse.group/t/speed-benefits-of-submodules/986/1 "2022-05-08T21:20:08Z")

</div>

Suppose I have two Racket files/modules:

```nohighlight
;; one.rkt

#lang racket

(provide a b)
(define foo 'simple-value)
(define bar (make-bytes 100000 65))

```

```nohighlight
;; two.rkt

#lang racket

(module small racket
  (provide foo)
  (define foo 'simple-value))

(provide bar)
(define bar (make-bytes 100000 65))

```

Is it any faster to `(require (submod "two.rkt" small))` than to `(require "one.rkt")` if all I want is `foo`? That is, is there any performance benefit (or penalty) at runtime in using submodules, even though they are compiled “inside” their containing modules?

---

<div class="post-metadata">

### Author: ![simonls](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/simonls/32/170_2.png) [@simonls](https://racket.discourse.group/u/simonls)
#### Post date: [May 8, 2022, 9:35pm UTC](https://racket.discourse.group/t/speed-benefits-of-submodules/986/2 "2022-05-08T21:35:08Z")

</div>

[Submodules](https://docs.racket-lang.org/guide/Module_Syntax.html#%28part._submodules%29)

> Running a module does not necessarily run its submodules. In the above example, running "park.rkt" runs its submodule zoo only because the "park.rkt" module [require](https://docs.racket-lang.org/reference/require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._require%29%29)s the zoo submodule. Otherwise, a module and each of its submodules can be run independently. Furthermore, if "park.rkt" is compiled to a bytecode file (via raco make), then the code for "park.rkt" or the code for zoo can be loaded independently.
> 
> Submodules can be nested within submodules, and a submodule can be referenced directly by a module other than its enclosing module by using a [submodule path](https://docs.racket-lang.org/guide/module-paths.html#%28elem._submod%29).

I haven't done any benchmarks/timings whether there is any difference in practice.

---

<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 9, 2022, 12:06pm UTC](https://racket.discourse.group/t/speed-benefits-of-submodules/986/3 "2022-05-09T12:06:48Z")

</div>

Assuming you include startup/load time as "runtime": I'd expect not requiring `"one.rkt"` to be better, much like using `#lang racket/base` instead of `#lang racket`. Less work is done and less memory is used.

In addition to avoiding the initialization of `bar` that you don't need from `"one.rkt"`, you're avoiding all of `"one.rkt"`'s transitive requires and their module-level definitions/expressions.

And if someday someone adds more module-level definitions or requires to `"one.rkt"`, you're insulated from that in `"two.rkt"`.

Having said all that, like @simonls I'm not sure the practical/measurable performance difference?

Also, this seems like it's really about the cost of `require`s, not the benefit of submodules; you'd avoid the same cost whether `foo` was defined in a submodule or in the top file module? (Using a submodule adds an extra `configure-runtime` cost -- but I think it's very, very tiny for most langs?)

* * *

Another idea would be to move the definition of `foo` to a new `"foo.rkt"` file that's required by both `"one.rkt"` and `"two.rkt"` -- motivated not so much by performance, but as an organizing principle??

---

<div class="post-metadata">

### Author: ![joeld](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/joeld/32/38_2.png) [@joeld](https://racket.discourse.group/u/joeld)
#### Post date: [May 9, 2022, 3:12pm UTC](https://racket.discourse.group/t/speed-benefits-of-submodules/986/4 "2022-05-09T15:12:24Z")

</div>

The question arises from

> [@greghendershott](#):
>
> Also, this seems like it's really about the cost of `require` s, not the benefit of submodules; you'd avoid the same cost whether `foo` was defined in a submodule or in the top file module? (Using a submodule adds an extra `configure-runtime` cost -- but I think it's very, very tiny for most langs?)
> 
> Another idea would be to move the definition of `foo` to a new `"foo.rkt"` file that's required by both `"one.rkt"` and `"two.rkt"` -- motivated not so much by performance, but as an organizing principle??

Thanks — the question arises because of a `#lang` I’m working on that will automatically `provide` some values. One of the values is likely to be smaller and more frequently accessed than the others. So I’m wondering if my lang should put that value in its own submodule. It’s not something the user of the `#lang` would have any control over.

I could (and probably will) do all the benchmarking, but I still want to know whether there is any _expected_ benefit to doing this, that is, . (And yes I do include load time!)
