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

Use of pImpl pattern #1327

Closed
dimitry-ishenko opened this issue Jul 2, 2020 · 5 comments
Closed

Use of pImpl pattern #1327

dimitry-ishenko opened this issue Jul 2, 2020 · 5 comments

Comments

@dimitry-ishenko
Copy link
Contributor

I see the pImpl pattern being heavily used in the CCG server code and I was wondering what is the motivation for it.

@ronag
Copy link
Member

ronag commented Jul 2, 2020

I think the link you refer to explains it rather well? Herb also has lot's of posts on the topic, e.g. https://herbsutter.com/gotw/_100/

@dimitry-ishenko
Copy link
Contributor Author

I think the link you refer to explains it rather well? Herb also has lot's of posts on the topic, e.g. https://herbsutter.com/gotw/_100/

@ronag the links do indeed explain what pImpl pattern is, but not the motivation for its use in CCG server.

From the first link: "This technique is used to construct C++ library interfaces with stable ABI and to reduce compile-time dependencies."

  • CCG server is not a library and there is no need to worry about stable ABI.

  • Compile-time dependencies is likewise not a big issue IMHO. (a) Most of us have access to fairly powerful machines. And (b) is there really a meaningful reduction in compilation time? I see a lot of claims on the interwebs and fancy words being thrown around (eg, compile-time firewall...), but I couldn't find any concrete examples (disclaimer: didn't look very hard).

From the 2nd link: "In C++, when anything in a header file class definition changes, all users of that class must be recompiled – even if the only change was to the private class members that the users of the class cannot even access."

  • This again is a moot point IMHO. Build jobs are mostly done by bots from scratch anyway (and on top of that include spinning up a container, installing dependencies, etc, etc.) And for developers building on their own machines, CMake is smart enough to figure out what needs to be rebuilt. OK one will have to recompile a few extra files, but again is there really a meaningful gain? It's not like we are talking minutes vs hours. Maybe a few extra minutes? Most of us will fire off compilation and work on something else in the meantime.

Did I miss anything?

@ronag
Copy link
Member

ronag commented Jul 2, 2020

It's compile time. When working on CCG and compiling dozens of times while developing even small compile time differences make huge difference in developer ergonomics. At least for me. Also, not all of use have access to powerful machines. Some of us work on laptops.

is there really a meaningful reduction in compilation time

Absolutely. Depends though on how many other files depend on the file being modified.

@dimitry-ishenko
Copy link
Contributor Author

It's compile time. When working on CCG and compiling dozens of times while developing even small compile time differences make huge difference in developer ergonomics. At least for me. Also, not all of use have access to powerful machines. Some of us work on laptops.

If that's the only issue, @dotarmin could we organize a proper development laptop for @ronag? 😜

In all seriousness though, we all know there is no free lunch. So, the gain at compile time comes at a cost of runtime overhead. In your defense, I myself don't have any concrete benchmarks to show how that affects runtime performance, but I do remember reading that pImpl inhibits certain types of optimizations (unless LTO is enabled).

And from maintenance perspective, each pImpl class requires a set of boilerplate forwarding functions, which may also lead to bugs. The article I've linked to in the beginning has a bit more details.

Anyway, not trying to knock on you. As one of the top contributors to the project, I have but full respect for you. I mainly just wanted to clarify it for myself and maybe inject some critical thinking 😀

@ronag
Copy link
Member

ronag commented Jul 2, 2020

In all seriousness though, we all know there is no free lunch. So, the gain at compile time comes at a cost of runtime overhead.

It's negligible. We don't use it in any performance sensitive parts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants