# How can I reduce memory allocation and execution time while ray tracing using VoxelRayTracers package?

**URL:** <https://discourse.julialang.org/t/how-can-i-reduce-memory-allocation-and-execution-time-while-ray-tracing-using-voxelraytracers-package/34971>\
**Category:** Performance\
**Tags:** question\
**Created:** [February 21, 2020, 12:36pm UTC](https://discourse.julialang.org/t/how-can-i-reduce-memory-allocation-and-execution-time-while-ray-tracing-using-voxelraytracers-package/34971 "2020-02-21T12:36:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [February 21, 2020, 12:36pm UTC](https://discourse.julialang.org/t/how-can-i-reduce-memory-allocation-and-execution-time-while-ray-tracing-using-voxelraytracers-package/34971/1 "2020-02-21T12:36:48Z")

</div>

Hi,

I am trying to use [VoxelRayTracers.jl](https://github.com/jw3126/VoxelRayTracers.jl) package to trace a ray through cubic voxels.

I want to improve the performance of ray tracing by reducing execution time because in my application I have to use many rays in three dimensions.

I wrote a simple example to show my case. I only used one `ray`, but variable `itr` represents number of rays. Then I have to sum traced voxel values in each ray.

```julia
using VoxelRayTracers
using BenchmarkTools
edges = (-2.0:0.1:2.0, -2.0:0.1:2.0)
ray = (position=[4.946152422706632, 3.433012701892219], velocity=[-0.8660254037844393,-0.5])
all_val = rand(41,41)
itr = 400000

function exit_data(ray, edges, val, itr)
      for i in 1:itr
            out = 0.0
            for hit in eachtraversal(ray, edges)
              out += val[hit.voxelindex]
            end
      end
end

```

I have checked execution time and memory allocation by using `@btime`

```julia
@btime exit_data($ray, $edges, $all_val, $itr)

```

and the result is as below:  
`710.617 ms (2949120 allocations: 145.00 MiB)`

In my real case, I have more than 1.5M rays and points will be 3 dimensions… so, execution time and memory allocation will be very high… Moreover, I have to iterate this whole process more than 1000 times using another algorithm. So, can anyone suggest some ideas to improve performance in terms of execution time and memory allocation?

---

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [February 21, 2020, 12:58pm UTC](https://discourse.julialang.org/t/how-can-i-reduce-memory-allocation-and-execution-time-while-ray-tracing-using-voxelraytracers-package/34971/2 "2020-02-21T12:58:17Z")

</div>

> [@Manu\_Francis](#):
>
> position=[4.946152422706632, 3.433012701892219], velocity=[-0.8660254037844393,-0.5]

Try using static vectors here. This should eliminate many of the allocations inside `eachtraversal` if I understand correctly.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [February 22, 2020, 6:58pm UTC](https://discourse.julialang.org/t/how-can-i-reduce-memory-allocation-and-execution-time-while-ray-tracing-using-voxelraytracers-package/34971/3 "2020-02-22T18:58:56Z")

</div>

Its a bug. Inference could not handle [this line](https://github.com/jw3126/VoxelRayTracers.jl/blob/4a69238a8b7192424b5d659aa1e2d099c1895107/src/core.jl#L37), not sure why. I found a workaround anyway, fixed on master and tagged a release. There should be no need to use a static vector.

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [February 23, 2020, 9:43am UTC](https://discourse.julialang.org/t/how-can-i-reduce-memory-allocation-and-execution-time-while-ray-tracing-using-voxelraytracers-package/34971/4 "2020-02-23T09:43:54Z")

</div>

@mohamed82008 : Thanks for your reply. I tried with static vectors like `ray = (position=@SVector[4.946152422706632, 3.433012701892219], velocity=@SVector[-0.8660254037844393,-0.5])` , but it give similar results with` @btime exit_data($ray, $edges, $all_val, $itr)`

---

<div class="post-metadata">

**Author:** ![Manu\_Francis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manu_francis/32/10010_2.png) [@Manu\_Francis](https://discourse.julialang.org/u/Manu_Francis)\
**Post date:** [February 23, 2020, 9:55am UTC](https://discourse.julialang.org/t/how-can-i-reduce-memory-allocation-and-execution-time-while-ray-tracing-using-voxelraytracers-package/34971/5 "2020-02-23T09:55:16Z")

</div>

@jw3126 : Thanks for your fix. Now, I am getting timing values as `506.445 ms (0 allocations: 0 bytes)` and time of execution also improved by `200ms`.

Thanks Again,  
Manu
