ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.06k stars 2.49k forks source link

creating a dynamic library on windows is very slow #18123

Open folkertdev opened 10 months ago

folkertdev commented 10 months ago

Zig Version

0.11.0

Steps to Reproduce and Observed Behavior

We are seeing very slow dynamic library creation on windows. For instance

PS C:\asdfasfasdfasdfsadf> Measure-Command { zig build-lib .\app.obj .\bitcode.obj -lc -dynamic -femit-bin="output" }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 1
Milliseconds      : 793
Ticks             : 17939246
TotalDays         : 2.07630162037037E-05
TotalHours        : 0.000498312388888889
TotalMinutes      : 0.0298987433333333
TotalSeconds      : 1.7939246
TotalMilliseconds : 1793.9246

PS C:\asdfasfasdfasdfsadf> Measure-Command { zig build-lib .\app.obj .\bitcode.obj -lc -femit-bin="output" }

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 81
Ticks             : 813278
TotalDays         : 9.41293981481481E-07
TotalHours        : 2.25910555555556E-05
TotalMinutes      : 0.00135546333333333
TotalSeconds      : 0.0813278
TotalMilliseconds : 81.3278

That is 1793ms for a dynamic library, vs just 81ms for a standard static one. It looks like (but the message moves too quickly) zig is building some sort of mingw artifact (perhaps because of -lc). The building of this artifact is not cached, so happens every time.

To reproduce, download the zip file slow-dylib.zip. In it are two object files. Then run

Measure-Command { zig build-lib .\app.obj .\bitcode.obj -lc -dynamic -femit-bin="output" }
Measure-Command { zig build-lib .\app.obj .\bitcode.obj -lc -femit-bin="output" }

that should reproduce the timings shown earlier.

Expected Behavior

For dynamic library creation (with very small inputs) to complete in reasonable time, maybe by caching more expensive work if needed.

andrewrk commented 10 months ago

Have you tried turning off your virus scanner?

Also, so far this has not demonstrated any caching failure, since you only built the library once. How long does it take the second time?

squeek502 commented 10 months ago

Can confirm the behavior with multiple runs (with a warmup run to populate the cache):

Benchmark 1: zig build-lib .\app.obj .\bitcode.obj -lc -dynamic -femit-bin=output
  Time (mean ± σ):      1.002 s ±  0.151 s    [User: 0.282 s, System: 0.726 s]
  Range (min … max):    0.880 s …  1.197 s    10 runs

Benchmark 2: zig build-lib .\app.obj .\bitcode.obj -lc -femit-bin=output
  Time (mean ± σ):      24.5 ms ±  11.8 ms    [User: 10.4 ms, System: 17.6 ms]
  Range (min … max):    19.6 ms … 103.2 ms    71 runs

Summary
  'zig build-lib .\app.obj .\bitcode.obj -lc -femit-bin=output' ran
   40.95 ± 20.60 times faster than 'zig build-lib .\app.obj .\bitcode.obj -lc -dynamic -femit-bin=output'
andrewrk commented 10 months ago

Thanks!

lukewilliamboswell commented 10 months ago

Have you tried turning off your virus scanner?

I have tested without the Windows real time protection active and there is no change. Otherwise I have no other virus software running on this machine.

Also, so far this has not demonstrated any caching failure, since you only built the library once. How long does it take the second time?

slow-dylib-windows

EDIT: I hadn't noticed @squeek502 benchmark