-
Notifications
You must be signed in to change notification settings - Fork 12
Open
Description
Hey, just watched your youtube video. Some recommendations:
- Enable compiler optimizations with
-O2or-O3! - If you won't ship the compiled executable anywhere and you don't need it to be portable (this matters on x86), you can also add
-march=nativewhich will optimize the executable to your specific CPU (eg. use extra instructions you have available). threadWorks::sendRequesttakesstd::string payloadby value, which means the string is copied every time. Pushstd::transformback tothreadWorks::runWorkerThreadsince it only has to happen once, and change the parameter type toconst std::string& payload.- You're making comparisons like
payload == "ocean". This could be a problem, because the right-hand side is a null-terminated string, and at runtime it has to traverse the string to find the null byte to determine the string length (unless it's optimized out by the compiler). You can fix it by including<string_view>, addingusing namespace std::string_view_literals;, and replacing such comparisons withpayload == "ocean"sv, which will ensure that the string size is known at compile time. - You can ditch nlohmann library for generating JSON, and you can simply render it manually to a string. The JSON library will model the structure as nested dynamic data structures (std::string, std::vector, std::unordered_map), which only creates a lot of unnecessary overhead. In the general case there are some things that have to be handled (eg. escaping quotes in strings). But in your case with only integers and alphanumeric strings, you can just do it.
oceanBuilder::getRandomFromJsonhas to parse static JSON on every request, copy it to a vector, and then picks a random entry. Either change the source data tostd::vector<std::string>, or push back this work to themainfunction. Same goes formatchBuilderandmyRandom::getRandomVectorFromJSONcalls.- Since this data will never be modified, you can also change the return type to
const std::string*to avoid making copies. Be careful with this though, because there is no reference counting here -- if that string gets deleted (eg. it was local to the function and it goes out of scope), the reference becomes invalid. Enable compiler warnings (-Wall -Wextra) to warn you about mistakes like this. apiClient::sendGETRequestcreatesrequestCombineddynamically. This work can be pushed back too.
To summarize, avoid making allocations.
seandewar
Metadata
Metadata
Assignees
Labels
No labels