# "We find that global names \[sic\] are frequently constants, and declaring them as such greatly improves performance"

**URL:** <https://discourse.julialang.org/t/we-find-that-global-names-sic-are-frequently-constants-and-declaring-them-as-such-greatly-improves-performance/13738>\
**Category:** Performance\
**Created:** [August 19, 2018, 11:41pm UTC](https://discourse.julialang.org/t/we-find-that-global-names-sic-are-frequently-constants-and-declaring-them-as-such-greatly-improves-performance/13738 "2018-08-19T23:41:20Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![chrisjj](https://avatars.discourse-cdn.com/v4/letter/c/a698b9/32.png) [@chrisjj](https://discourse.julialang.org/u/chrisjj)\
**Post date:** [August 19, 2018, 11:41pm UTC](https://discourse.julialang.org/t/we-find-that-global-names-sic-are-frequently-constants-and-declaring-them-as-such-greatly-improves-performance/13738/1 "2018-08-19T23:41:20Z")

</div>

(From [https://docs.julialang.org/en/stable/manual/performance-tips/#man-performance-tips-1](https://docs.julialang.org/en/stable/manual/performance-tips/#man-performance-tips-1) )

Really?? Does the compiler not itself determine that a global variable value is constant, and optimise accordingly?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 19, 2018, 11:48pm UTC](https://discourse.julialang.org/t/we-find-that-global-names-sic-are-frequently-constants-and-declaring-them-as-such-greatly-improves-performance/13738/2 "2018-08-19T23:48:10Z")

</div>

How would one do that?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [August 19, 2018, 11:56pm UTC](https://discourse.julialang.org/t/we-find-that-global-names-sic-are-frequently-constants-and-declaring-them-as-such-greatly-improves-performance/13738/3 "2018-08-19T23:56:01Z")

</div>

Well, if a variable is assigned only once in the program than its a constant. So the compiler should be able to realize that, but it might be expensive to check that.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 20, 2018, 1:00am UTC](https://discourse.julialang.org/t/we-find-that-global-names-sic-are-frequently-constants-and-declaring-them-as-such-greatly-improves-performance/13738/4 "2018-08-20T01:00:16Z")

</div>

There’s this function called `eval` though. It wouldn’t help in the REPL either (which calls `eval` repeatedly). It’s also generally impossible to know how many times a function is called, so it would only help if the assignment is at top-level only and never in a function. The bottom line is that one could only do this by disallowing `eval`ing into a module, which would prevent great packages like [Revise](https://github.com/timholy/Revise.jl) from working. This kind of analysis may seem plausible coming from relatively constrained statically typed languages where the compiler sees all code that can possibly be called before anything is run, but that’s not the way Julia and dynamic languages in general work.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [August 20, 2018, 1:29am UTC](https://discourse.julialang.org/t/we-find-that-global-names-sic-are-frequently-constants-and-declaring-them-as-such-greatly-improves-performance/13738/5 "2018-08-20T01:29:27Z")

</div>

What’s up with the ‘[sic]’? I don’t see any spelling or grammar mistakes.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [August 20, 2018, 5:02am UTC](https://discourse.julialang.org/t/we-find-that-global-names-sic-are-frequently-constants-and-declaring-them-as-such-greatly-improves-performance/13738/6 "2018-08-20T05:02:22Z")

</div>

Perhaps the implication is that “global names” is not the correct term here? @chrisjj, what did you mean?

---

<div class="post-metadata">

**Author:** ![TsurHerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsurherman/32/1234_2.png) [@TsurHerman](https://discourse.julialang.org/u/TsurHerman)\
**Post date:** [August 20, 2018, 6:43am UTC](https://discourse.julialang.org/t/we-find-that-global-names-sic-are-frequently-constants-and-declaring-them-as-such-greatly-improves-performance/13738/7 "2018-08-20T06:43:21Z")

</div>

Would it be possible to treat global variables as anonymous functions returning a constant , and to invalidate all functions depending on it whenever their return type changed?

I think it would achieve the desired result, but may have other implications for example with a global variable that keeps changing types

---

<div class="post-metadata">

**Author:** ![chrisjj](https://avatars.discourse-cdn.com/v4/letter/c/a698b9/32.png) [@chrisjj](https://discourse.julialang.org/u/chrisjj)\
**Post date:** [August 20, 2018, 1:19pm UTC](https://discourse.julialang.org/t/we-find-that-global-names-sic-are-frequently-constants-and-declaring-them-as-such-greatly-improves-performance/13738/8 "2018-08-20T13:19:21Z")

</div>

> Perhaps the implication is that “global names” is not the correct term here

That’s true.

---

<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:** [August 20, 2018, 1:28pm UTC](https://discourse.julialang.org/t/we-find-that-global-names-sic-are-frequently-constants-and-declaring-them-as-such-greatly-improves-performance/13738/9 "2018-08-20T13:28:10Z")

</div>

> [@TsurHerman](#):
>
> Would it be possible to treat global variables as anonymous functions returning a constant , and to invalidate all functions depending on it whenever their return type changed?

So you need to wait for the world age to update just to access a changed global?  
Sounds pretty annoying (note that the discussion here is for non-const globals).
