Skip to content

Commit

Permalink
Userland: static vs non-static constexpr variables
Browse files Browse the repository at this point in the history
Problem:
- `static` variables consume memory and sometimes are less
  optimizable.
- `static const` variables can be `constexpr`, usually.
- `static` function-local variables require an initialization check
  every time the function is run.

Solution:
- If a global `static` variable is only used in a single function then
  move it into the function and make it non-`static` and `constexpr`.
- Make all global `static` variables `constexpr` instead of `const`.
- Change function-local `static const[expr]` variables to be just
  `constexpr`.
  • Loading branch information
ldm5180 authored and linusg committed May 21, 2021
1 parent 17ff895 commit 800ea8e
Show file tree
Hide file tree
Showing 38 changed files with 191 additions and 183 deletions.
7 changes: 4 additions & 3 deletions Userland/Applications/Browser/CookieJar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "CookieJar.h"
#include <AK/IPv4Address.h>
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <AK/URL.h>
#include <AK/Vector.h>
#include <LibWeb/Cookie/ParsedCookie.h>
Expand Down Expand Up @@ -48,9 +49,9 @@ void CookieJar::set_cookie(const URL& url, const Web::Cookie::ParsedCookie& pars

void CookieJar::dump_cookies() const
{
static const char* key_color = "\033[34;1m";
static const char* attribute_color = "\033[33m";
static const char* no_color = "\033[0m";
constexpr StringView key_color = "\033[34;1m";
constexpr StringView attribute_color = "\033[33m";
constexpr StringView no_color = "\033[0m";

StringBuilder builder;
builder.appendff("{} cookies stored\n", m_cookies.size());
Expand Down
11 changes: 6 additions & 5 deletions Userland/Applications/Calendar/AddEventDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include "AddEventDialog.h"
#include <AK/StringView.h>
#include <LibCore/DateTime.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
Expand All @@ -20,11 +21,6 @@
#include <LibGfx/Font.h>
#include <LibGfx/FontDatabase.h>

static const char* short_month_names[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

AddEventDialog::AddEventDialog(Core::DateTime date_time, Window* parent_window)
: Dialog(parent_window)
, m_date_time(date_time)
Expand Down Expand Up @@ -121,6 +117,11 @@ String AddEventDialog::MonthListModel::column_name(int column) const

GUI::Variant AddEventDialog::MonthListModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
{
constexpr StringView short_month_names[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

auto& month = short_month_names[index.row()];
if (role == GUI::ModelRole::Display) {
switch (index.column()) {
Expand Down
25 changes: 13 additions & 12 deletions Userland/Applications/FontEditor/FontEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "GlyphMapWidget.h"
#include "NewFontDialog.h"
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <AK/UnicodeUtils.h>
#include <Applications/FontEditor/FontEditorWindowGML.h>
#include <LibDesktop/Launcher.h>
Expand Down Expand Up @@ -37,19 +38,19 @@
#include <LibGfx/TextDirection.h>
#include <stdlib.h>

static constexpr int s_pangram_count = 7;
static const char* pangrams[s_pangram_count] = {
"quick fox jumps nightly above wizard",
"five quacking zephyrs jolt my wax bed",
"pack my box with five dozen liquor jugs",
"quick brown fox jumps over the lazy dog",
"waxy and quivering jocks fumble the pizza",
"~#:[@_1%]*{$2.3}/4^(5'6\")-&|7+8!=<9,0\\>?;",
"byxfjärmat föl gick på duvshowen"
};

static RefPtr<GUI::Window> create_font_preview_window(FontEditorWidget& editor)
{
constexpr int pangram_count = 7;
constexpr StringView pangrams[pangram_count] = {
"quick fox jumps nightly above wizard",
"five quacking zephyrs jolt my wax bed",
"pack my box with five dozen liquor jugs",
"quick brown fox jumps over the lazy dog",
"waxy and quivering jocks fumble the pizza",
"~#:[@_1%]*{$2.3}/4^(5'6\")-&|7+8!=<9,0\\>?;",
"byxfjärmat föl gick på duvshowen"
};

auto window = GUI::Window::construct();
window->set_window_type(GUI::WindowType::ToolWindow);
window->set_title("Font preview");
Expand Down Expand Up @@ -94,7 +95,7 @@ static RefPtr<GUI::Window> create_font_preview_window(FontEditorWidget& editor)
reload_button.set_fixed_width(22);
reload_button.on_click = [&] {
static int i = 1;
if (i >= s_pangram_count)
if (i >= pangram_count)
i = 0;
preview_textbox.set_text(pangrams[i]);
i++;
Expand Down
16 changes: 8 additions & 8 deletions Userland/Applications/HexEditor/FindDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
*/

#include "FindDialog.h"
#include <AK/Array.h>
#include <AK/Hex.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/Label.h>
Expand All @@ -19,17 +19,12 @@
#include <LibGfx/FontDatabase.h>

struct Option {
String title;
StringView title;
OptionId opt;
bool enabled;
bool default_action;
};

static const Vector<Option> options = {
{ "ACII String", OPTION_ASCII_STRING, true, true },
{ "Hex value", OPTION_HEX_VALUE, true, false },
};

int FindDialog::show(GUI::Window* parent_window, String& out_text, ByteBuffer& out_buffer)
{
auto dialog = FindDialog::construct();
Expand Down Expand Up @@ -88,6 +83,11 @@ Result<ByteBuffer, String> FindDialog::process_input(String text_value, OptionId
FindDialog::FindDialog()
: Dialog(nullptr)
{
constexpr Array options = {
Option { "ACII String", OPTION_ASCII_STRING, true, true },
Option { "Hex value", OPTION_HEX_VALUE, true, false },
};

resize(280, 180 + ((static_cast<int>(options.size()) - 3) * 16));
center_on_screen();
set_resizable(false);
Expand All @@ -113,7 +113,7 @@ FindDialog::FindDialog()
radio.set_enabled(action.enabled);
radio.set_text(action.title);

radio.on_checked = [this, i](auto) {
radio.on_checked = [&](auto) {
m_selected_option = options[i].opt;
};

Expand Down
3 changes: 2 additions & 1 deletion Userland/Applications/SpaceAnalyzer/TreeMapWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

#include "TreeMapWidget.h"
#include <AK/Array.h>
#include <AK/NumberFormat.h>
#include <LibGUI/Painter.h>
#include <LibGUI/WindowServerConnection.h>
Expand All @@ -24,7 +25,7 @@ TreeMapWidget::~TreeMapWidget()
{
}

static const Color colors[] = {
static constexpr Array colors = {
Color(253, 231, 37),
Color(148, 216, 64),
Color(60, 188, 117),
Expand Down
5 changes: 3 additions & 2 deletions Userland/Applications/SpaceAnalyzer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <AK/Queue.h>
#include <AK/QuickSort.h>
#include <AK/RefCounted.h>
#include <AK/StringView.h>
#include <AK/URL.h>
#include <Applications/SpaceAnalyzer/SpaceAnalyzerGML.h>
#include <LibCore/DirIterator.h>
Expand All @@ -27,8 +28,6 @@
#include <sys/stat.h>
#include <unistd.h>

static const char* APP_NAME = "Space Analyzer";

struct TreeNode : public SpaceAnalyzer::TreeMapNode {
TreeNode(String name)
: m_name(move(name)) {};
Expand Down Expand Up @@ -253,6 +252,8 @@ static String get_absolute_path_to_selected_node(const SpaceAnalyzer::TreeMapWid

int main(int argc, char* argv[])
{
constexpr StringView APP_NAME = "Space Analyzer";

auto app = GUI::Application::construct(argc, argv);

RefPtr<Tree> tree = adopt_ref(*new Tree(""));
Expand Down
35 changes: 18 additions & 17 deletions Userland/Demos/Fire/Fire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* [ ] handle fire bitmap edges better
*/

#include <AK/Array.h>
#include <LibCore/ElapsedTimer.h>
#include <LibGUI/Action.h>
#include <LibGUI/Application.h>
Expand All @@ -38,22 +39,9 @@
#include <time.h>
#include <unistd.h>

#define FIRE_WIDTH 320
#define FIRE_HEIGHT 168
#define FIRE_MAX 29

static const Color s_palette[] = {
Color(0x07, 0x07, 0x07), Color(0x1F, 0x07, 0x07), Color(0x2F, 0x0F, 0x07),
Color(0x47, 0x0F, 0x07), Color(0x57, 0x17, 0x07), Color(0x67, 0x1F, 0x07),
Color(0x77, 0x1F, 0x07), Color(0x9F, 0x2F, 0x07), Color(0xAF, 0x3F, 0x07),
Color(0xBF, 0x47, 0x07), Color(0xC7, 0x47, 0x07), Color(0xDF, 0x4F, 0x07),
Color(0xDF, 0x57, 0x07), Color(0xD7, 0x5F, 0x07), Color(0xD7, 0x5F, 0x07),
Color(0xD7, 0x67, 0x0F), Color(0xCF, 0x6F, 0x0F), Color(0xCF, 0x7F, 0x0F),
Color(0xCF, 0x87, 0x17), Color(0xC7, 0x87, 0x17), Color(0xC7, 0x8F, 0x17),
Color(0xC7, 0x97, 0x1F), Color(0xBF, 0x9F, 0x1F), Color(0xBF, 0xA7, 0x27),
Color(0xBF, 0xAF, 0x2F), Color(0xB7, 0xAF, 0x2F), Color(0xB7, 0xB7, 0x37),
Color(0xCF, 0xCF, 0x6F), Color(0xEF, 0xEF, 0xC7), Color(0xFF, 0xFF, 0xFF)
};
static constexpr auto FIRE_WIDTH = 320;
static constexpr auto FIRE_HEIGHT = 168;
static constexpr auto FIRE_MAX = 29;

class Fire : public GUI::Widget {
C_OBJECT(Fire)
Expand All @@ -80,11 +68,24 @@ class Fire : public GUI::Widget {

Fire::Fire()
{
constexpr Array palette = {
Color(0x07, 0x07, 0x07), Color(0x1F, 0x07, 0x07), Color(0x2F, 0x0F, 0x07),
Color(0x47, 0x0F, 0x07), Color(0x57, 0x17, 0x07), Color(0x67, 0x1F, 0x07),
Color(0x77, 0x1F, 0x07), Color(0x9F, 0x2F, 0x07), Color(0xAF, 0x3F, 0x07),
Color(0xBF, 0x47, 0x07), Color(0xC7, 0x47, 0x07), Color(0xDF, 0x4F, 0x07),
Color(0xDF, 0x57, 0x07), Color(0xD7, 0x5F, 0x07), Color(0xD7, 0x5F, 0x07),
Color(0xD7, 0x67, 0x0F), Color(0xCF, 0x6F, 0x0F), Color(0xCF, 0x7F, 0x0F),
Color(0xCF, 0x87, 0x17), Color(0xC7, 0x87, 0x17), Color(0xC7, 0x8F, 0x17),
Color(0xC7, 0x97, 0x1F), Color(0xBF, 0x9F, 0x1F), Color(0xBF, 0xA7, 0x27),
Color(0xBF, 0xAF, 0x2F), Color(0xB7, 0xAF, 0x2F), Color(0xB7, 0xB7, 0x37),
Color(0xCF, 0xCF, 0x6F), Color(0xEF, 0xEF, 0xC7), Color(0xFF, 0xFF, 0xFF)
};

bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::Indexed8, { 320, 200 });

/* Initialize fire palette */
for (int i = 0; i < 30; i++)
bitmap->set_palette_color(i, s_palette[i]);
bitmap->set_palette_color(i, palette[i]);

/* Set remaining entries to white */
for (int i = 30; i < 256; i++)
Expand Down
4 changes: 2 additions & 2 deletions Userland/Games/Breakout/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class Game final : public GUI::Widget {
C_OBJECT(Game);

public:
static const int game_width = 480;
static const int game_height = 500;
static constexpr int game_width = 480;
static constexpr int game_height = 500;

virtual ~Game() override;

Expand Down
4 changes: 2 additions & 2 deletions Userland/Games/Pong/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Game final : public GUI::Widget {
C_OBJECT(Game);

public:
static const int game_width = 560;
static const int game_height = 480;
static constexpr int game_width = 560;
static constexpr int game_height = 480;

virtual ~Game() override;

Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibC/netdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ static constexpr i32 lookup_server_endpoint_magic = 9001;

// Get service entry buffers and file information for the getservent() family of functions.
static FILE* services_file = nullptr;
static const char* services_path = "/etc/services";
static constexpr char services_path[] = "/etc/services";

static bool fill_getserv_buffers(const char* line, ssize_t read);
static servent __getserv_buffer;
Expand All @@ -50,7 +50,7 @@ static ssize_t service_file_offset = 0;

// Get protocol entry buffers and file information for the getprotent() family of functions.
static FILE* protocols_file = nullptr;
static const char* protocols_path = "/etc/protocols";
static constexpr char protocols_path[] = "/etc/protocols";

static bool fill_getproto_buffers(const char* line, ssize_t read);
static protoent __getproto_buffer;
Expand Down
3 changes: 1 addition & 2 deletions Userland/Libraries/LibC/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,9 @@ char* ctime_r(const time_t* t, char* buf)
return asctime_r(localtime_r(t, &tm_buf), buf);
}

static const int __seconds_per_day = 60 * 60 * 24;

static void time_to_tm(struct tm* tm, time_t t)
{
constexpr int __seconds_per_day = 60 * 60 * 24;
int year = 1970;
for (; t >= days_in_year(year) * __seconds_per_day; ++year)
t -= days_in_year(year) * __seconds_per_day;
Expand Down
13 changes: 7 additions & 6 deletions Userland/Libraries/LibGUI/Calendar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/Array.h>
#include <LibCore/DateTime.h>
#include <LibGUI/Calendar.h>
#include <LibGUI/Painter.h>
Expand All @@ -16,21 +17,21 @@ REGISTER_WIDGET(GUI, Calendar);

namespace GUI {

static const char* long_day_names[] = {
static constexpr Array long_day_names = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};

static const char* short_day_names[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static const char* mini_day_names[] = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
static const char* micro_day_names[] = { "S", "M", "T", "W", "T", "F", "S" };
static constexpr Array short_day_names = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static constexpr Array mini_day_names = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
static constexpr Array micro_day_names = { "S", "M", "T", "W", "T", "F", "S" };

static const char* long_month_names[] = {
static constexpr Array long_month_names = {
"January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"
};

static const char* short_month_names[] = {
static constexpr Array short_month_names = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
Expand Down
6 changes: 3 additions & 3 deletions Userland/Libraries/LibGUI/CheckBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ REGISTER_WIDGET(GUI, CheckBox)

namespace GUI {

static const int s_box_width = 13;
static const int s_box_height = 13;
static const int s_horizontal_padding = 6;
static constexpr int s_box_width = 13;
static constexpr int s_box_height = 13;
static constexpr int s_horizontal_padding = 6;

CheckBox::CheckBox(String text)
: AbstractButton(move(text))
Expand Down
6 changes: 3 additions & 3 deletions Userland/Libraries/LibGUI/ColumnsView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace GUI {

static const char* s_arrow_bitmap_data = {
static constexpr char s_arrow_bitmap_data[] = {
" "
" # "
" ## "
Expand All @@ -24,8 +24,8 @@ static const char* s_arrow_bitmap_data = {
" # "
" "
};
static const int s_arrow_bitmap_width = 9;
static const int s_arrow_bitmap_height = 9;
static constexpr int s_arrow_bitmap_width = 9;
static constexpr int s_arrow_bitmap_height = 9;

ColumnsView::ColumnsView()
{
Expand Down
5 changes: 4 additions & 1 deletion Userland/Libraries/LibGUI/FileIconProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,10 @@ Icon FileIconProvider::icon_for_executable(const String& path)
int image_size;
};

static const IconSection icon_sections[] = { { .section_name = "serenity_icon_s", .image_size = 16 }, { .section_name = "serenity_icon_m", .image_size = 32 } };
constexpr Array icon_sections = {
IconSection { .section_name = "serenity_icon_s", .image_size = 16 },
IconSection { .section_name = "serenity_icon_m", .image_size = 32 }
};

bool had_error = false;
for (const auto& icon_section : icon_sections) {
Expand Down
Loading

0 comments on commit 800ea8e

Please sign in to comment.