# Unnecessary copy using convert.()

**URL:** <https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009>\
**Category:** General Usage\
**Created:** [February 22, 2023, 11:02am UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009 "2023-02-22T11:02:38Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [February 22, 2023, 11:02am UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009/1 "2023-02-22T11:02:38Z")

</div>

I was surprised by this call to convert on a vector generating a copy, when the output type is the same as the input type. Is this desired behaviour?:

```julia
julia> A = [1, 2, 3, 4]
4-element Vector{Int64}:
 1
 2
 3
 4

julia> B = convert.(Int64, A)
4-element Vector{Int64}:
 1
 2
 3
 4

julia> A === B
false

julia> @btime convert.(Int64, $A)
  33.073 ns (1 allocation: 96 bytes)
4-element Vector{Int64}:
 1
 2
 3
 4

```

I’m using Julia 1.8.2

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [February 22, 2023, 11:34am UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009/2 "2023-02-22T11:34:43Z")

</div>

Broadcasting _always_ result in a copy, no matter the function:

```julia
julia> A = [1, 2, 3];

julia> B = identity.(A);

julia> A === B
false

```

However, since broadcasting is element-wise, you should really compare if the _elements_ are `===` instead, which they are:

```julia
julia> A[1] === B[1]
true

```

The example is then more interesting with a vector of mutables:

```julia
julia> A = [[1, 2], [3, 4]];

julia> B = convert.(Vector{Int}, A); # no copy needed

julia> A[1] === B[1]
true

julia> C = convert.(Vector{Float64}, A); # copy needed

julia> A[1] === C[1]
false

```

---

<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:** [February 22, 2023, 12:06pm UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009/3 "2023-02-22T12:06:02Z")

</div>

I think it would be odd if broadcasting _didn’t_ create a new array. This one is maybe a bit less obvious:

```julia
julia> A = [1, 2, 3, 4];

julia> A === Vector(A)
false

```

This is also why it is frustrating to see people writing `x = Vector([1,2,3])`. It does needless extra work.

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [February 22, 2023, 2:29pm UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009/4 "2023-02-22T14:29:00Z")

</div>

Thanks, both. I’m now wondering how to avoid such a conversion when not required. Must I do an explicit check myself?:

```julia
function convertvec(type, vec)
if eltype(vec) == type
    return vec
end
return convert.(type, vec)
end

```

---

<div class="post-metadata">

**Author:** ![cstjean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cstjean/32/1444_2.png) [@cstjean](https://discourse.julialang.org/u/cstjean)\
**Post date:** [February 22, 2023, 3:18pm UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009/5 "2023-02-22T15:18:57Z")

</div>

```julia
julia> v = [1,2,3,4];

julia> convert(Vector{Int}, v) === v
true # no copy was made

julia> convert(Vector{Float64}, v) === v
false # "copy" was made (i.e. new vector was created with new type)

```

?

There’s also this convenient syntax for implicit `convert`:

```julia
v2::Vector{Int} = v # doesn't copy unless necessary

```

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [February 22, 2023, 3:30pm UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009/6 "2023-02-22T15:30:47Z")

</div>

Perfect. What I needed. Thanks!

---

<div class="post-metadata">

**Author:** ![moble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moble/32/23535_2.png) [@moble](https://discourse.julialang.org/u/moble)\
**Post date:** [February 23, 2023, 9:02pm UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009/7 "2023-02-23T21:02:02Z")

</div>

Maybe I’m misunderstanding, but when I first saw this, I assumed you were looking for [`reinterpret`](https://docs.julialang.org/en/v1/base/arrays/#Base.reinterpret). This lets you just reinterpret a region of memory as an array of some different type. For example, you could reinterpret `v` as a `Vector{Float64}` _without_ making a copy.

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [February 25, 2023, 10:39am UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009/8 "2023-02-25T10:39:24Z")

</div>

In the case that the input and output types are different, I do want the call to do a type conversion, necessitating a copy. Hence why I’m using the function `convert`.

---

<div class="post-metadata">

**Author:** ![moble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moble/32/23535_2.png) [@moble](https://discourse.julialang.org/u/moble)\
**Post date:** [February 25, 2023, 5:07pm UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009/9 "2023-02-25T17:07:35Z")

</div>

I still don’t understand what you’re saying. A type conversion _does not_ necessitate a copy, and the title of this thread suggests you _don’t want_ a copy. So what is your actual goal?

For example,

```julia
julia> A = [1, 2, 3, 4];
julia> B = reinterpret(Float64, A);
julia> C = reinterpret(Float32, B);
julia> D = reinterpret(ComplexF64, C);

```

makes no copies. All four arrays point to the same underlying memory; they just tell Julia to interpret that memory in four different ways.

---

<div class="post-metadata">

**Author:** ![user664303](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/user664303/32/37843_2.png) [@user664303](https://discourse.julialang.org/u/user664303)\
**Post date:** [February 25, 2023, 7:36pm UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009/10 "2023-02-25T19:36:59Z")

</div>

The statements regarding conversion necessitating or not necessitating a copy really depend on what you understand “convert” to mean. I was using it to mean what the method `convert` does (which _does_ necessitate a copy), and not what the method `reinterpret` does. And I’m happy to disagree on that meaning; I certainly don’t want to get into an argument over semantics. Fortunately others understood what I meant, and I got the answer I was looking for.

---

<div class="post-metadata">

**Author:** ![moble](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/moble/32/23535_2.png) [@moble](https://discourse.julialang.org/u/moble)\
**Post date:** [February 25, 2023, 8:00pm UTC](https://discourse.julialang.org/t/unnecessary-copy-using-convert/95009/11 "2023-02-25T20:00:16Z")

</div>

Sorry. I was just trying to help.
