Code improvement wherever possible #434
Replies: 4 comments 26 replies
-
@stefanos82 Note that btop++ was the project I used to learn C++, so there's likely to be a lot of inconsistencies in the efficiency of the code (and a lot of opportunities for optimization). If the coding "peculiarities" you mention is about style and not function you would also need a good argument to why the code should be changed (that's not based on just personal opinion). |
Beta Was this translation helpful? Give feedback.
-
By the way, since GCC 12.1 we can use I personally use mold combined with ccache which improves compilation-times sometimes by orders of magnitude! Any feedback on that? |
Beta Was this translation helpful? Give feedback.
-
Currently I'm working on replacing Reminder: Boolean literals Question: What exactly is this boolean variable doing in //* Calculate and draw cpu box outlines
if (Cpu::shown) {
using namespace Cpu;
bool show_temp = (Config::getB("check_temp") and got_sensors);
width = round((double)Term::width * width_p / 100);
height = max(8, (int)ceil((double)Term::height * (trim(boxes) == "cpu" ? 100 : height_p) / 100));
x = 1;
y = cpu_bottom ? Term::height - height + 1 : 1;
b_columns = max(1, (int)ceil((double)(Shared::coreCount + 1) / (height - 5)));
if (b_columns * (21 + 12 * show_temp) < width - (width / 3)) {
b_column_size = 2;
b_width = (21 + 12 * show_temp) * b_columns - (b_columns - 1);
}
else if (b_columns * (15 + 6 * show_temp) < width - (width / 3)) {
b_column_size = 1;
b_width = (15 + 6 * show_temp) * b_columns - (b_columns - 1);
}
else if (b_columns * (8 + 6 * show_temp) < width - (width / 3)) {
b_column_size = 0;
}
else {
b_columns = (width - width / 3) / (8 + 6 * show_temp);
b_column_size = 0;
}
if (b_column_size == 0) b_width = (8 + 6 * show_temp) * b_columns + 1;
b_height = min(height - 2, (int)ceil((double)Shared::coreCount / b_columns) + 4);
...
... |
Beta Was this translation helpful? Give feedback.
-
I'm currently working on namespace {
//* Convert 24-bit colors to 256 colors
int truecolor_to_256(const int& r, const int& g, const int& b) {
//? Use upper 232-255 greyscale values if the downscaled red, green and blue are the same value
if (const int red = round((double)r / 11); red == round((double)g / 11) and red == round((double)b / 11)) {
return 232 + red;
}
//? Else use 6x6x6 color cube to calculate approximate colors
else {
return round((double)r / 51) * 36 + round((double)g / 51) * 6 + round((double)b / 51) + 16;
}
}
} I have an idea how to get rid of the C-style type casting and I want your feedback. Here's how the code will look: namespace {
//* Convert 24-bit colors to 256 colors
int truecolor_to_256(const int& r, const int& g, const int& b) {
//? Use upper 232-255 greyscale values if the downscaled red, green and blue are the same value
if (const int red = round((r+0.0) / 11); red == round((g+0.0) / 11) and red == round((b+0.0) / 11)) {
return 232 + red;
}
//? Else use 6x6x6 color cube to calculate approximate colors
else {
return round((r+0.0) / 51) * 36 + round((g+0.0) / 51) * 6 + round((b+0.0) / 51) + 16;
}
}
} As you can see, we are taking an advantage of implicit conversion mechanism and since we are using So, what do you think? |
Beta Was this translation helpful? Give feedback.
-
I have tried to find real-time contact information such as Discord or IRC channel but could not find anything, therefore I will ask here if it's alright.
I have noticed some coding "peculiarities" as I like to call them that could be replaced with improved code.
Is it OK to fork, make the necessary changes, and then PR or do I need to follow a certain procedure first?
Please advise.
Thank you very much.
Beta Was this translation helpful? Give feedback.
All reactions