# Does redefinition of const variables always create unpredictable behavior?

**URL:** https://discourse.julialang.org/t/does-redefinition-of-const-variables-always-create-unpredictable-behavior/103001
**Category:** Performance
**Tags:** question
**Created:** [August 20, 2023, 11:48am UTC](https://discourse.julialang.org/t/does-redefinition-of-const-variables-always-create-unpredictable-behavior/103001 "2023-08-20T11:48:35Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)
#### Post date: [August 20, 2023, 11:48am UTC](https://discourse.julialang.org/t/does-redefinition-of-const-variables-always-create-unpredictable-behavior/103001/1 "2023-08-20T11:48:35Z")

</div>

I have a program that runs fine. I want it to run faster. So I redefined some of its top-level variables to be const, like this

```julia
const myvar = 1

```

Now when I run the program I get the following message:

```julia
WARNING: redefinition of constant myvar. This may fail, cause incorrect answers, or produce other errors.

```

Before changing some variables to be const my if I just redefined the values of some variables, selected the whole code in the editor, and hit Ctrl\_Enter, the program would run fine and yield me the correct values. I could do this many times, and did not have to start a new session.

Should I be worried that the program will not always run correctly just because some variables have been defined as const?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 20, 2023, 11:58am UTC](https://discourse.julialang.org/t/does-redefinition-of-const-variables-always-create-unpredictable-behavior/103001/2 "2023-08-20T11:58:28Z")

</div>

Yes, if some constant value was folded on the compilation, your result may be wrong.

Just wrap all your code in a main function, or pass all data as parameters to your functions.

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [August 20, 2023, 12:07pm UTC](https://discourse.julialang.org/t/does-redefinition-of-const-variables-always-create-unpredictable-behavior/103001/3 "2023-08-20T12:07:13Z")

</div>

You can use `typed globals` if you want to get the performance improvement and not get into constant redefinition issues.

```julia
myvar::Int = 1

```

---

<div class="post-metadata">

### Author: ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)
#### Post date: [August 20, 2023, 3:02pm UTC](https://discourse.julialang.org/t/does-redefinition-of-const-variables-always-create-unpredictable-behavior/103001/4 "2023-08-20T15:02:27Z")

</div>

That sounds like a perfect solution. Will one get the same performance enhancement from `myvar::Int = 1` as one would get from `const myvar::Int = 1`?

---

<div class="post-metadata">

### Author: ![algunion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/algunion/32/51630_2.png) [@algunion](https://discourse.julialang.org/u/algunion)
#### Post date: [August 20, 2023, 3:14pm UTC](https://discourse.julialang.org/t/does-redefinition-of-const-variables-always-create-unpredictable-behavior/103001/5 "2023-08-20T15:14:24Z")

</div>

> [@Soldalma](#):
>
> That sounds like a perfect solution. Will one get the same performance enhancement from `myvar::Int = 1` as one would get from `const myvar::Int = 1`?

Both declaring a constant and performing type annotation help the compiler optimize by conveying the following information: the global variable/constant will not change its type at runtime.

So, for all intents and purposes I think is safe to assume the two approaches will have equal performance. Also, you can benchmark to ensure that happens for your use case.

---

<div class="post-metadata">

### Author: ![Tarny\_GG\_Channie](https://avatars.discourse-cdn.com/v4/letter/t/3bc359/32.png) [@Tarny\_GG\_Channie](https://discourse.julialang.org/u/Tarny_GG_Channie)
#### Post date: [August 20, 2023, 3:45pm UTC](https://discourse.julialang.org/t/does-redefinition-of-const-variables-always-create-unpredictable-behavior/103001/6 "2023-08-20T15:45:07Z")

</div>

Yes.  
The compiler can assume that something is a constant… for example…

```julia
type or paste code here

const SIZE = 3

function something(x)
     for i in 1:SIZE
           println(i)
     end
end

```

Could become

```julia

function something(x)
     println(1)
     println(2)
     println(3)
end

```

Now, this would work under the assumption that SIZE doesn’t change. When that assumption is violated… well… anything can happen.

---

<div class="post-metadata">

### Author: ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)
#### Post date: [August 20, 2023, 4:28pm UTC](https://discourse.julialang.org/t/does-redefinition-of-const-variables-always-create-unpredictable-behavior/103001/7 "2023-08-20T16:28:42Z")

</div>

This answers my question. However, oddly, I created a new version of my program with all the consts removed, but leaving the type declarations. This version runs about 5% faster (!) than the original version with consts and type declarations. I tried several times and this happened consistently.

Possible explanations:

1. Increased compilation time due to the consts.
2. At run time having to check if there were changes. (Although I think this would be the same in both cases)
