TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Unity builds lurked into the Firefox Build System

65 点作者 sylvestre大约 2 年前

23 条评论

Dwedit大约 2 年前
Note that this is not referring to the Game Engine Unity. It's just referring to #including .cpp files.
评论 #35843640 未加载
10000truths大约 2 年前
With the advent of LTO, unity builds are mostly a band-aid for poor management of header files. The Linux kernel project was able to net a ~40% reduction in compilation CPU-time just by pruning the contents of some key header files [1].<p>It really boils down to two rules:<p>1. Don&#x27;t declare anything in header files that is only used in one compilation unit. Internal structs and functions should be declared and defined in source files, and internal linkage used wherever possible. gcc and clang&#x27;s -fvisibility=hidden is useful here.<p>2. The more frequently a header file is included (whether transitively or directly), the more it should be split up. If a &quot;common&quot; or &quot;utility&quot; header file is included in 10000 source files, then any struct, function, etc. that you add to that file will have to be parsed 10000 times by the compiler every time you build from scratch, even if only 10 source files actually use the struct&#x2F;function that you added. gcc and clang&#x27;s -H flag is useful here.<p>[1] <a href="https:&#x2F;&#x2F;lore.kernel.org&#x2F;lkml&#x2F;YdIfz+LMewetSaEB@gmail.com&#x2F;" rel="nofollow">https:&#x2F;&#x2F;lore.kernel.org&#x2F;lkml&#x2F;YdIfz+LMewetSaEB@gmail.com&#x2F;</a>
评论 #35845589 未加载
评论 #35848994 未加载
omoikane大约 2 年前
&gt; This generally leads to faster compilation time in part because it aggregates the cost of parsing the same headers over and over.<p>But this also reduces the opportunity to parallelize compilation across multiple files because they have been concatenated into fewer build units, and each unit now requires more memory to deal with the non-header parts. For some build systems and repositories, this actually increases build time.
评论 #35843688 未加载
评论 #35849065 未加载
评论 #35843741 未加载
评论 #35851256 未加载
stephc_int13大约 2 年前
I used Unity builds for my projects basically forever, at some point I discovered the practice had a name and some debates around it.<p>It is a simple thing to do, and the gains are substantial, faster and simpler, less maintenance, especially across different platforms.<p>For big projects I simply cut them into several libraries.<p>I&#x27;ve seen some incredulous reactions, mostly from young coders, and I know that makefiles <i>should</i> be faster, but in practice I never found that to be true.
andybak大约 2 年前
&quot;Lurked into&quot;?<p>You can lurk but surely you can&#x27;t lurk <i>into</i> something?
评论 #35842642 未加载
评论 #35842663 未加载
Y_Y大约 2 年前
This used to be mandatory for nvcc&#x2F;CUDA, if you had multiple source files (not just headers) you had to #include all of them in your main file. It made me very uncomfortable.
thinkling大约 2 年前
I’ve been out of C&#x2F;C++ development for a long time but seem to remember that precompiled headers were a thing back in the day. That approach didn’t have the name space issues pointed out here. Why are precompiled headers not used anymore?
评论 #35843859 未加载
评论 #35845379 未加载
评论 #35843937 未加载
评论 #35849109 未加载
mastax大约 2 年前
Interesting. I&#x27;ve been aware of this technique for years because of the SQLite Amalgamation, but that was always sold as a way to simplify distribution and perhaps improve performance of the binary. I hadn&#x27;t considered it as a build speed optimization, though that seems somewhat obvious in hindsight.
评论 #35843786 未加载
leni536大约 2 年前
Unity builds mean that you can no longer use internal linkage safely anymore, and that&#x27;s not something I like to give up. It forces the codebase to follow a certain style that I don&#x27;t like. Hopefully modules will give the advantage of unity builds without this downside.
Scubabear68大约 2 年前
Headers and C style macros are probably the most unfortunate aspects of C (and by extension, C++).<p>So many hacks in compilers to try to work around this. A shame there is no language level fix for this nonsense.<p>Really wish there could be a C++—- that would improve on C in areas like this, and avoid all the incredible nonsense of C++. And no, not Rust or Go.
评论 #35845620 未加载
评论 #35849181 未加载
评论 #35844647 未加载
评论 #35844911 未加载
StellarScience大约 2 年前
We leverage many third party C++ libraries with complex templates, concepts, and constexpr expressions that seem to require lots of CPU to compile. We&#x27;ve found unity builds to be almost 3X faster, so we make it the default for both developer and CI jobs.<p>But we keep a separate CI job that checks the non-unity build, so developers have to add the right #include statements and can&#x27;t accidentally reference file-scoped functions from other files. While working on a given library or project, developers often disable the unity build for just that project to reduce incremental build times. It seems to offer the benefits of both approaches.<p>Precompiled headers don&#x27;t give nearly the same speedup. We&#x27;re excited for C++ modules of course, but we&#x27;re trying to temper any expectations that modules will improve build speed.
firstlink大约 2 年前
The compilation-unit-per-file model (and in fact the whole concept of linking) are a legacy incremental build solution for C which somehow metastasized into fundamental requirements of building software on current OSes. It is an atrocity and should be disavowed by all developers.
robalni大约 2 年前
I always use unity builds for all my projects now. That combined with using tcc as compiler (for C code) makes builds really fast. Another nice feature of unity builds is that I don&#x27;t need to declare functions twice and keep the declarations synced. It&#x27;s also nice to only have one place to find information about a function; people often put comments in header files that you can miss if you go to the definition.<p>All of those things combined make C programming more enjoyable.
评论 #35843720 未加载
dundarious大约 2 年前
Why can’t static analyzers analyze the main cpp that #include-s the actual code? I don’t understand that point.<p>And what were the resulting affects on build times?
评论 #35842610 未加载
tyleo大约 2 年前
Lots of game studios use Unity builds like this. It saves a massive amount of time. Last I heard it also improves Incredibuild performance which is another popular tool for decreasing build timed.
cpeterso大约 2 年前
Another benefit not mentioned is optimization. The compiler may be able to inline more function calls when function definitions and callers are in the same unified compilation unit.
xcdzvyn大约 2 年前
Link is 404: <a href="https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20230505055736&#x2F;https:&#x2F;&#x2F;serge-sans-paille.github.io&#x2F;pythran-stories&#x2F;how-unity-builds-lurked-into-the-firefox-build-system.html" rel="nofollow">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20230505055736&#x2F;https:&#x2F;&#x2F;serge-san...</a>
zdimension大约 2 年前
The post was renamed, the URL changed accordingly: <a href="https:&#x2F;&#x2F;serge-sans-paille.github.io&#x2F;pythran-stories&#x2F;how-unity-builds-crept-into-the-firefox-build-system.html" rel="nofollow">https:&#x2F;&#x2F;serge-sans-paille.github.io&#x2F;pythran-stories&#x2F;how-unit...</a><p>(HTTP 301 on the old URL would have been appreciated)
kccqzy大约 2 年前
To avoid some of these issues, it can be helpful in a project to require that all files including header files must be compilable on their own. Doesn&#x27;t get rid of all the problems (you can still depend on transitive includes without explicitly including them) but enforces a minimal amount of code hygiene.
zX41ZdbW大约 2 年前
Tried Unity builds recently for ClickHouse, but without success: <a href="https:&#x2F;&#x2F;github.com&#x2F;ClickHouse&#x2F;ClickHouse&#x2F;pull&#x2F;18952#issuecomment-819901667">https:&#x2F;&#x2F;github.com&#x2F;ClickHouse&#x2F;ClickHouse&#x2F;pull&#x2F;18952#issuecom...</a>
nine_k大约 2 年前
It&#x27;s another reminder how &lt;expletives omitted&gt; C++ is, but for a long time nothing better existed.
Grum9大约 2 年前
Been using unity builds forever. The trick is to also have a standard build and try to compile it every week or so to catch anything that might have been missed, like a source file that is missing a header and wont compile alone because it got the header through the unity build ordering.
vinyl7大约 2 年前
Compilation units are a relic of a time where computers only had a few KB of memory. At this point computers are fast enough and have enough memory to compile the whole thing in one go faster than whatever gains doing change detection and linking will have.
评论 #35842177 未加载
评论 #35842876 未加载
评论 #35842258 未加载
评论 #35842664 未加载
评论 #35843349 未加载