diff options
author | Lenny Maiorani <lenny@colorado.edu> | 2021-05-19 09:32:07 -0600 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-05-21 10:07:06 +0100 |
commit | 800ea8ea969835297dc7e7da345a45b9dc5e751a (patch) | |
tree | 5918276b3f75e73d7f4559f97587a23f652612a5 /Userland/Libraries/LibC | |
parent | 17ff895e1cbc685b99b22856aed16852b564c1f4 (diff) | |
download | serenity-800ea8ea969835297dc7e7da345a45b9dc5e751a.zip |
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`.
Diffstat (limited to 'Userland/Libraries/LibC')
-rw-r--r-- | Userland/Libraries/LibC/netdb.cpp | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibC/time.cpp | 3 |
2 files changed, 3 insertions, 4 deletions
diff --git a/Userland/Libraries/LibC/netdb.cpp b/Userland/Libraries/LibC/netdb.cpp index 77d0b790dd..33d17b5a21 100644 --- a/Userland/Libraries/LibC/netdb.cpp +++ b/Userland/Libraries/LibC/netdb.cpp @@ -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; @@ -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; diff --git a/Userland/Libraries/LibC/time.cpp b/Userland/Libraries/LibC/time.cpp index 9add36bb76..738d8ea594 100644 --- a/Userland/Libraries/LibC/time.cpp +++ b/Userland/Libraries/LibC/time.cpp @@ -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; |