# @debug has massive performance impact on Windows

**URL:** <https://discourse.julialang.org/t/debug-has-massive-performance-impact-on-windows/103974>\
**Category:** Performance\
**Created:** [September 18, 2023, 9:53am UTC](https://discourse.julialang.org/t/debug-has-massive-performance-impact-on-windows/103974 "2023-09-18T09:53:57Z")\
**Posts on this page:** 6\
**Page:** 2

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 18, 2023, 1:59pm UTC](https://discourse.julialang.org/t/debug-has-massive-performance-impact-on-windows/103974/21 "2023-09-18T13:59:11Z")

</div>

> [@ThummeTo](#):
>
> So could this be Julia-Default?

Don’t think so. It’s important that it primarily respect the setting in `ENV` by default, but checking `ENV` is what causes the problem.

---

<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 18, 2023, 2:07pm UTC](https://discourse.julialang.org/t/debug-has-massive-performance-impact-on-windows/103974/22 "2023-09-18T14:07:20Z")

</div>

One could do something ugly and special case this in the environment lookup on Windows to prevent allocation for this specific case with the argument that the overhead of the string compare is negligible to the allocation from `cwstring`. But I am not sure that is acceptable.

```diff
diff --git a/base/env.jl b/base/env.jl
index 077b3a4ed2..c3c60da9c0 100644
--- a/base/env.jl
+++ b/base/env.jl
@@ -7,8 +7,10 @@ if Sys.iswindows()
     _hasenv(s::Vector{UInt16}) = _getenvlen(s) != 0 || Libc.GetLastError() != ERROR_ENVVAR_NOT_FOUND
     _hasenv(s::AbstractString) = _hasenv(cwstring(s))
 
+ const JULIA_DEBUG_CWSTRING = cwstring("JULIA_DEBUG")
+
     function access_env(onError::Function, str::AbstractString)
- var = cwstring(str)
+ var = str == "JULIA_DEBUG" ? JULIA_DEBUG_CWSTRING : cwstring(str)
         len = _getenvlen(var)
         if len == 0
             return Libc.GetLastError() != ERROR_ENVVAR_NOT_FOUND ? "" : onError(str)

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [September 18, 2023, 4:05pm UTC](https://discourse.julialang.org/t/debug-has-massive-performance-impact-on-windows/103974/23 "2023-09-18T16:05:25Z")

</div>

Nice idea @kristoffer.carlsson. I’ve created a PR with that diff since I know you’re a busy guy, and it’s probably best to discuss the merits or alternatives to this idea on github than here.

> <https://github.com/JuliaLang/julia/pull/51371>
>
> This is just me proposing a suggestion from @KristofferC in https://discourse.ju…lialang.org/t/debug-has-massive-performance-impact/103974/22, it's all his code / idea, not mine.
> 
> The idea behind this is that people have observed that \[using \`@debug\` is significantly more costly on Windows than other platforms\](https://discourse.julialang.org/t/debug-has-massive-performance-impact/103974), even though we have documented in that manual that it should be a really cheap operation. @KristofferC suggests this is due to the fact that \[checking environment variables in Windows is more costly\](https://discourse.julialang.org/t/debug-has-massive-performance-impact/103974/18). 
> 
> The idea here is that we preallocate a \`Cwstring\` on Windows that just holds the text \`"JULIA\_DEBUG"\`, so that if \`access\_env(f, "JULIA\_DEBUG")\` gets called, we don't need to create a new \`Cwstring\` and then throw it away each time.

---

<div class="post-metadata">

**Author:** ![George9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george9000/32/23619_2.png) [@George9000](https://discourse.julialang.org/u/George9000)\
**Post date:** [September 18, 2023, 6:11pm UTC](https://discourse.julialang.org/t/debug-has-massive-performance-impact-on-windows/103974/24 "2023-09-18T18:11:24Z")

</div>

Confirm @skleinbo on M1.

```julia
julia> @btime my_sum($x);
  173.135 ns (0 allocations: 0 bytes)

julia> @btime my_sum_debug($x);
  285.606 ns (0 allocations: 0 bytes)

julia> @btime sum($x)
  173.192 ns (0 allocations: 0 bytes)

```

> **versioninfo**
>
> ```julia
> julia> versioninfo()
> Julia Version 1.9.3
> Commit bed2cd540a (2023-08-24 14:43 UTC)
> 
> Platform Info:
> OS: macOS (arm64-apple-darwin21.6.0)
> CPU: 10 × Apple M1 Max
> WORD_SIZE: 64
> LIBM: libopenlibm
> LLVM: libLLVM-14.0.6 (ORCJIT, apple-m1)
> Threads: 5 on 8 virtual cores
> Environment:
> JULIA_NUM_THREADS = 4
> JULIA_EDITOR = Emacs
> JULIA_PKG_DEVDIR = /Users/foo/Documents/julia/dev
> 
> ```

---

<div class="post-metadata">

**Author:** ![ThummeTo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thummeto/32/26105_2.png) [@ThummeTo](https://discourse.julialang.org/u/ThummeTo)\
**Post date:** [September 19, 2023, 11:25am UTC](https://discourse.julialang.org/t/debug-has-massive-performance-impact-on-windows/103974/25 "2023-09-19T11:25:45Z")

</div>

So cool that this (from thread and feedbacks to PR) was resolved within hours 👍  
That’s something that impresses me again and again about Julia (and its great community).  
Thank you all!

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 19, 2023, 2:44pm UTC](https://discourse.julialang.org/t/debug-has-massive-performance-impact-on-windows/103974/26 "2023-09-19T14:44:33Z")

</div>

The allocation you get are not Windows specific. Still, while I get them too on Linux with 1.9.2, I do not get 10x slowdown, but they go away in 1.9.3, so likely on Windows too.

I noticed `@btime my_sum_debug($x)` gave different results when repeating, so I tested, and both it and non-debug have very large variance despite allocations gone:

```julia
julia> @benchmark my_sum_debug($x)
BenchmarkTools.Trial: 10000 samples with 343 evaluations.
 Range (min … max): 259.067 ns … 501.595 ns ┊ GC (min … max): 0.00% … 0.00%
 Time (median): 285.913 ns ┊ GC (median): 0.00%
 Time (mean ± σ): 291.185 ns ± 20.333 ns ┊ GC (mean ± σ): 0.00% ± 0.00%

```

And I get only 501.595/161.899 3.1x slower max for degub vs min for non-debug.

> [@Mason](#):
>
> Importantly, I’m not incurring any allocations from the debug statements, and they’re only taking ~60 extra nanoseconds rather than ~600 extra nanoseconds.

Can you post Windows results for 1.9.3? Maybe allocations and/or `@debug` is unusually slow on Windows, even though I saw nothing Windows specific with the macro definition. Then it’s just slow because of allocations, also other code?

[Previous page](https://discourse.julialang.org/t/debug-has-massive-performance-impact-on-windows/103974.md?page=1)
