Skip to content

Commit

Permalink
Userland: Change static const variables to static constexpr
Browse files Browse the repository at this point in the history
`static const` variables can be computed and initialized at run-time
during initialization or the first time a function is called. Change
them to `static constexpr` to ensure they are computed at
compile-time.

This allows some removal of `strlen` because the length of the
`StringView` can be used which is pre-computed at compile-time.
  • Loading branch information
ldm5180 authored and awesomekling committed Mar 18, 2022
1 parent 31515a9 commit f912a48
Show file tree
Hide file tree
Showing 23 changed files with 111 additions and 82 deletions.
3 changes: 2 additions & 1 deletion Userland/Demos/Fire/Fire.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* [ ] handle fire bitmap edges better
*/

#include <AK/Array.h>
#include <LibCore/ElapsedTimer.h>
#include <LibCore/System.h>
#include <LibGUI/Action.h>
Expand All @@ -45,7 +46,7 @@
#define FIRE_HEIGHT 200
#define FIRE_MAX 29

static const Color s_palette[] = {
static constexpr Array<Color, 30> 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),
Expand Down
6 changes: 4 additions & 2 deletions Userland/Demos/VirGLDemo/CommandBufferBuilder.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/*
* Copyright (c) 2022, Sahan Fernando <[email protected]>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/StringView.h>
#include <Kernel/API/VirGL.h>
#include <sys/ioctl_numbers.h>

Expand Down Expand Up @@ -235,9 +237,9 @@ void CommandBufferBuilder::append_set_constant_buffer(Vector<float> const& const
}
}

void CommandBufferBuilder::append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, const char* shader_data)
void CommandBufferBuilder::append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, StringView shader_data)
{
size_t shader_len = strlen(shader_data) + 1; // Need to remember to copy null terminator as well if needed
size_t shader_len = shader_data.length() + 1; // Need to remember to copy null terminator as well if needed
CommandBuilder builder(m_buffer, Protocol::VirGLCommand::CREATE_OBJECT, to_underlying(Protocol::ObjectType::SHADER));
builder.appendu32(handle.value()); // VIRGL_OBJ_CREATE_HANDLE
builder.appendu32(to_underlying(shader_type));
Expand Down
4 changes: 3 additions & 1 deletion Userland/Demos/VirGLDemo/CommandBufferBuilder.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/*
* Copyright (c) 2022, Sahan Fernando <[email protected]>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <AK/StringView.h>
#include <AK/Vector.h>
#include <sys/ioctl_numbers.h>

Expand All @@ -28,7 +30,7 @@ class CommandBufferBuilder {
void append_gl_viewport();
void append_set_framebuffer_state_no_attach();
void append_set_constant_buffer(Vector<float> const& constant_buffer);
void append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, const char* shader_data);
void append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, StringView shader_data);
void append_bind_shader(ObjectHandle handle, Gallium::ShaderType shader_type);
void append_create_rasterizer(ObjectHandle handle);
void append_bind_rasterizer(ObjectHandle handle);
Expand Down
45 changes: 24 additions & 21 deletions Userland/Demos/VirGLDemo/VirGLDemo.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/*
* Copyright (c) 2022, Sahan Fernando <[email protected]>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/Array.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
#include <Kernel/API/VirGL.h>
#include <LibGUI/Application.h>
Expand All @@ -23,26 +26,26 @@
#include "VirGLProtocol.h"
#include "Widget.h"

static const char* frag_shader = "FRAG\n"
"PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1\n"
"DCL IN[0], COLOR, COLOR\n"
"DCL OUT[0], COLOR\n"
" 0: MOV OUT[0], IN[0]\n"
" 1: END\n";
static constexpr StringView frag_shader = "FRAG\n"
"PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1\n"
"DCL IN[0], COLOR, COLOR\n"
"DCL OUT[0], COLOR\n"
" 0: MOV OUT[0], IN[0]\n"
" 1: END\n";

static const char* vert_shader = "VERT\n"
"DCL IN[0]\n"
"DCL IN[1]\n"
"DCL OUT[0], POSITION\n"
"DCL OUT[1], COLOR\n"
"DCL CONST[0..3]\n"
"DCL TEMP[0..1]\n"
" 0: MUL TEMP[0], IN[0].xxxx, CONST[0]\n"
" 1: MAD TEMP[1], IN[0].yyyy, CONST[1], TEMP[0]\n"
" 2: MAD TEMP[0], IN[0].zzzz, CONST[2], TEMP[1]\n"
" 3: MAD OUT[0], IN[0].wwww, CONST[3], TEMP[0]\n"
" 4: MOV_SAT OUT[1], IN[1]\n"
" 5: END\n";
static constexpr StringView vert_shader = "VERT\n"
"DCL IN[0]\n"
"DCL IN[1]\n"
"DCL OUT[0], POSITION\n"
"DCL OUT[1], COLOR\n"
"DCL CONST[0..3]\n"
"DCL TEMP[0..1]\n"
" 0: MUL TEMP[0], IN[0].xxxx, CONST[0]\n"
" 1: MAD TEMP[1], IN[0].yyyy, CONST[1], TEMP[0]\n"
" 2: MAD TEMP[0], IN[0].zzzz, CONST[2], TEMP[1]\n"
" 3: MAD OUT[0], IN[0].wwww, CONST[3], TEMP[0]\n"
" 4: MOV_SAT OUT[1], IN[1]\n"
" 5: END\n";

struct VertexData {
float r;
Expand Down Expand Up @@ -92,7 +95,7 @@ static ResourceID create_virgl_resource(VirGL3DResourceSpec& spec)
static Vector<VertexData> gen_vertex_data()
{
Vector<VertexData> data;
static const VertexData vertices[8] = {
static constexpr Array<VertexData, 8> vertices = {
VertexData { .r = 0, .g = 0, .b = 0, .x = -0.5, .y = -0.5, .z = -0.5 },
VertexData { .r = 0, .g = 0, .b = 0, .x = 0.5, .y = -0.5, .z = -0.5 },
VertexData { .r = 0, .g = 0, .b = 0, .x = -0.5, .y = 0.5, .z = -0.5 },
Expand All @@ -102,7 +105,7 @@ static Vector<VertexData> gen_vertex_data()
VertexData { .r = 0, .g = 0, .b = 0, .x = -0.5, .y = 0.5, .z = 0.5 },
VertexData { .r = 0, .g = 0, .b = 0, .x = 0.5, .y = 0.5, .z = 0.5 },
};
size_t tris[36] = {
static constexpr Array<size_t, 36> tris = {
0, 1, 2, 1, 3, 2, // Top
4, 0, 6, 0, 2, 6, // Left
4, 5, 0, 5, 1, 0, // Up
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 @@ -16,8 +16,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 = default;

Expand Down
4 changes: 2 additions & 2 deletions Userland/Games/FlappyBug/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Game final : public GUI::Frame {
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;

Function<u32(u32)> on_game_end;

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 @@ -23,8 +23,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 = default;

Expand Down
3 changes: 1 addition & 2 deletions Userland/Libraries/LibAudio/Loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

namespace Audio {

static const String empty_string = "";
static String no_plugin_error = "No loader plugin available";
static constexpr StringView no_plugin_error = "No loader plugin available";

using LoaderSamples = Result<NonnullRefPtr<Buffer>, LoaderError>;
using MaybeLoaderError = Result<void, LoaderError>;
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<StringView, 7> 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<StringView, 7> short_day_names = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static constexpr Array<StringView, 7> mini_day_names = { "Su", "Mo", "Tu", "We", "Th", "Fr", "Sa" };
static constexpr Array<StringView, 7> micro_day_names = { "S", "M", "T", "W", "T", "F", "S" };

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

static const char* short_month_names[] = {
static constexpr Array<StringView, 12> 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 @@ -16,9 +16,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
7 changes: 6 additions & 1 deletion Userland/Libraries/LibGUI/FileIconProvider.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/*
* Copyright (c) 2020-2021, Andreas Kling <[email protected]>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/Array.h>
#include <AK/LexicalPath.h>
#include <AK/String.h>
#include <LibCore/ConfigFile.h>
Expand Down Expand Up @@ -178,7 +180,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 } };
static constexpr Array<IconSection, 2> 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
3 changes: 2 additions & 1 deletion Userland/Libraries/LibGUI/LinkLabel.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Alex McGrath <[email protected]>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand Down Expand Up @@ -48,7 +49,7 @@ void LinkLabel::set_hovered(bool hover)

void LinkLabel::mousemove_event(MouseEvent& event)
{
static const int extra_target_width = 3;
constexpr int extra_target_width = 3;
set_hovered(event.position().x() <= font().width(text()) + extra_target_width);
}

Expand Down
5 changes: 3 additions & 2 deletions Userland/Libraries/LibGfx/GIFLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2018-2021, Andreas Kling <[email protected]>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
Expand All @@ -16,8 +17,8 @@
namespace Gfx {

// Row strides and offsets for each interlace pass.
static const int INTERLACE_ROW_STRIDES[] = { 8, 8, 4, 2 };
static const int INTERLACE_ROW_OFFSETS[] = { 0, 4, 2, 1 };
static constexpr Array<int, 4> INTERLACE_ROW_STRIDES = { 8, 8, 4, 2 };
static constexpr Array<int, 4> INTERLACE_ROW_OFFSETS = { 0, 4, 2, 1 };

struct GIFImageDescriptor {
u16 x { 0 };
Expand Down
6 changes: 4 additions & 2 deletions Userland/Libraries/LibGfx/PNGLoader.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <[email protected]>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/Array.h>
#include <AK/Debug.h>
#include <AK/Endian.h>
#include <AK/Vector.h>
Expand All @@ -17,7 +19,7 @@

namespace Gfx {

static const u8 png_header[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
static constexpr Array<u8, 8> png_header = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };

struct PNG_IHDR {
NetworkOrdered<u32> width;
Expand Down Expand Up @@ -516,7 +518,7 @@ static bool decode_png_header(PNGLoadingContext& context)
return false;
}

if (memcmp(context.data, png_header, sizeof(png_header)) != 0) {
if (memcmp(context.data, png_header.span().data(), sizeof(png_header)) != 0) {
dbgln_if(PNG_DEBUG, "Invalid PNG header");
context.state = PNGLoadingContext::State::Error;
return false;
Expand Down
10 changes: 8 additions & 2 deletions Userland/Libraries/LibJS/Runtime/NumberPrototype.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/*
* Copyright (c) 2020, Andreas Kling <[email protected]>
* Copyright (c) 2021, Linus Groh <[email protected]>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/Array.h>
#include <AK/Function.h>
#include <AK/TypeCasts.h>
#include <LibJS/Runtime/AbstractOperations.h>
Expand All @@ -19,7 +21,7 @@

namespace JS {

static const u8 max_precision_for_radix[37] = {
static constexpr AK::Array<u8, 37> max_precision_for_radix = {
// clang-format off
0, 0, 52, 32, 26, 22, 20, 18, 17, 16,
15, 15, 14, 14, 13, 13, 13, 12, 12, 12,
Expand All @@ -28,7 +30,11 @@ static const u8 max_precision_for_radix[37] = {
// clang-format on
};

static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
static constexpr AK::Array<char, 36> digits = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};

static String decimal_digits_to_string(double number)
{
Expand Down
Loading

0 comments on commit f912a48

Please sign in to comment.