Immutable States/Actions

40 views
Skip to first unread message

Zachary Sunberg

unread,
Dec 16, 2015, 9:28:26 PM12/16/15
to pomdps-users
Hi all, Just wondering - has anyone tried using immutable states, actions, and/or observations? If so, what was your experience like? Did it improve performance in any cases?

- Zach

Maxim Egorov

unread,
Jan 6, 2016, 3:31:11 PM1/6/16
to julia-pomdp-users
From the few quick tests I've tried, the advantage of immutable types really shows when allocating memory. Since we pre-allocate everything, I'm not sure there is much of an advantage in using immutable types. I ran a quick test:
type StateT

    x::Int64

    y::Int64

end

immutable StateI

    x::Int64

    y::Int64

end

function test1(n::Int64)

    for i=1:n, j=1:n

        s = StateT(i,j)

    end

end

function test2(n::Int64)

    for i=1:n, j=1:n

        s = StateI(i,j)

    end

end

@time test1(10_000) #  
0.801656 seconds (100.00 M allocations: 2.980 GB, 20.56% gc time)

@time test2(10_000) # 0.000003 seconds (4 allocations: 160 bytes)





It seems like Julia does something clever with memory allocation for immutable types.

Zachary Sunberg

unread,
Jan 6, 2016, 6:20:22 PM1/6/16
to julia-pomdp-users
Max, thanks for the tests. I'm pretty sure that in the second test, the compiler completely optimizes away the for loop and simply returns void.

```
julia> @code_llvm test2(10_000)

define void @julia_test2_21364(i64) {
L7:
  ret void
}
```
(compare this to `@code_llvm test1(10_000)`)

I think we would have to test immutables in an actual problem to see the performance implications

Maxim Egorov

unread,
Jan 6, 2016, 6:29:10 PM1/6/16
to pomdps...@googlegroups.com
Hey Zach,

You're right. I made a slight modification to the test functions so the types are actually being used. 

function test1(n::Int64)
    tot = 0

    for i=1:n, j=1:n

       s = StateT(i,j)

       tot += (s.x+s.y)

   end

   return tot

end


function test2(n::Int64)
    tot = 0

    for i=1:n, j=1:n

       = StateI(i,j)

       tot += (s.x+s.y)

   end

   return tot

end


@time test1(10_000) # 0.717841 seconds (100.00 M allocations: 2.980 GB, 22.74% gc time)

@time test2(10_000) # 0.000009 seconds (5 allocations: 176 bytes)

There does seem to be an effect on memory/performance.

Reply all
Reply to author
Forward
0 new messages