Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cpp: Remove code duplication #3995

Merged
merged 3 commits into from
Mar 5, 2023
Merged

Cpp: Remove code duplication #3995

merged 3 commits into from
Mar 5, 2023

Conversation

Krzmbrzl
Copy link
Contributor

@Krzmbrzl Krzmbrzl commented Nov 30, 2022

The same function was defined inside multiple different source files (in an anonymous namespace).
Not only is this generally bad practice from a maintenance point of view, but it also break unity builds (which can speed up compilation times A LOT).

The fix simply consists in introducing a new HashUtils file that contains the single implementation and every source file that uses this function simply has to be linked against that new file (and include the corresponding header).

@Krzmbrzl Krzmbrzl changed the base branch from master to dev November 30, 2022 16:30
@Krzmbrzl Krzmbrzl force-pushed the fix-unit-builds branch 2 times, most recently from 23f2e50 to 60ab5b5 Compare November 30, 2022 17:06
@Krzmbrzl
Copy link
Contributor Author

Krzmbrzl commented Dec 1, 2022

A note on the unity build enabling: Using unity builds can cause the build to fail, even though it worked locally. The reason for such failures is then symbol duplication caused by two source files having defined the exact same symbols. If such source files get mangled together into a single unity build file, then that'll create a compiler error.
However, if you have multiple symbols with the exact same name, you probably should factor them out into a single implementation anyway.
Furthermore, if such problems exist within your code base, simply adding a new file can cause the build to error as then the grouping performed by the unity build may change.

I still think unity builds are tremendously useful, but I can also remove the commit that enables them, if you consider unity builds to cause a too convoluted build.

@parrt
Copy link
Member

parrt commented Feb 19, 2023

@hzeller any experience with unity builds?

@hzeller
Copy link
Contributor

hzeller commented Feb 19, 2023

I have no experience with unity builds, but I can see how that can mess up things.
The change looks ok, but I suggest to

  • Not call it Utils.h. This is very generic. At least 'hash' should be in the name. Maybe HashUtils.h ?
  • Implement it inline directly in the header file and don't add a *.cpp file. The code is small enough that it could measurably slow down things if the compiler had to issue a function call instead of doing things in-place.

The same function was defined inside multiple different source files (in
an anonymous namespace).
Not only is this generally bad practice from a maintenance point of
view, but it also break unity builds (which can speed up compilation
times A LOT).

The fix simply consists in introducing a new Utils file that contains
the single implementation and every source file that uses this function
simply has to be linked against that new file (and include the
corresponding header).

Signed-off-by: Robert Adam <[email protected]>
This should reduce the build time for those steps significantly

Note: Unity builds are enabled only for cmake-based CI builds

Signed-off-by: Robert Adam <[email protected]>
@Krzmbrzl
Copy link
Contributor Author

Krzmbrzl commented Feb 20, 2023

I applied the requested changes and rebased against dev. I was not sure whether you want me to get rid of the commit that enables unity builds, so I left it in. Let me know if you wish to proceed otherwise (removing it has the disadvantage of opening the door to unknowingly break unity builds again in a future commit).

@hzeller
Copy link
Contributor

hzeller commented Feb 20, 2023

I think I would keep the unity build because it will help detect these cases.
However, I'd add a comment in the CI configuration where the unity flags are given to cmake to explain the situation.

Maybe something like

# Using unity build to ensure we don't accidentally break it with duplicate local symbols.
cmake ...

@hzeller
Copy link
Contributor

hzeller commented Feb 20, 2023

On second thought, it might be worthwhile having both builds: with and without unity.
Because unity builds can hide other problems (e.g. forgotten header to include which happens to work in the unity case as everything is put together).

So either

  • keep the original builds as-is and add another one that just adds unity support.
  • Make unity-building a matrix option in the CI build.

@Krzmbrzl
Copy link
Contributor Author

it might be worthwhile having both builds: with and without unity.

Indeed. I adapted the workflow file accordingly 👍

@hzeller
Copy link
Contributor

hzeller commented Feb 23, 2023

I think this looks good. @parrt , handing over to you.

@parrt parrt added this to the 4.12.1 milestone Mar 5, 2023
@parrt
Copy link
Member

parrt commented Mar 5, 2023

Thanks!

@parrt parrt merged commit 3143e88 into antlr:dev Mar 5, 2023
jimidle pushed a commit to jimidle/antlr4 that referenced this pull request Mar 28, 2023
* Cpp: Remove code duplication

The same function was defined inside multiple different source files (in
an anonymous namespace).
Not only is this generally bad practice from a maintenance point of
view, but it also break unity builds (which can speed up compilation
times A LOT).

The fix simply consists in introducing a new Utils file that contains
the single implementation and every source file that uses this function
simply has to be linked against that new file (and include the
corresponding header).

Signed-off-by: Robert Adam <[email protected]>

* CI: Enable unity builds for cpp targets

This should reduce the build time for those steps significantly

Note: Unity builds are enabled only for cmake-based CI builds

Signed-off-by: Robert Adam <[email protected]>

* CI: Perform unity and non-unity cpp builds

Signed-off-by: Robert Adam <[email protected]>

---------

Signed-off-by: Robert Adam <[email protected]>
Signed-off-by: Jim.Idle <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants