# CSV.jl type stability

**URL:** https://discourse.julialang.org/t/csv-jl-type-stability/89097
**Category:** General Usage
**Tags:** csv, type-stability
**Created:** [October 22, 2022, 2:06am UTC](https://discourse.julialang.org/t/csv-jl-type-stability/89097 "2022-10-22T02:06:39Z")
**Posts on this page:** 7
**Page:** 2

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [October 22, 2022, 10:42am UTC](https://discourse.julialang.org/t/csv-jl-type-stability/89097/21 "2022-10-22T10:42:48Z")

</div>

> [@rafael.guerra](#):
>
> rowtable

If I understand correctly rowtable is going to consume the entire file and make a vector of tuples, but this could be enormous. Better to consume one row at a time and make the special type one at a time as in my loop. It’s possible I’m misunderstanding though.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [October 22, 2022, 11:46am UTC](https://discourse.julialang.org/t/csv-jl-type-stability/89097/22 "2022-10-22T11:46:53Z")

</div>

@dlakelan, you are right, but it is still very interesting code.

As for the CSV.Rows approach, it seems to be much more inefficient in this case than Greg Plowman’s `eachline` parsing (I’ve tested for 1 M rows, with 4 Ints, 3 Float64 and 1 String in each). Are you seeing the same thing?

---

<div class="post-metadata">

### Author: ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)
#### Post date: [October 22, 2022, 12:09pm UTC](https://discourse.julialang.org/t/csv-jl-type-stability/89097/23 "2022-10-22T12:09:31Z")

</div>

Thank you for all the suggestions everyone! I will play around with all of them.

---

<div class="post-metadata">

### Author: ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)
#### Post date: [October 22, 2022, 4:50pm UTC](https://discourse.julialang.org/t/csv-jl-type-stability/89097/24 "2022-10-22T16:50:35Z")

</div>

@rafael.guerra I am actually seeing the opposite. That the manual parsing is taking longer than the type annotations. In fact the manual parsing is taking longer than no type annotations.

---

<div class="post-metadata">

### Author: ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)
#### Post date: [October 22, 2022, 4:52pm UTC](https://discourse.julialang.org/t/csv-jl-type-stability/89097/25 "2022-10-22T16:52:18Z")

</div>

Hmm it actually seems the slowness is coming from the GZip package that I am using to decompress the file, rather than the actual conversion themselves. Let me try with versions of the file that are already decompressed on disk.

---

<div class="post-metadata">

### Author: ![HashBrown](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hashbrown/32/43601_2.png) [@HashBrown](https://discourse.julialang.org/u/HashBrown)
#### Post date: [October 22, 2022, 5:02pm UTC](https://discourse.julialang.org/t/csv-jl-type-stability/89097/26 "2022-10-22T17:02:24Z")

</div>

Ok so now I am seeing that both the manual reading and the type annotated reading are comparable. And that without type annotations is about half as fast.

The manual reading is still slightly slower (by about 10%) once both it and the type annotated CSV.File have already been compiled (on the first run, manual reading is 50% faster because of the significantly lower compile time)

---

<div class="post-metadata">

### Author: ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)
#### Post date: [October 22, 2022, 8:10pm UTC](https://discourse.julialang.org/t/csv-jl-type-stability/89097/27 "2022-10-22T20:10:57Z")

</div>

> [@dlakelan](#):
>
> If I understand correctly rowtable is going to consume the entire file and make a vector of tuples, but this could be enormous. Better to consume one row at a time and make the special type one at a time as in my loop.

If you load all the data in memory anyway in the form of those objects, then an extra copy of the same data won’t hurt performance much.  
And when performance is needed, column-oriented storage is often better. For example, with StructArrays there’s a very efficient solution `CSV.File(...) |> columntable |> StructArray{MyCustomType}`

[Previous page](https://discourse.julialang.org/t/csv-jl-type-stability/89097.md?page=1)
