You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
support cache hits with differently named zig lib directories which have the same contents #13050
acceptedThis proposal is planned.enhancementSolving this issue will likely involve adding new logic or components to the codebase.frontendTokenization, parsing, AstGen, Sema, and Liveness.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.
When using a relative zig lib directory (ZIG_LIB_DIR=lib) and when building from different directories, libc shims get rebuilt for every working directory. This is a particularly nasty problem for Bazel, which uses a different directory for each "sandbox" (i.e. execution unit), but relative directory to ZIG_LIB_DIR due to reproducibility. In our case, it causes tens of gigabytes of libc++.a in zig cache directory, besides the CPU usage to generate those.
Steps to reproduce
Setup:
~$ mkdir -p a1 a2
~$ for d in a1 a2; do ln -s /code/zig/lib $d/lib; done
Directory a1:
~$ cd a1
~/a1$ time ZIG_LIB_DIR=lib zig cc -target aarch64-linux-gnu.2.28 - -o main
LLD Link... ld.lld: error: cannot open -: No such file or directory
real 0m16.092s
user 1m18.706s
sys 0m6.679s
Observe the zig c++ command takes ~16 seconds, which means it built the glibc shim. Now switch to directory a2:
~$ cd a2
~/a2$ time ZIG_LIB_DIR=lib zig c++ -target aarch64-linux-gnu.2.28 - -o main
LLD Link... ld.lld: error: cannot open -: No such file or directory
real 0m18.914s
user 1m31.513s
sys 0m7.670s
If we run this again in a2, we see the latency is significantly decreased:
~$ cd a2
~/a2$ time ZIG_LIB_DIR=lib zig c++ -target aarch64-linux-gnu.2.28 - -o main
LLD Link... ld.lld: error: cannot open -: No such file or directory
real 0m0.038s
user 0m0.017s
sys 0m0.021s
Expected Behavior
It takes <1 second to run the a2 from from the first attempt.
Ideally, manifest files in the global cache would contain only absolute files, since the global cache is shared among multiple projects, each with a potentially different working directory.
Meanwhile, manifest files in the local cache would ideally contain only paths relative to the local project root, so that the project directory could be moved to a new location and the cache would continue functioning seamlessly.
I had a look around and any time we call Cache.addFile, it calls fs.path.resolve on the input path to convert from relative path to absolute path. The one exception is Cache.addFilePostContents, which accepts a pre-resolved path. One of the callsites does this correctly, but the other does not, and I think that is the cause of the bug.
I don't think resolving symlinks is necessary or desirable.
I'll propose a different PR shortly to solve this problem.
I opened #13071 after which no relative paths show up in the global cache manifest.
However, the system will still consider two paths to be different even if they are symlinks resolving to the same directory, leaving the original problem detailed in this issue unresolved. However, might I suggest that the link is resolved prior to setting the environment variable? For example, this solves the problem:
[nix-shell:~/Downloads/zig/build-release]$ cd a1
[nix-shell:~/Downloads/zig/build-release/a1]$ ZIG_LIB_DIR=$(readlink lib) ../stage4/bin/zig build-exe ../../test/standalone/hello_world/hello.zig
[nix-shell:~/Downloads/zig/build-release/a1]$ cd ../a2
[nix-shell:~/Downloads/zig/build-release/a2]$ ZIG_LIB_DIR=$(readlink lib) ../stage4/bin/zig build-exe ../../test/standalone/hello_world/hello.zig
With this strategy, only one copy of the global static libraries such as compiler_rt.a and libc++.a is generated.
However, might I suggest that the link is resolved prior to setting the environment variable? For example, this solves the problem:
Unfortunately, the Bazel's symlink to zig's lib is an absolute path, which brings back #12980 back again:. If we readlink it, the ZIG_LIB_DIR becomes absolute, which causes the result of zig cc -M to be absolute, which beats Bazel's global cache.
As a result, two machines/people with a different sandbox path (e.g. /home/motiejus and /home/john is enough to make a difference a difference) cannot share the compiled artifacts.
Hmm I'm actually not able to reproduce the problem. Here is an attempt where I am getting what I understand to be the desired behavior, based on our discussion:
I spent a couple of days investigating the intersection of bazel and zig-cc with regards to build performance issues. There is one more aspect to it:
By default, Bazel creates a symlink farm per sandbox. Sandbox is created per action (action is a single compilation step). Zig has ~16k files, thus it takes time to create and teardown every sandbox. This is the cause of the current slowness that I've mentioned in other channels.
To overcome (1), Bazel supports sandboxfs. It is a FUSE-mounted directory that makes the "sandbox" read-only, whlist avoiding the symlink farm. That could be used with ZIG_LIB_DIR=external/zig_sdk/lib. This fixes the performance issue with a symlink farm (16k symlinks are replaced with a single mount(2), however, as far as Zig is concerned, it's a completely different directory, which messes up global caching. With sandboxfs, even Cache: resolve symlinks when manipulating file names #13051 does not help.
To sum up: for Zig to be friendly with Bazel, we need to find a way for zig to understand that different ZIG_LIB_DIRs (read: different sandboxes) may actually refer to identical directory contents.
Summary of a voice chat that @motiejus and I had: It looks like this problem can be solved by a combination of two things:
Not adding the zig lib directory path into cache hash. It's enough to consider the file contents of every file.
In cache manifest files, have a special way to encode a "zig lib directory" prefix. Files from the zig lib directory in the global cache will not be absolute file paths, but be relative to the zig lib directory, and indicate that they are such.
This enhancement will benefit the portability of zig because "absolute file paths" are problematic for some systems, such as WASI, and operating systems that do not have realpath.
changed the title [-]ZIG_LIB_DIR: relative paths cause shims to be rebuilt[/-][+]support cache hits with differently named zig lib directories which have the same contents[/+]on Nov 20, 2022
acceptedThis proposal is planned.enhancementSolving this issue will likely involve adding new logic or components to the codebase.frontendTokenization, parsing, AstGen, Sema, and Liveness.proposalThis issue suggests language modifications. If it also has the "accepted" label then it is planned.
Activity
motiejus commentedon Oct 3, 2022
I've observed that some of the
~/.cache/zig/h/*.txtfiles (in my understanding, cache manifests) contain non-canonicalized paths:IMO I would expcect it to be:
Or even better, if at all possible?
Though I did not find where this path is actually constructed.
Cache: resolve symlinks when manipulating file names
Cache: resolve symlinks when manipulating file names
Cache: resolve symlinks when manipulating file names
Cache: resolve symlinks when manipulating file names
andrewrk commentedon Oct 5, 2022
Ideally, manifest files in the global cache would contain only absolute files, since the global cache is shared among multiple projects, each with a potentially different working directory.
Meanwhile, manifest files in the local cache would ideally contain only paths relative to the local project root, so that the project directory could be moved to a new location and the cache would continue functioning seamlessly.
I had a look around and any time we call
Cache.addFile, it callsfs.path.resolveon the input path to convert from relative path to absolute path. The one exception isCache.addFilePostContents, which accepts a pre-resolved path. One of the callsites does this correctly, but the other does not, and I think that is the cause of the bug.I don't think resolving symlinks is necessary or desirable.
I'll propose a different PR shortly to solve this problem.
stage2: resolve file before putting them into cache
andrewrk commentedon Oct 5, 2022
I opened #13071 after which no relative paths show up in the global cache manifest.
However, the system will still consider two paths to be different even if they are symlinks resolving to the same directory, leaving the original problem detailed in this issue unresolved. However, might I suggest that the link is resolved prior to setting the environment variable? For example, this solves the problem:
With this strategy, only one copy of the global static libraries such as compiler_rt.a and libc++.a is generated.
motiejus commentedon Oct 5, 2022
Unfortunately, the Bazel's symlink to zig's
libis an absolute path, which brings back #12980 back again:. If we readlink it, theZIG_LIB_DIRbecomes absolute, which causes the result ofzig cc -Mto be absolute, which beats Bazel's global cache.As a result, two machines/people with a different sandbox path (e.g.
/home/motiejusand/home/johnis enough to make a difference a difference) cannot share the compiled artifacts.motiejus commentedon Oct 5, 2022
In other words, I am looking for the right
ZIG_LIB_DIR, so the result ofzig cc -M file.ccontains only relative paths.stage2: resolve file before putting them into cache
andrewrk commentedon Oct 12, 2022
Hmm I'm actually not able to reproduce the problem. Here is an attempt where I am getting what I understand to be the desired behavior, based on our discussion:
Here you can see that all the files are relative paths.
9 remaining items
motiejus commentedon Oct 26, 2022
I spent a couple of days investigating the intersection of bazel and zig-cc with regards to build performance issues. There is one more aspect to it:
ZIG_LIB_DIR=external/zig_sdk/lib. This fixes the performance issue with a symlink farm (16k symlinks are replaced with a singlemount(2), however, as far as Zig is concerned, it's a completely different directory, which messes up global caching. With sandboxfs, even Cache: resolve symlinks when manipulating file names #13051 does not help.To sum up: for Zig to be friendly with Bazel, we need to find a way for zig to understand that different
ZIG_LIB_DIRs (read: different sandboxes) may actually refer to identical directory contents.andrewrk commentedon Nov 14, 2022
Summary of a voice chat that @motiejus and I had: It looks like this problem can be solved by a combination of two things:
This enhancement will benefit the portability of zig because "absolute file paths" are problematic for some systems, such as WASI, and operating systems that do not have realpath.
I will look into this over the next week.
Cache: introduce prefixes to manifests
Cache: introduce prefixes to manifests
Cache: introduce prefixes to manifests
Cache: introduce prefixes to manifests
[-]ZIG_LIB_DIR: relative paths cause shims to be rebuilt[/-][+]support cache hits with differently named zig lib directories which have the same contents[/+]Cache: introduce prefixes to manifests
Cache: introduce prefixes to manifests
Cache: introduce prefixes to manifests
resolve ZIG_LIB_DIR before passing it to zig
README: rollback to v0.9.2
upgrade to zig 0.11.0-dev.324+f61c5f3f5