There is no mystery in the execution model of Pharo. The recursive fragment
^ self * (self - 1) factorial
that happens inside the second ifTrue: compiles to the following sequence of bytecodes:
39 <70> self ; receiver of outer message *
40 <70> self ; receiver of inner message -
41 <76> pushConstant: 1 ; argument of self - 1
42 <B1> send: - ; subtract
43 <D0> send: factorial ; send factorial (nothing special here!)
44 <B8> send: * ; multiply
45 <7C> returnTop ; return
Note that in line 43 nothing special happens. The code just sends factorial in the same way it would, had the selector been any other. In particular we can see that there is no special manipulation of the stack here.
This doesn't mean that there cannot be optimizations in the underlying native code. But that is a different discussion. It is the execution model the one that matters to the programmer because any optimization underneath bytecodes is meant to support this model at the conceptual level.
UPDATE
Interestingly, the non-recursive version
factorial2
| f |
f := 1.
2 to: self do: [:i | f := f * i].
^f
is a little bit slower that the recursive one (Pharo). The reason must be that the overhead associated to increasing i is a little bit greater than the recursive send mechanism.
Here are the expressions I tried:
[25000 factorial] timeToRun
[25000 factorial2] timeToRun
ifTrue:case and just count the number of times the same method is on the stack... ;-) – Amos M. Carpenter May 7 '16 at 14:54