Skip to content

Commit 872f8ab

Browse files
[refactor] Apply forward declaration as much as possible (#64)
* [refactor] Apply forward declaration as much as possible Fixes #60 ### Motivation The includes in pulsar-client-cpp is very casual. There are a lot of implicit includes and the forward declaration is not used much. For example, if `lib/ClientConnection.h` was modified, 27 files would be recompiled. The other problem is the `SortIncludes` attribute in `.clang-format` file is false. It might be okay in early days. However, as the project grows, including many headers without ordering brings a very bad experience. Combining with the very few usages of the forward declaration, it's hard to determine whether a header is still required after a change. ### Modifications Apply forward declarations as much as possible and change the `SortIncludes` to true in `.clang-format` file. It's special for the `PulsarApi.pb.h` file because the size of this header is over 1 MiB. For classes we can use forward declaration, but for enumerations we have to include this header. To solve this problem, `ProtoApiEnums.h` is added to define some constant integers that can be cast implicitly from the enumerations. If we want to use enumerations from `PulsarApi.pb.h`, we can include `ProtoApiEnums.h` instead. Finally, to unify the include rules, in `lib/*.h`, if we want to include a header (e.g. `xxx.h`) from the same directory, just use `#include "xxx.h"`. Don't use `#include "lib/xxx.h"` or `#include <lib/xxx.h>` in headers of `lib/` directory. ### Improvements Since forward declaration is applied everywhere now, take `lib/ClientConnection.h` for example. After this patch, only 10 files needs to be recompiled, while 27 files would be recompiled before. This patch also reduces the binary size and speeds up the compilation time. Binary size: - `libpulsar.a`: 319234696 (305 MiB) -> 286716564 (274 MiB) - `libpulsar.so`: 110162496 (106 MiB) -> 102428456 (98 MiB) Compilation time with the following commands: ```bash cmake -B build -DBUILD_TESTS=OFF cmake --build build -j8 ``` * Fix MSVC build
1 parent 7bb6402 commit 872f8ab

File tree

278 files changed

+1557
-1123
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

278 files changed

+1557
-1123
lines changed

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
BasedOnStyle: Google
2020
IndentWidth: 4
2121
ColumnLimit: 110
22-
SortIncludes: false
22+
SortIncludes: true
2323
BreakBeforeBraces: Custom
2424
BraceWrapping:
2525
AfterEnum: true

examples/SampleAsyncProducer.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19+
#include <pulsar/Client.h>
20+
1921
#include <iostream>
2022
#include <thread>
2123

22-
#include <pulsar/Client.h>
23-
24-
#include <lib/LogUtils.h>
24+
#include "lib/LogUtils.h"
2525

2626
DECLARE_LOG_OBJECT()
2727

examples/SampleConsumer.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
#include <iostream>
20-
2119
#include <pulsar/Client.h>
2220

23-
#include <lib/LogUtils.h>
21+
#include <iostream>
22+
23+
#include "lib/LogUtils.h"
2424

2525
DECLARE_LOG_OBJECT()
2626

examples/SampleConsumerListener.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
#include <iostream>
20-
2119
#include <pulsar/Client.h>
2220

23-
#include <lib/LogUtils.h>
21+
#include <iostream>
22+
23+
#include "lib/LogUtils.h"
2424

2525
DECLARE_LOG_OBJECT()
2626

examples/SampleProducer.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
#include <iostream>
20-
2119
#include <pulsar/Client.h>
2220

23-
#include <lib/LogUtils.h>
21+
#include <iostream>
22+
23+
#include "lib/LogUtils.h"
2424

2525
DECLARE_LOG_OBJECT()
2626

include/pulsar/Authentication.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
#ifndef PULSAR_AUTHENTICATION_H_
2020
#define PULSAR_AUTHENTICATION_H_
2121

22+
#include <pulsar/Result.h>
2223
#include <pulsar/defines.h>
23-
#include <vector>
24-
#include <string>
24+
25+
#include <functional>
2526
#include <map>
2627
#include <memory>
27-
#include <pulsar/Result.h>
28-
#include <functional>
28+
#include <string>
29+
#include <vector>
2930

3031
namespace pulsar {
3132

include/pulsar/BatchReceivePolicy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#define BATCH_RECEIVE_POLICY_HPP_
2121

2222
#include <pulsar/defines.h>
23+
2324
#include <memory>
2425

2526
namespace pulsar {

include/pulsar/BrokerConsumerStats.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
#ifndef PULSAR_CPP_BROKERCONSUMERSTATS_H
2020
#define PULSAR_CPP_BROKERCONSUMERSTATS_H
2121

22-
#include <pulsar/defines.h>
23-
#include <string.h>
24-
#include <iostream>
22+
#include <pulsar/ConsumerType.h>
2523
#include <pulsar/Result.h>
24+
#include <pulsar/defines.h>
25+
2626
#include <functional>
27+
#include <iostream>
2728
#include <memory>
28-
#include <pulsar/ConsumerType.h>
2929

3030
namespace pulsar {
3131
class BrokerConsumerStatsImplBase;

include/pulsar/Client.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,18 @@
1919
#ifndef PULSAR_CLIENT_HPP_
2020
#define PULSAR_CLIENT_HPP_
2121

22-
#include <pulsar/defines.h>
22+
#include <pulsar/ClientConfiguration.h>
23+
#include <pulsar/ConsoleLoggerFactory.h>
2324
#include <pulsar/Consumer.h>
25+
#include <pulsar/FileLoggerFactory.h>
26+
#include <pulsar/Message.h>
27+
#include <pulsar/MessageBuilder.h>
2428
#include <pulsar/Producer.h>
2529
#include <pulsar/Reader.h>
2630
#include <pulsar/Result.h>
27-
#include <pulsar/Message.h>
28-
#include <pulsar/MessageBuilder.h>
29-
#include <pulsar/ClientConfiguration.h>
3031
#include <pulsar/Schema.h>
31-
#include <pulsar/ConsoleLoggerFactory.h>
32-
#include <pulsar/FileLoggerFactory.h>
32+
#include <pulsar/defines.h>
33+
3334
#include <string>
3435

3536
namespace pulsar {

include/pulsar/ClientConfiguration.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#ifndef PULSAR_CLIENTCONFIGURATION_H_
2020
#define PULSAR_CLIENTCONFIGURATION_H_
2121

22-
#include <pulsar/defines.h>
2322
#include <pulsar/Authentication.h>
2423
#include <pulsar/Logger.h>
24+
#include <pulsar/defines.h>
2525

2626
namespace pulsar {
2727
class PulsarWrapper;

0 commit comments

Comments
 (0)