Skip to content

Commit 091ac6e

Browse files
Merge pull request #552 from crypto-chassis/example-how-to-reduce-build-time
how to reduce build time
2 parents 85402e7 + cf68e23 commit 091ac6e

9 files changed

Lines changed: 101 additions & 14 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- [Use multiple sessions](#use-multiple-sessions)
4646
- [Override exchange urls](#override-exchange-urls)
4747
- [Connect to a proxy](#connect-to-a-proxy)
48+
- [Reduce build time](#reduce-build-time)
4849
- [Performance Tuning](#performance-tuning)
4950
- [Known Issues and Workarounds](#known-issues-and-workarounds)
5051

@@ -1081,6 +1082,8 @@ Instantiate `Subscription` with the desired `proxyUrl`.
10811082
Subscription subscription("okx", "BTC-USDT", "MARKET_DEPTH", "", "", {}, "172.30.0.146:9000");
10821083
```
10831084

1085+
#### Reduce build time
1086+
The Pimpl (Pointer to Implementation) idiom in C++ can significantly reduce build time. This reduction is achieved by minimizing compilation dependencies and isolating implementation details. See [this example](example/src/reduce_build_time).
10841087

10851088
## Performance Tuning
10861089
* Turn on compiler optimization flags (e.g. `cmake -DCMAKE_BUILD_TYPE=Release ...`).

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,4 @@ add_subdirectory(src/override_exchange_url_at_runtime)
8787
add_subdirectory(src/test_order_latency)
8888
add_subdirectory(src/test_cpu_usage)
8989
add_subdirectory(src/use_multiple_sessions)
90+
add_subdirectory(src/reduce_build_time)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set(NAME reduce_build_time)
2+
project(${NAME})
3+
add_compile_definitions(CCAPI_ENABLE_SERVICE_MARKET_DATA)
4+
add_compile_definitions(CCAPI_ENABLE_EXCHANGE_OKX)
5+
add_executable(${NAME} main.cpp my_session.cpp)
6+
add_dependencies(${NAME} boost rapidjson)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "ccapi_cpp/ccapi_event_handler.h"
2+
#include "my_session.h"
3+
4+
namespace ccapi {
5+
6+
Logger* Logger::logger = nullptr; // This line is needed.
7+
8+
class MyEventHandler : public EventHandler {
9+
public:
10+
void processEvent(const Event& event, Session*) override { std::cout << "Received an event:\n" + event.toPrettyString(2, 2) << std::endl; }
11+
12+
void setMySessionPtr(MySession* mySessionPtr) { this->mySessionPtr = mySessionPtr; }
13+
14+
private:
15+
MySession* mySessionPtr{nullptr};
16+
};
17+
18+
} /* namespace ccapi */
19+
20+
using ::ccapi::MyEventHandler;
21+
using ::ccapi::MySession;
22+
using ::ccapi::SessionConfigs;
23+
using ::ccapi::SessionOptions;
24+
using ::ccapi::Subscription;
25+
26+
int main(int argc, char** argv) {
27+
SessionOptions sessionOptions;
28+
SessionConfigs sessionConfigs;
29+
MyEventHandler eventHandler;
30+
MySession mySession(sessionOptions, sessionConfigs, &eventHandler);
31+
eventHandler.setMySessionPtr(&mySession);
32+
Subscription subscription("okx", "BTC-USDT", "MARKET_DEPTH");
33+
mySession.subscribe(subscription);
34+
std::this_thread::sleep_for(std::chrono::seconds(10));
35+
mySession.stop();
36+
std::cout << "Bye" << std::endl;
37+
return EXIT_SUCCESS;
38+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include "my_session.h"
2+
3+
#include "ccapi_cpp/ccapi_session.h"
4+
5+
namespace ccapi {
6+
7+
MySession::MySession(const SessionOptions& sessionOptions, const SessionConfigs& sessionConfigs, EventHandler* eventHandler)
8+
: ccapiSessionPtr(new Session(sessionOptions, sessionConfigs, eventHandler)) {}
9+
10+
void MySession::subscribe(Subscription& subscription) { this->ccapiSessionPtr->subscribe(subscription); }
11+
12+
void MySession::stop() { this->ccapiSessionPtr->stop(); }
13+
14+
} // namespace ccapi
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef EXAMPLE_SRC_REDUCE_BUILD_TIME_MY_SESSION_H_
2+
#define EXAMPLE_SRC_REDUCE_BUILD_TIME_MY_SESSION_H_
3+
4+
#include "ccapi_cpp/ccapi_session_configs.h"
5+
#include "ccapi_cpp/ccapi_session_options.h"
6+
#include "ccapi_cpp/ccapi_subscription.h"
7+
8+
namespace ccapi {
9+
10+
class EventHandler;
11+
class Session; // forward declaration instead of including full header!
12+
13+
class MySession {
14+
public:
15+
explicit MySession(const SessionOptions& sessionOptions, const SessionConfigs& sessionConfigs, EventHandler* eventHandler);
16+
void subscribe(Subscription& subscription);
17+
void stop();
18+
19+
private:
20+
Session* ccapiSessionPtr{nullptr};
21+
};
22+
23+
} // namespace ccapi
24+
25+
#endif // EXAMPLE_SRC_REDUCE_BUILD_TIME_MY_SESSION_H_

include/ccapi_cpp/ccapi_inflate_stream.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ class InflateStream {
5858
return boost::system::error_code();
5959
}
6060

61-
boost::system::error_code decompress(uint8_t const *buf, size_t len, std::string &out) {
61+
boost::system::error_code decompress(uint8_t const* buf, size_t len, std::string& out) {
6262
if (!this->initialized) {
6363
CCAPI_LOGGER_ERROR("decompress error");
6464
return boost::system::error_code();
6565
}
6666

6767
this->istate.avail_in = len;
68-
this->istate.next_in = const_cast<unsigned char *>(buf);
68+
this->istate.next_in = const_cast<unsigned char*>(buf);
6969
do {
7070
this->istate.avail_out = this->decompressBufferSize;
7171
this->istate.next_out = this->buffer.get();
@@ -74,7 +74,7 @@ class InflateStream {
7474
CCAPI_LOGGER_ERROR("decompress error");
7575
return boost::system::error_code();
7676
}
77-
out.append(reinterpret_cast<char *>(this->buffer.get()), this->decompressBufferSize - this->istate.avail_out);
77+
out.append(reinterpret_cast<char*>(this->buffer.get()), this->decompressBufferSize - this->istate.avail_out);
7878
} while (this->istate.avail_out == 0);
7979
return boost::system::error_code();
8080
}

include/ccapi_cpp/ccapi_url.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Url {
3131
return output;
3232
}
3333

34-
static std::string urlEncode(const std::string &value) {
34+
static std::string urlEncode(const std::string& value) {
3535
std::ostringstream escaped;
3636
escaped.fill('0');
3737
escaped << std::hex;
@@ -50,7 +50,7 @@ class Url {
5050
return escaped.str();
5151
}
5252

53-
static std::string urlDecode(const std::string &value) {
53+
static std::string urlDecode(const std::string& value) {
5454
std::string ret;
5555
char ch;
5656
int i, ii;
@@ -67,18 +67,18 @@ class Url {
6767
return (ret);
6868
}
6969

70-
static std::map<std::string, std::string> convertQueryStringToMap(const std::string &input) {
70+
static std::map<std::string, std::string> convertQueryStringToMap(const std::string& input) {
7171
std::map<std::string, std::string> output;
72-
for (const auto &x : UtilString::split(input, "&")) {
72+
for (const auto& x : UtilString::split(input, "&")) {
7373
auto y = UtilString::split(x, "=");
7474
output.insert(std::make_pair(y.at(0), Url::urlDecode(y.at(1))));
7575
}
7676
return output;
7777
}
7878

79-
static std::string convertMapToQueryString(const std::map<std::string, std::string> &input) {
79+
static std::string convertMapToQueryString(const std::map<std::string, std::string>& input) {
8080
std::string output;
81-
for (const auto &x : input) {
81+
for (const auto& x : input) {
8282
output += x.first;
8383
output += "=";
8484
output += x.second;
@@ -90,10 +90,10 @@ class Url {
9090
return output;
9191
}
9292

93-
static std::string convertMapToFormUrlEncoded(const std::map<std::string, std::string> &input) {
93+
static std::string convertMapToFormUrlEncoded(const std::map<std::string, std::string>& input) {
9494
std::string output;
9595
int i = 0;
96-
for (const auto &x : input) {
96+
for (const auto& x : input) {
9797
output += Url::urlEncode(x.first);
9898
output += "=";
9999
output += Url::urlEncode(x.second);
@@ -104,9 +104,9 @@ class Url {
104104
return output;
105105
}
106106

107-
static std::map<std::string, std::string> convertFormUrlEncodedToMap(const std::string &input) {
107+
static std::map<std::string, std::string> convertFormUrlEncodedToMap(const std::string& input) {
108108
std::map<std::string, std::string> output;
109-
for (const auto &x : UtilString::split(input, "&")) {
109+
for (const auto& x : UtilString::split(input, "&")) {
110110
auto y = UtilString::split(x, "=");
111111
output.insert(std::make_pair(Url::urlDecode(y.at(0)), Url::urlDecode(y.at(1))));
112112
}

test/test_build/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "ccapi_cpp/ccapi_session.h"
22
using ::ccapi::Session;
33

4-
int main(int argc, char **argv) {
4+
int main(int argc, char** argv) {
55
Session session;
66
return EXIT_SUCCESS;
77
}

0 commit comments

Comments
 (0)