# Allocations for type assertions

**URL:** <https://discourse.julialang.org/t/allocations-for-type-assertions/101104>\
**Category:** Internals & Design\
**Created:** [July 3, 2023, 4:21am UTC](https://discourse.julialang.org/t/allocations-for-type-assertions/101104 "2023-07-03T04:21:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![motabbara](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@motabbara](https://discourse.julialang.org/u/motabbara)\
**Post date:** [July 3, 2023, 4:21am UTC](https://discourse.julialang.org/t/allocations-for-type-assertions/101104/1 "2023-07-03T04:21:24Z")

</div>

In the below code snippet

```julia
something::ConcreteType = mydict[key] ###mydict = Dict{String,AbstractType}()

```

the LLVM IR shows a call to ijl\_gc\_pool\_alloc and from the labelling, it looks like that’s part of the type assertion on the LHS of the snippet. What is the rationale for the allocation and what can I do to avoid it if know the returned object is definitely a ConcreteType ? Is type assertion always allocating?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 3, 2023, 4:32am UTC](https://discourse.julialang.org/t/allocations-for-type-assertions/101104/2 "2023-07-03T04:32:31Z")

</div>

It’s likely the retrieval from the dict that allocates intermediary storage, not the type assertion. The objects from the dict are only known to be of an abstract type after all.

---

<div class="post-metadata">

**Author:** ![motabbara](https://avatars.discourse-cdn.com/v4/letter/m/d9b06d/32.png) [@motabbara](https://discourse.julialang.org/u/motabbara)\
**Post date:** [July 3, 2023, 5:54am UTC](https://discourse.julialang.org/t/allocations-for-type-assertions/101104/3 "2023-07-03T05:54:07Z")

</div>

If the dict values are already boxed and heap-allocated, why does retrieval cause another allocation?

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 3, 2023, 6:23am UTC](https://discourse.julialang.org/t/allocations-for-type-assertions/101104/4 "2023-07-03T06:23:21Z")

</div>

I’m not 100% sure on this, but it may have to do with the value needing to be rooted when returned from the function/passed to `convert` (which indeed would be for the conversion) 🤔 It’s admittedly a bit hard to diagnose the exact cause of an allocation, but in this case it’s definitely due to the abstractly typed values in the dict.

As for avoiding that allocation - type instabilities like that unfortunately cause allocations all over the place, since now you need to keep values boxed (and more importantly, GC rooted) when passing them between functions. So even if the value returns from the function, it needs to be boxed, because the overall type can only be assumed to be `Real`.

For more direct diagnosis though, you’ll have to provide an example with more context; there’s just too many possible ways to progress here to diagnose from just a single conversion & dict indexing.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [July 3, 2023, 7:14am UTC](https://discourse.julialang.org/t/allocations-for-type-assertions/101104/5 "2023-07-03T07:14:50Z")

</div>

When you type assert the variable, it will try to convert the RHS to the type of the variable. If you know the RHS type you may instead want to type assert the RHS:

```julia
something = mydict[key]::ConcreteType

```
