# HDF5.jl: How to write type stable code

**URL:** <https://discourse.julialang.org/t/hdf5-jl-how-to-write-type-stable-code/6549>\
**Category:** General Usage\
**Created:** [October 19, 2017, 7:31am UTC](https://discourse.julialang.org/t/hdf5-jl-how-to-write-type-stable-code/6549 "2017-10-19T07:31:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 19, 2017, 7:31am UTC](https://discourse.julialang.org/t/hdf5-jl-how-to-write-type-stable-code/6549/1 "2017-10-19T07:31:57Z")

</div>

Hi, I was wondering how to write type stable code using HDF5 files. I Have code that looks like this

```julia
getMyArray(filename::String) = hdfread(filename,"/myArray")

```

So if I know that the array is of type Float64 and has three dimension is it sufficient to write

```julia
getMyArray(filename::String)::Array{Float64,3} = hdfread(filename,"/myArray")

```

Or can I do any better?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 19, 2017, 8:08am UTC](https://discourse.julialang.org/t/hdf5-jl-how-to-write-type-stable-code/6549/2 "2017-10-19T08:08:24Z")

</div>

I think that is the correct way of doing it.

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 19, 2017, 8:37am UTC](https://discourse.julialang.org/t/hdf5-jl-how-to-write-type-stable-code/6549/3 "2017-10-19T08:37:51Z")

</div>

Ok thanks! And how “bad” is that `hdfread` is still type unstable? I think there are functions that allow to preallocate the array making things potentially type stable. I would like to know if this is worth investigating or if my code is fine an I won’t gain anything.

---

<div class="post-metadata">

**Author:** ![Jeremy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeremy/32/295_2.png) [@Jeremy](https://discourse.julialang.org/u/Jeremy)\
**Post date:** [October 19, 2017, 8:49am UTC](https://discourse.julialang.org/t/hdf5-jl-how-to-write-type-stable-code/6549/4 "2017-10-19T08:49:37Z")

</div>

I think the HDF5.jl package is still a work in progress. A great deal works but I’m currently stuck on how to append data, writing variable length strings, and how to make compound tables _and_ fill them.

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 19, 2017, 8:53am UTC](https://discourse.julialang.org/t/hdf5-jl-how-to-write-type-stable-code/6549/5 "2017-10-19T08:53:59Z")

</div>

yes, there are some remaining pieces in HDF5.jl but my question is actually not that tight on that lib. Any IO reader will have this issue I described above.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 19, 2017, 9:12am UTC](https://discourse.julialang.org/t/hdf5-jl-how-to-write-type-stable-code/6549/6 "2017-10-19T09:12:25Z")

</div>

It’s not really a problem that `hdfread` is type unstable because the time to do the type assert should be completely insignificant compared to the time it takes to execute `hdfread`

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 19, 2017, 9:17am UTC](https://discourse.julialang.org/t/hdf5-jl-how-to-write-type-stable-code/6549/7 "2017-10-19T09:17:48Z")

</div>

Not sure I understand. My issue is that I have several of these `hdfread` in my code and the first time I call it needs 1.7 seconds, the second requires 0.004762 seconds.

In the end I don’t personally care about type stability in this case. But I had the impression that type stable code is easier to compile (i.e. infer).

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [October 19, 2017, 9:42am UTC](https://discourse.julialang.org/t/hdf5-jl-how-to-write-type-stable-code/6549/8 "2017-10-19T09:42:30Z")

</div>

I didn’t know we were talking about compilation time… I haven’t gotten the impression that type stable code is faster to compile. In some sense, shouldn’t it be slower since you can do more optimizations with type stable code?

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [October 19, 2017, 9:53am UTC](https://discourse.julialang.org/t/hdf5-jl-how-to-write-type-stable-code/6549/9 "2017-10-19T09:53:10Z")

</div>

Yes sorry. That is the question I asked in [Does compile time depend on type stability - #3 by tobias.knopp](https://discourse.julialang.org/t/does-compile-time-depend-on-type-stability/6548/3)

My global issue is that the UI application I am working on is not usable if it requires 2 minutes to start up and pressing a button the first time requires 20 seconds. Since pressing a button involves reading e.g. HDF5 file I was wondering if it would be a good idea to “clean up” the type unstable low level code in order to make my application start faster.
