# Strange allocations?

**URL:** <https://discourse.julialang.org/t/strange-allocations/18746>\
**Category:** Internals & Design\
**Tags:** memory-allocation\
**Created:** [December 17, 2018, 2:28pm UTC](https://discourse.julialang.org/t/strange-allocations/18746 "2018-12-17T14:28:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [December 17, 2018, 2:28pm UTC](https://discourse.julialang.org/t/strange-allocations/18746/1 "2018-12-17T14:28:48Z")

</div>

Consider these two function definitions:

```julia
function myhypot(x::Number...)
	s = sum(abs2, float.(x))
	r = sqrt(s)
	if isnan(s) || isinf(s) || s ≤ floatmin(s) * (length(x) - 1)
        println("here")
		any(isinf, x) && return oftype(r, Inf)
		ma = maximum(abs.(x))
		iszero(ma) && return oftype(r, ma)
		return oftype(r, ma * sqrt(sum(abs2, x ./ ma)))
	else
		return r
	end
end

function h(x::Number...)
	s = sum(abs2, float.(x))
	r = sqrt(s)
	return r
end

```

The two functions are equivalent if the branch condition is not met. I added a `println` statement to make sure that this branch was not being executed.

Here is an example where the branch is not entered. However I get very different allocations:

```julia
julia> @btime myhypot(4., 5., 6., 7.)
  1.314 μs (13 allocations: 400 bytes)
11.224972160321824

julia> @btime h(4., 5., 6., 7.)
  4.878 ns (0 allocations: 0 bytes)
11.224972160321824

```

Both calls should be executing the same code. Why one allocates and the other doesn’t?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 17, 2018, 9:21pm UTC](https://discourse.julialang.org/t/strange-allocations/18746/2 "2018-12-17T21:21:36Z")

</div>

Well, at least I discover that the allocations are coming from the `isinf` function. If I change to `isodd`, then nothing is allocated:

```julia
function myhypot_test(x::Number...)
	s = sum(abs2, float.(x))
	r = sqrt(s)
	if isnan(s) || isinf(s) || s ≤ floatmin(s) * (length(x) - 1)
		any(isodd, x) && return oftype(r, Inf)
		ma = maximum(abs.(x))
		iszero(ma) && return oftype(r, ma)
        return oftype(r, ma )
	else
		return r
	end
end

@btime myhypot_test(4.,5.,6.,7.)
  6.101 ns (0 allocations: 0 bytes)
11.224972160321824

```

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [December 17, 2018, 9:23pm UTC](https://discourse.julialang.org/t/strange-allocations/18746/3 "2018-12-17T21:23:19Z")

</div>

Bizarre!

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 17, 2018, 9:30pm UTC](https://discourse.julialang.org/t/strange-allocations/18746/4 "2018-12-17T21:30:30Z")

</div>

Even stranger is:

```julia
function myhypot(x::Number...)
	s = sum(abs2, float.(x))
	r = sqrt(s)
    if isnan(s) || isinf(s) || isinf(s)
        isinf.(x)
        return r
	else
		return r
	end
end

julia> @btime myhypot(4.,5.,6.,7.)
  1.365 μs (12 allocations: 384 bytes)
11.224972160321824

function myhypot(x::Number...)
	s = sum(abs2, float.(x))
	r = sqrt(s)
    if isnan(s) || isinf(s)
        isinf.(x)
        return r
	else
		return r
	end
end

julia> @btime myhypot(4.,5.,6.,7.)
  4.254 ns (0 allocations: 0 bytes)
11.224972160321824

function myhypot(x::Number...)
	s = sum(abs2, float.(x))
	r = sqrt(s)
    if isnan(s) || isinf(s) || isinf(s)
        return r
	else
		return r
	end
end

julia> @btime myhypot(4.,5.,6.,7.)
  3.783 ns (0 allocations: 0 bytes)
11.224972160321824

```

This is completely unexpected to me.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 17, 2018, 9:38pm UTC](https://discourse.julialang.org/t/strange-allocations/18746/5 "2018-12-17T21:38:10Z")

</div>

> [@Ronis\_BR](#):
>
> If I change to `isodd` , then nothing is allocated:

That is, ehm, odd:

```julia
julia> isodd(3.0)
ERROR: MethodError: no method matching isodd(::Float64)

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [December 17, 2018, 9:43pm UTC](https://discourse.julialang.org/t/strange-allocations/18746/6 "2018-12-17T21:43:09Z")

</div>

Not sure what fixed it, but it no longer happens on master:

```julia
julia> function myhypot(x::Number...)
               s = sum(abs2, float.(x))
               r = sqrt(s)
           if isnan(s) || isinf(s) || isinf(s)
               isinf.(x)
               return r
               else
                       return r
               end
       end
myhypot (generic function with 1 method)

julia> @btime myhypot(4.,5.,6.,7.)
  5.025 ns (0 allocations: 0 bytes)
11.224972160321824

```

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [December 18, 2018, 3:06am UTC](https://discourse.julialang.org/t/strange-allocations/18746/7 "2018-12-18T03:06:12Z")

</div>

For some reason the splatting penalty seems to be kicking in very early in this case. If you replace the splatted argument `x` with four positional arguments and recreate the tuple `x` inside the function, there is no performance problem. If you make the argument `x` a tuple, things are also fine.

Glad it’s fixed on master.
