The VM, Performance & Profiling

Pharo, Squeak, and Cuis run on descendants of the OpenSmalltalk VM (Pharo maintains its own fork, *pharo-vm*):

40.1 The OpenSmalltalk VM

Pharo, Squeak, and Cuis run on descendants of the OpenSmalltalk VM (Pharo maintains its own fork, *pharo-vm*):

The VM is written in Slang (a Smalltalk subset) with VMMaker, translated to C, and can be simulated inside Smalltalk for debugging.

Cog (Eliot Miranda, 2008): a JIT that compiles bytecode to native code (x86-64, ARM64), with polymorphic inline caches making message sends cheap.

Spur (2014): the object memory — 64-bit, immediate SmallInteger, Character, and SmallFloat64; forwarding pointers making become: cheap; segmented old space.

Sista: an experimental speculative-inlining optimizer.

Since Pharo 9 the VM runs headless by default and opens windows via SDL2 when the image asks.

40.2 Garbage collection

New space (eden + two survivor spaces) collected by a fast generational scavenger — most objects die young.

Old space collected by a mark-compact (full GC); objects surviving several scavenges are tenured.

Weak references (WeakArray, WeakSet, WeakValueDictionary) and ephemerons support caches and finalization (WeakRegistry, FinalizationRegistry).

Smalltalk garbageCollect.                     "full GC, answers free bytes"
Smalltalk garbageCollectMost.                 "scavenge only"
BankAccount allInstances size.
anObject pointersTo.                          "who references this object? (leak hunting)"
SpaceTally new printSpaceAnalysis.            "memory per class"

40.3 Measuring

[ 1000 factorial ] timeToRun.                 "→ a Duration"
[ 1000 factorial ] bench.                     "→ e.g. '2,345 per second' (runs for ~5 s)"
[ (1 to: 1000000) asOrderedCollection ] timeToRun.

40.4 Profiling

Time Profiler: *Tools → Time profiler*, or programmatically TimeProfiler spyOn: [ ... ] (Pharo); MessageTally spyOn: [ ... ] (Squeak, older Pharo). Shows a tree of where time is spent.

Virtual-machine statistics: Smalltalk vm exposes parameters (GC counts, memory sizes).

Allocation profiling: Pharo's *Illimani* and similar memory profilers (loadable).

40.5 Optimization guidelines

Building a long string with , in a loop String streamContents:

includes: on a big Array/OrderedCollection Set or Dictionary lookup

Repeated asSymbol, allInstances, reflection Cache the result Growing collections one by one

Preallocate: OrderedCollection new: 10000, Array new: n Many small Float computations

FloatArray-style typed arrays, or FFI to a C library Hot path with exceptions for control flow

Test conditions explicitly (at:ifAbsent:) Deep doesNotUnderstand: tricks Generate real methods

General rules: profile first, keep methods small (the JIT and inline caches love monomorphic sends), choose the right algorithm and collection, and avoid premature optimization — clarity first.