Local variables are often only kept on the stack (or even only in
registers), especially if they are immutable. For example, this collatz
function does no allocation when applied to integers:

julia> function collatz(n)
           k = 0
           while n > 1
               n = isodd(n) ? 3n+1 : n>>1
               k += 1
           end
           return k
       end
collatz (generic function with 1 method)

julia> @code_native collatz(123)
.section __TEXT,__text,regular,pure_instructions
Filename: none
Source line: 4
push RBP
mov RBP, RSP
 xor EAX, EAX
cmp RDI, 2
 jl 36
Source line: 4
test DIL, 1
 jne 8
sar RDI
 jmpq 5
lea RDI, QWORD PTR [RDI + 2*RDI + 1]
Source line: 5
inc RAX
cmp RDI, 1
 jg -36
Source line: 7
pop RBP
 ret

If you apply it to a mutable BigInt value instead, then that value needs to
be heap allocated, so the code is far less efficient and does quite a lot
of work.


On Fri, Jul 11, 2014 at 2:47 PM, Andrei Zh <[email protected]>
wrote:

> I'm pretty sure that at least some objects in Julia are created in memory.
> But for a language with such strong code analysis capabilities it seems
> quite natural to keep local objects, corresponding to local variables, on
> stack (at least those proved to be small). Does Julia have this feature?
>

Reply via email to