Skip to content

Commit

Permalink
improvement: replace likely macro with C++20 attribute (SFTtech#1517)
Browse files Browse the repository at this point in the history
Replace likely macro with C++20 attribute
  • Loading branch information
derekfrogget authored Jun 18, 2023
1 parent 0ce207f commit 09fec55
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 25 deletions.
1 change: 1 addition & 0 deletions copying.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ _the openage authors_ are:
| Matthias Geiger | CountOmega | matthias dawt geiger1024 à outlook dawt com |
| Yuvraj Tetarwal | YuviTz1 | yuvi56789 à gmail dawt com |
| Tarun Samanta | TS | [email protected] |
| Derek Frogget | FoggyLight | [email protected] |

If you're a first-time committer, add yourself to the above list. This is not
just for legal reasons, but also to keep an overview of all those nicknames.
Expand Down
4 changes: 2 additions & 2 deletions libopenage/curve/keyframe_container.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2017-2019 the openage authors. See copying.md for legal info.
// Copyright 2017-2023 the openage authors. See copying.md for legal info.

#pragma once

Expand Down Expand Up @@ -411,7 +411,7 @@ KeyframeContainer<T>::erase_group(const time_t &time,
// erase elements until all element with that time are purged
while (at != std::end(this->container) and at->time == time) {
at = this->container.erase(at);
if (likely(at != std::begin(this->container))) {
if (at != std::begin(this->container)) [[likely]] {
--at;
}
}
Expand Down
5 changes: 3 additions & 2 deletions libopenage/datastructure/pairing_heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ class PairingHeap final {
// 1. link root children pairwise, last node may be alone
element_t first_pair = nullptr;
element_t previous_pair = nullptr;

while (current_sibling != nullptr) [[unlikely]] {
element_t link0 = current_sibling;
element_t link1 = current_sibling->next_sibling;
Expand Down Expand Up @@ -377,7 +378,7 @@ class PairingHeap final {
* O(1)
*/
void decrease(const element_t &node) {
if (likely(node != this->root_node)) {
if (node != this->root_node) [[likely]] {
// cut out the node and its subtree
node->loosen();
this->root_node = node->link_with(this->root_node);
Expand All @@ -394,7 +395,7 @@ class PairingHeap final {
* O(1) (but slower than decrease), and O(pop) when node is the root.
*/
void update(const element_t &node) {
if (likely(node != this->root_node)) {
if (node != this->root_node) [[likely]] {
this->unlink_node(node);
this->push_node(node);
}
Expand Down
3 changes: 2 additions & 1 deletion libopenage/error/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Error::Error(const log::message &msg, bool generate_backtrace, bool store_cause)
:
std::runtime_error{runtime_error_message},
msg(msg) {

if (enable_break_on_create) [[unlikely]] {
BREAKPOINT;
}
Expand All @@ -42,7 +43,7 @@ void Error::store_cause() {
// we could simply do this->cause = std::current_exception(),
// but we need to trim the cause Error's backtrace.

if (likely(!std::current_exception())) {
if (!std::current_exception()) [[likely]] {
return;
}

Expand Down
4 changes: 3 additions & 1 deletion libopenage/pyinterface/exctranslate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ void translate_exc_cpp_to_py() {
throw;

} catch (PyException &exc) {

if (raise_cpp_pyexception == nullptr) [[unlikely]] {
throw Error(MSG(err) <<
"raise_pyexception_in_py is uninitialized; "
Expand All @@ -66,6 +67,7 @@ void translate_exc_cpp_to_py() {
raise_cpp_pyexception(&exc);

} catch (Error &exc) {

if (raise_cpp_error == nullptr) [[unlikely]] {
throw Error(MSG(err) <<
"raise_error_in_py is uninitialized; "
Expand Down Expand Up @@ -95,7 +97,7 @@ void translate_exc_py_to_cpp() {
"can't check for and translate Python exception to C++ exception.");
}

if (likely(not check_for_py_exception())) {
if (not check_for_py_exception()) [[likely]] {
// no exception has occurred.
return;
}
Expand Down
12 changes: 0 additions & 12 deletions libopenage/util/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@
#endif // HAVE_SSIZE_T
#endif // _MSC_VER

/*
* Branch prediction tuning.
* the expression is expected to be true (=likely) or false (=unlikely).
*
* btw, this implementation was taken from the Linux kernel.
*/
#if defined(__GNUC__)
#define likely(x) __builtin_expect(!!(x), 1)
#else
#define likely(x) (x)
#endif


/**
* Software breakpoint if you're too lazy
Expand Down
2 changes: 1 addition & 1 deletion libopenage/util/compress/lzxd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ int HuffmanTable<maxsymbols_p, tablebits_p, allow_empty>::read_sym() {
unsigned i = 1 << (sizeof(lzx->bits.bit_buffer) * 8 - tablebits);
while (sym >= maxsymbols) {
// huff_traverse
if (((i >>= 1) == 0)) [[unlikely]] {
if ((i >>= 1) == 0) [[unlikely]] {
throw Error(MSG(err) << "huff_error in huff_traverse");
}
sym = table[(sym << 1) | ((lzx->bits.bit_buffer & i) ? 1 : 0)];
Expand Down
4 changes: 2 additions & 2 deletions libopenage/util/csv.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2013-2017 the openage authors. See copying.md for legal info.
// Copyright 2013-2023 the openage authors. See copying.md for legal info.

#include "csv.h"

Expand Down Expand Up @@ -48,7 +48,7 @@ CSVCollection::CSVCollection(const Path &entryfile_path) {
continue;
}

if (likely(current_file.size() > 0)) {
if (current_file.size() > 0) [[likely]] {
// add line to the current file linelist
this->data.at(current_file).push_back(line);
}
Expand Down
6 changes: 3 additions & 3 deletions libopenage/util/path.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2015-2019 the openage authors. See copying.md for legal info.
// Copyright 2015-2023 the openage authors. See copying.md for legal info.

#include "path.h"

Expand Down Expand Up @@ -28,7 +28,7 @@ void path_normalizer(Path::parts_t &output, const Path::parts_t &input) {
continue;
}
else if (part == "..") {
if (likely(output.size() > 0)) {
if (output.size() > 0) [[likely]] {
output.pop_back();
}
}
Expand Down Expand Up @@ -294,7 +294,7 @@ std::string Path::get_stem() const {
Path Path::joinpath(const parts_t &subpaths) const {
parts_t new_parts = this->parts;
for (auto &part : subpaths) {
if (likely(part.size() > 0)) {
if (part.size() > 0) [[likely]] {
new_parts.push_back(part);
}
}
Expand Down
2 changes: 1 addition & 1 deletion libopenage/util/strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ std::vector<std::string> split_escape(const std::string &txt, char delim, size_t

// output vector
std::vector<std::string> items;
if (likely(size_hint)) {
if (size_hint) [[likely]] {
items.reserve(size_hint);
}

Expand Down

0 comments on commit 09fec55

Please sign in to comment.