Hello,
using only-in in a require statement can improve the memory footprint? It’s a good practice?
Matteo
Hello,
using only-in in a require statement can improve the memory footprint? It’s a good practice?
Matteo
My understanding: The granularity is modules.
By using require
at all you pay the speed and space cost for requiring the entire module -- as well as all the modules it transitively requires, that aren't already required.
This can be significant, for example racket
vs. just racket/base
.
Clauses like only-in
don't change this; they're not an optimization for space or speed (the entire module(s) will still be instantiated). Instead they're about visibility: You don't want to expose all the exported definitions, just some. You want to be explicit that you're importing the module to get what from it, exactly.
One example: If you have some local definition of foo
, and require a module m
just to use its exported bar
, great, but someday m
could decide to start exporting something called foo
, too. Strictly speaking, exporting new things breaks backward compatibility. Clauses like only-in
, prefix-in
, rename-in
are ways for importers to handle that, as well as potentially make the code and intent clearer.