# Making Int's size runtime selectable with no perfomance kill

**URL:** <https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744>\
**Category:** New to Julia\
**Tags:** type\
**Created:** [September 6, 2017, 12:25pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744 "2017-09-06T12:25:44Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![timofeymukha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timofeymukha/32/2161_2.png) [@timofeymukha](https://discourse.julialang.org/u/timofeymukha)\
**Post date:** [September 6, 2017, 12:25pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/1 "2017-09-06T12:25:44Z")

</div>

Dear all,

In my application (a mesh generator for CFD) I would like the user to determine wether Ints should be Int32 or Int64. The reason is that Int32 is usually large enough, meshes are still rearly larger than 2 billion points.

What I did is that in my main() function I define

```julia
const Label::DataType = eval(parse("Int$(parsedArgs["intsize"])"))

```

so the user can pass 32 or 64 as a command line argument.

Also, I define two types in the top-level scope of the modules as follows

```julia
const Face{T} = SVector{4, T}
const Cell{T} = SVector{8, T}

```

The code generates large lists of types `Label` and `Face{Label}`.  
I use `Label` to parametrize all my functions and types using

```julia
where {Label <: Integer}

```

For instance,

```julia
type Block{Label <: Integer}                                                    
     vertexLabels::Vector{Label}                                                 
     vertices::Vector{Point}                                                     
     points::Vector{Point}                                                       
     cells::Vector{Cell{Label}}                                                  
     boundaryFaces::Vector{Vector{Face{Label}}}                                  
     edgePoints::Vector{Vector{Point}}                                           
     edgeWeights::Vector{Vector{Float64}}                                        
     curvedEdges::Vector{CurvedEdge}                                             
     nCells::SVector{3, Integer}                                                 
     gradingType::String                                                         
     grading::Vector{Any}                                                        
 end

```

and

```julia
function make_block_edges!(block::Block{Label}) where {Label <: Integer}

```

The code works. But using `Label` has totally killed the performance and using ProfileView I see that most of the diagram is read and filled with things from `inference.jl`. So, obviously I’ve srewed this up. I am wondering if I could get any help on what I did wrong and how to do this properly.

Kind regards,  
timofey

---

<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:** [September 6, 2017, 1:52pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/2 "2017-09-06T13:52:46Z")

</div>

Just have the user pass Int32 or Int64 e.g

```julia
function make_block(::Type{T}) where T <: Integer
    Block{T} (...) 
end

make_block(Int64) 

```

Also,  
`nCells::SVector{3, Integer} ` will be slow. Either parameterize or use a concrete type`.

---

<div class="post-metadata">

**Author:** ![timofeymukha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timofeymukha/32/2161_2.png) [@timofeymukha](https://discourse.julialang.org/u/timofeymukha)\
**Post date:** [September 6, 2017, 2:43pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/3 "2017-09-06T14:43:11Z")

</div>

Hej! Thank you for the reply.

This is more or less what I do, I think. I pass `Label`as an argument to the function that creates the blocks. I can’t have the “user” provide the type itself though, I have to parse it from text first.

Good catch with the `nCells` fixed that now.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 6, 2017, 3:15pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/4 "2017-09-06T15:15:46Z")

</div>

If `Label` is a constant, then do you even need `Block` to be a parameterized type?

You could do something like this:

```julia
const Label = parsedArgs["intsize"] == "32" ? Int32 : Int64
type Block                                                    
     vertexLabels::Vector{Label}                                                 
     ...
end

```

---

<div class="post-metadata">

**Author:** ![timofeymukha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timofeymukha/32/2161_2.png) [@timofeymukha](https://discourse.julialang.org/u/timofeymukha)\
**Post date:** [September 6, 2017, 3:30pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/5 "2017-09-06T15:30:26Z")

</div>

Hello!

True, but then `Label` must be in the module-scope, whereas currently it is getting parsed inside the main() function.

In other words, I currently have a module that provides all the functionality (parametrized wrt `Label`) and a script that uses that module with a `main()` function.  
Basically, If the definition of `Block` is not in the same scope as where `Label` is defined, I have to parameterize, right?

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 6, 2017, 4:12pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/6 "2017-09-06T16:12:17Z")

</div>

> [@timofeymukha](#):
>
> In other words, I currently have a module that provides all the functionality (parametrized wrt Label) and a script that uses that module with a main() function.

OK. Yes, that seems to be the sensible way to do it. I’ve done similar things and gotten the same performance as with a fixed type, so I have no idea why it didn’t work for you.

---

<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:** [September 6, 2017, 4:23pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/7 "2017-09-06T16:23:22Z")

</div>

Just look at the string and instantiate a `Block{Int64} ` or `Block{Int32} ` in `main` then? A function barrier when you call something from main will take care of type instability.

---

<div class="post-metadata">

**Author:** ![timofeymukha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timofeymukha/32/2161_2.png) [@timofeymukha](https://discourse.julialang.org/u/timofeymukha)\
**Post date:** [September 6, 2017, 5:09pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/8 "2017-09-06T17:09:24Z")

</div>

It appears that the `Integer` in the type definition was in fact the main performance killer. Before parametrizing I had `Int64` there…  
Such things are scary of course… who knows where I have more stuff that makes things 2x slower 🙂.  
Anyway, thank you for the help!

---

<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:** [September 6, 2017, 5:48pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/9 "2017-09-06T17:48:18Z")

</div>

You should really rethink the eval parse stuff though. It prevents your code from being precompiled for example for no benefit. Almost every time you do a eval parse, there are better ways of doing it.

---

<div class="post-metadata">

**Author:** ![timofeymukha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timofeymukha/32/2161_2.png) [@timofeymukha](https://discourse.julialang.org/u/timofeymukha)\
**Post date:** [September 6, 2017, 5:56pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/10 "2017-09-06T17:56:29Z")

</div>

Ok, thank you! I will try to think of a different way. Although I have actually grown fond of eval parse!

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 6, 2017, 6:03pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/11 "2017-09-06T18:03:23Z")

</div>

If the only two alternatives are `Int32` and `Int64`, then something like this should do it:

```julia
const Label = parsedArgs["intsize"] == "32" ? Int32 : Int64

```

Besides the precompile issue, `eval` can also be a security issue, and can lead to really strange error messages when given unexpected input, e.g. if you have `parsedArgs["intsize"] = "32;String"`.

---

<div class="post-metadata">

**Author:** ![timofeymukha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/timofeymukha/32/2161_2.png) [@timofeymukha](https://discourse.julialang.org/u/timofeymukha)\
**Post date:** [September 6, 2017, 6:19pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/12 "2017-09-06T18:19:19Z")

</div>

I think the simplest solution is to just tell the users to give the typename directly as the cmd-line argument and set the time of the argument to ::DataType. But I like your solution to, it is really neat!

I have no problem sacrificing parse eval here, but I use it in another part of the code which reads expressions from a JSON file and evaluates them. This allows the user to define variables in the JSON and then use them in the mesh definition. Like, defining L=1 and then placing a point with the x coordinate equal to L. I’ll have to think on how to do that in another way…

---

<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:** [September 6, 2017, 10:44pm UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/13 "2017-09-06T22:44:24Z")

</div>

Unless you want to support arbitrary julia code, you just write a small parser for the stuff you need (e.g variables).

The use case here seems quite simple. Depending on the value of a string you want to instantiate a type with different parameters. Just direct translation gives you `if str == "..." instantiate(...) ` etc. Creating a new global variable that is read by the module when it is compiled seems like a very round about way of doing it.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [September 7, 2017, 8:13am UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/14 "2017-09-07T08:13:34Z")

</div>

> [@kristoffer.carlsson](#):
>
> just write a small parser

You don’t even need to write a parser. Using Julia’s built-in `parse` works beautifully. (This is one of the things I really love about Julia!) All you need then is an expression evaluator that stores variables in a dictionary instead of polluting the local namespace. I wouldn’t be surprised if there’s already one around somewhere. Otherwise it could probably be done in like ten lines of code.

---

<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:** [September 7, 2017, 8:25am UTC](https://discourse.julialang.org/t/making-ints-size-runtime-selectable-with-no-perfomance-kill/5744/15 "2017-09-07T08:25:49Z")

</div>

If you only support a small amount of syntax, writing your own parser / interpreter should make it easier to give good error messages and reject bad syntax than having to mess around with Julia’s AST.
