Memory Pre-allocation in the global scope

In particular, when we talk about “allocations” we mean heap allocations, which are generally only needed to create (or enlarge) variable-sized containers like Array or Dict, or to create mutable struct objects. Allocating such blocks of memory requires an expensive system call (think malloc in C).

In contrast, immutable values like numbers (except bignums), tuples, and struct objects can be stored much more cheaply, e.g. in stack or CPU-register memory, with no system call, so one doesn’t typically worry about the performance cost of “allocating” them.

2 Likes