mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 08:07: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:
parent
17ff895e1c
commit
800ea8ea96
38 changed files with 192 additions and 184 deletions
|
@ -354,7 +354,7 @@ void ClassicStylePainter::paint_radio_button(Painter& painter, const IntRect& re
|
|||
painter.blit(rect.location(), bitmap, bitmap.rect());
|
||||
}
|
||||
|
||||
static const char* s_checked_bitmap_data = {
|
||||
static constexpr char s_checked_bitmap_data[] = {
|
||||
" "
|
||||
" # "
|
||||
" ## "
|
||||
|
@ -367,8 +367,8 @@ static const char* s_checked_bitmap_data = {
|
|||
};
|
||||
|
||||
static Gfx::CharacterBitmap* s_checked_bitmap;
|
||||
static const int s_checked_bitmap_width = 9;
|
||||
static const int s_checked_bitmap_height = 9;
|
||||
static constexpr int s_checked_bitmap_width = 9;
|
||||
static constexpr int s_checked_bitmap_height = 9;
|
||||
|
||||
void ClassicStylePainter::paint_check_box(Painter& painter, const IntRect& rect, const Palette& palette, bool is_enabled, bool is_checked, bool is_being_pressed)
|
||||
{
|
||||
|
|
|
@ -140,7 +140,7 @@ public:
|
|||
#endif
|
||||
}
|
||||
|
||||
constexpr Color interpolate(const Color& other, float weight) const
|
||||
Color interpolate(const Color& other, float weight) const
|
||||
{
|
||||
u8 r = red() + roundf(static_cast<float>(other.red() - red()) * weight);
|
||||
u8 g = green() + roundf(static_cast<float>(other.green() - green()) * weight);
|
||||
|
|
|
@ -19,10 +19,6 @@
|
|||
|
||||
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 };
|
||||
|
||||
struct ImageDescriptor {
|
||||
u16 x { 0 };
|
||||
u16 y { 0 };
|
||||
|
@ -112,8 +108,8 @@ enum class GIFFormat {
|
|||
|
||||
static Optional<GIFFormat> decode_gif_header(InputMemoryStream& stream)
|
||||
{
|
||||
static const char valid_header_87[] = "GIF87a";
|
||||
static const char valid_header_89[] = "GIF89a";
|
||||
constexpr char valid_header_87[] = "GIF87a";
|
||||
constexpr char valid_header_89[] = "GIF89a";
|
||||
|
||||
Array<u8, 6> header;
|
||||
stream >> header;
|
||||
|
@ -378,6 +374,8 @@ static bool decode_frame(GIFLoadingContext& context, size_t frame_index)
|
|||
if (pixel_index % image.width == 0) {
|
||||
if (image.interlaced) {
|
||||
if (interlace_pass < 4) {
|
||||
constexpr Array INTERLACE_ROW_STRIDES = { 8, 8, 4, 2 };
|
||||
constexpr Array INTERLACE_ROW_OFFSETS = { 0, 4, 2, 1 };
|
||||
if (row + INTERLACE_ROW_STRIDES[interlace_pass] >= image.height) {
|
||||
++interlace_pass;
|
||||
if (interlace_pass < 4)
|
||||
|
|
|
@ -861,20 +861,20 @@ static void dequantize(JPGLoadingContext& context, Vector<Macroblock>& macrobloc
|
|||
|
||||
static void inverse_dct(const JPGLoadingContext& context, Vector<Macroblock>& macroblocks)
|
||||
{
|
||||
static const float m0 = 2.0 * cos(1.0 / 16.0 * 2.0 * M_PI);
|
||||
static const float m1 = 2.0 * cos(2.0 / 16.0 * 2.0 * M_PI);
|
||||
static const float m3 = 2.0 * cos(2.0 / 16.0 * 2.0 * M_PI);
|
||||
static const float m5 = 2.0 * cos(3.0 / 16.0 * 2.0 * M_PI);
|
||||
static const float m2 = m0 - m5;
|
||||
static const float m4 = m0 + m5;
|
||||
static const float s0 = cos(0.0 / 16.0 * M_PI) / sqrt(8);
|
||||
static const float s1 = cos(1.0 / 16.0 * M_PI) / 2.0;
|
||||
static const float s2 = cos(2.0 / 16.0 * M_PI) / 2.0;
|
||||
static const float s3 = cos(3.0 / 16.0 * M_PI) / 2.0;
|
||||
static const float s4 = cos(4.0 / 16.0 * M_PI) / 2.0;
|
||||
static const float s5 = cos(5.0 / 16.0 * M_PI) / 2.0;
|
||||
static const float s6 = cos(6.0 / 16.0 * M_PI) / 2.0;
|
||||
static const float s7 = cos(7.0 / 16.0 * M_PI) / 2.0;
|
||||
const float m0 = 2.0 * cos(1.0 / 16.0 * 2.0 * M_PI);
|
||||
const float m1 = 2.0 * cos(2.0 / 16.0 * 2.0 * M_PI);
|
||||
const float m3 = 2.0 * cos(2.0 / 16.0 * 2.0 * M_PI);
|
||||
const float m5 = 2.0 * cos(3.0 / 16.0 * 2.0 * M_PI);
|
||||
const float m2 = m0 - m5;
|
||||
const float m4 = m0 + m5;
|
||||
const float s0 = cos(0.0 / 16.0 * M_PI) / sqrt(8);
|
||||
const float s1 = cos(1.0 / 16.0 * M_PI) / 2.0;
|
||||
const float s2 = cos(2.0 / 16.0 * M_PI) / 2.0;
|
||||
const float s3 = cos(3.0 / 16.0 * M_PI) / 2.0;
|
||||
const float s4 = cos(4.0 / 16.0 * M_PI) / 2.0;
|
||||
const float s5 = cos(5.0 / 16.0 * M_PI) / 2.0;
|
||||
const float s6 = cos(6.0 / 16.0 * M_PI) / 2.0;
|
||||
const float s7 = cos(7.0 / 16.0 * M_PI) / 2.0;
|
||||
|
||||
for (u32 vcursor = 0; vcursor < context.mblock_meta.vcount; vcursor += context.vsample_factor) {
|
||||
for (u32 hcursor = 0; hcursor < context.mblock_meta.hcount; hcursor += context.hsample_factor) {
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
|
||||
namespace Gfx {
|
||||
|
||||
static const u8 png_header[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
|
||||
static constexpr Array png_header = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 };
|
||||
|
||||
struct PNG_IHDR {
|
||||
NetworkOrdered<u32> width;
|
||||
|
@ -512,7 +512,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.data(), sizeof(png_header)) != 0) {
|
||||
dbgln_if(PNG_DEBUG, "Invalid PNG header");
|
||||
context.state = PNGLoadingContext::State::Error;
|
||||
return false;
|
||||
|
@ -661,14 +661,14 @@ static int adam7_width(PNGLoadingContext& context, int pass)
|
|||
}
|
||||
}
|
||||
|
||||
// Index 0 unused (non-interlaced case)
|
||||
static int adam7_starty[8] = { 0, 0, 0, 4, 0, 2, 0, 1 };
|
||||
static int adam7_startx[8] = { 0, 0, 4, 0, 2, 0, 1, 0 };
|
||||
static int adam7_stepy[8] = { 1, 8, 8, 8, 4, 4, 2, 2 };
|
||||
static int adam7_stepx[8] = { 1, 8, 8, 4, 4, 2, 2, 1 };
|
||||
|
||||
static bool decode_adam7_pass(PNGLoadingContext& context, Streamer& streamer, int pass)
|
||||
{
|
||||
// Index 0 unused (non-interlaced case)
|
||||
constexpr Array adam7_starty = { 0, 0, 0, 4, 0, 2, 0, 1 };
|
||||
constexpr Array adam7_startx = { 0, 0, 4, 0, 2, 0, 1, 0 };
|
||||
constexpr Array adam7_stepy = { 1, 8, 8, 8, 4, 4, 2, 2 };
|
||||
constexpr Array adam7_stepx = { 1, 8, 8, 4, 4, 2, 2, 1 };
|
||||
|
||||
PNGLoadingContext subimage_context;
|
||||
subimage_context.width = adam7_width(context, pass);
|
||||
subimage_context.height = adam7_height(context, pass);
|
||||
|
|
|
@ -1476,7 +1476,7 @@ void do_draw_text(const IntRect& rect, const TextType& text, const Font& font, T
|
|||
lines.append(line);
|
||||
}
|
||||
|
||||
static const int line_spacing = 4;
|
||||
constexpr int line_spacing = 4;
|
||||
int line_height = font.glyph_height() + line_spacing;
|
||||
IntRect bounding_rect { 0, 0, 0, (static_cast<int>(lines.size()) * line_height) - line_spacing };
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue