# Avoiding Vectors of Abstract Types

**URL:** <https://discourse.julialang.org/t/avoiding-vectors-of-abstract-types/61883>\
**Category:** Performance\
**Tags:** question, type-stability\
**Created:** [May 26, 2021, 7:20pm UTC](https://discourse.julialang.org/t/avoiding-vectors-of-abstract-types/61883 "2021-05-26T19:20:06Z")\
**Posts on this page:** 3\
**Page:** 2

<div class="post-metadata">

**Author:** ![tisztamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tisztamo/32/16200_2.png) [@tisztamo](https://discourse.julialang.org/u/tisztamo)\
**Post date:** [February 16, 2022, 2:24pm UTC](https://discourse.julialang.org/t/avoiding-vectors-of-abstract-types/61883/21 "2022-02-16T14:24:26Z")

</div>

> [@filchristou](#):
>
> not really acting on any “Abstract” data

I am far from understanding where “world splitting” happens in your example, but changing

```julia
function mysum(mv::Vector{T}) where T
    sum([v.x for v in mv])
end

```

to

```julia
function mysum(mv::Vector{T}) where T
	s=0
	for i=1:length(mv)
		s+=mv[i].x
	end
    return s
end

```

Results in much better timings:

```julia
  59.99385162601627 # 1
  62.53760935910483 # 2
 392.1048792270531 # 3 
 392.31189320388347 # 4 types in the union
 393.2404761904762 # 5
 391.7910280373831 # 7
 391.6691304347826 # 8

```

And I would also call the second version “acting on the data”.

---

<div class="post-metadata">

**Author:** ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)\
**Post date:** [February 16, 2022, 2:54pm UTC](https://discourse.julialang.org/t/avoiding-vectors-of-abstract-types/61883/22 "2022-02-16T14:54:32Z")

</div>

I was puzzled why union splitting doesn’t work here…

Following the Julia rule “if in doubt write your own loop” I rewrote mysum as

```julia
function mysum(mv)
    s=0
    for i=1:length(mv)
        s+=mv[i].x
    end
    s
end

```

and get (note the logscale of the y-axis):

 ![newplot](https://global.discourse-cdn.com/julialang/original/3X/0/3/031cc3c31bf7abdd384f5afd9293bfdae0e5721b.png)

In your function somewhere – may be in the array comprehension – Julia seems to fall back to “world splitting” instead of “union splitting”.

---

<div class="post-metadata">

**Author:** ![j-fu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j-fu/32/11373_2.png) [@j-fu](https://discourse.julialang.org/u/j-fu)\
**Post date:** [February 17, 2022, 9:46pm UTC](https://discourse.julialang.org/t/avoiding-vectors-of-abstract-types/61883/23 "2022-02-17T21:46:36Z")

</div>

But…

```julia
function mysum(mv)
    s=0
    for v ∈ mv
        s+=v.x
    end
    s
end

```

 ![incollection](https://global.discourse-cdn.com/julialang/original/3X/f/2/f2d13f440e2f29978afdcf2d3c28dfd8f03f323d.png)

and

```julia
function mysum(mv)
    s=0
    foreach(mv) do v
        s+=v.x
    end
    s
end

```

 ![foreach](https://global.discourse-cdn.com/julialang/original/3X/2/7/27d906a16ed54ee73dd6f1657275df10d86d4ce3.png)

[Previous page](https://discourse.julialang.org/t/avoiding-vectors-of-abstract-types/61883.md?page=1)
