1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-28 01:27:44 +00:00

Userland: static vs non-static constexpr variables

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`.
This commit is contained in:
Lenny Maiorani 2021-05-19 09:32:07 -06:00 committed by Linus Groh
parent 17ff895e1c
commit 800ea8ea96
38 changed files with 192 additions and 184 deletions

View file

@ -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>
@ -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());

View file

@ -5,6 +5,7 @@
*/
#include "AddEventDialog.h"
#include <AK/StringView.h>
#include <LibCore/DateTime.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
@ -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)
@ -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()) {

View file

@ -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>
@ -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");
@ -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++;

View file

@ -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>
@ -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();
@ -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);
@ -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;
};

View file

@ -5,6 +5,7 @@
*/
#include "TreeMapWidget.h"
#include <AK/Array.h>
#include <AK/NumberFormat.h>
#include <LibGUI/Painter.h>
#include <LibGUI/WindowServerConnection.h>
@ -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),

View file

@ -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>
@ -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)) {};
@ -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(""));