Skip to content

Commit 9110bcf

Browse files
committed
Remove unnecessary StringName idx cache in _Data to reduce its size by 4 bytes.
Encapsulate `StringName` details in its cpp file.
1 parent 28089c4 commit 9110bcf

File tree

2 files changed

+52
-76
lines changed

2 files changed

+52
-76
lines changed

core/string/string_name.cpp

+51-62
Original file line numberDiff line numberDiff line change
@@ -33,38 +33,31 @@
3333
#include "core/os/os.h"
3434
#include "core/string/print_string.h"
3535

36-
bool StringName::_Data::operator==(const String &p_name) const {
37-
return name == p_name;
38-
}
39-
40-
bool StringName::_Data::operator!=(const String &p_name) const {
41-
return !operator==(p_name);
42-
}
36+
struct StringName::Table {
37+
constexpr static uint32_t TABLE_BITS = 16;
38+
constexpr static uint32_t TABLE_LEN = 1 << TABLE_BITS;
39+
constexpr static uint32_t TABLE_MASK = TABLE_LEN - 1;
4340

44-
bool StringName::_Data::operator==(const char *p_name) const {
45-
return name == p_name;
46-
}
47-
48-
bool StringName::_Data::operator!=(const char *p_name) const {
49-
return !operator==(p_name);
50-
}
41+
static inline _Data *table[TABLE_LEN];
42+
static inline Mutex mutex;
43+
};
5144

5245
void StringName::setup() {
5346
ERR_FAIL_COND(configured);
54-
for (int i = 0; i < STRING_TABLE_LEN; i++) {
55-
_table[i] = nullptr;
47+
for (int i = 0; i < Table::TABLE_LEN; i++) {
48+
Table::table[i] = nullptr;
5649
}
5750
configured = true;
5851
}
5952

6053
void StringName::cleanup() {
61-
MutexLock lock(mutex);
54+
MutexLock lock(Table::mutex);
6255

6356
#ifdef DEBUG_ENABLED
6457
if (unlikely(debug_stringname)) {
6558
Vector<_Data *> data;
66-
for (int i = 0; i < STRING_TABLE_LEN; i++) {
67-
_Data *d = _table[i];
59+
for (int i = 0; i < Table::TABLE_LEN; i++) {
60+
_Data *d = Table::table[i];
6861
while (d) {
6962
data.push_back(d);
7063
d = d->next;
@@ -77,7 +70,7 @@ void StringName::cleanup() {
7770
int unreferenced_stringnames = 0;
7871
int rarely_referenced_stringnames = 0;
7972
for (int i = 0; i < data.size(); i++) {
80-
print_line(itos(i + 1) + ": " + data[i]->get_name() + " - " + itos(data[i]->debug_references));
73+
print_line(itos(i + 1) + ": " + data[i]->name + " - " + itos(data[i]->debug_references));
8174
if (data[i]->debug_references == 0) {
8275
unreferenced_stringnames += 1;
8376
} else if (data[i]->debug_references < 5) {
@@ -90,9 +83,9 @@ void StringName::cleanup() {
9083
}
9184
#endif
9285
int lost_strings = 0;
93-
for (int i = 0; i < STRING_TABLE_LEN; i++) {
94-
while (_table[i]) {
95-
_Data *d = _table[i];
86+
for (int i = 0; i < Table::TABLE_LEN; i++) {
87+
while (Table::table[i]) {
88+
_Data *d = Table::table[i];
9689
if (d->static_count.get() != d->refcount.get()) {
9790
lost_strings++;
9891

@@ -101,7 +94,7 @@ void StringName::cleanup() {
10194
}
10295
}
10396

104-
_table[i] = _table[i]->next;
97+
Table::table[i] = Table::table[i]->next;
10598
memdelete(d);
10699
}
107100
}
@@ -115,18 +108,16 @@ void StringName::unref() {
115108
ERR_FAIL_COND(!configured);
116109

117110
if (_data && _data->refcount.unref()) {
118-
MutexLock lock(mutex);
111+
MutexLock lock(Table::mutex);
119112

120113
if (CoreGlobals::leak_reporting_enabled && _data->static_count.get() > 0) {
121114
ERR_PRINT("BUG: Unreferenced static string to 0: " + _data->name);
122115
}
123116
if (_data->prev) {
124117
_data->prev->next = _data->next;
125118
} else {
126-
if (_table[_data->idx] != _data) {
127-
ERR_PRINT("BUG!");
128-
}
129-
_table[_data->idx] = _data->next;
119+
const uint32_t idx = _data->hash & Table::TABLE_MASK;
120+
Table::table[idx] = _data->next;
130121
}
131122

132123
if (_data->next) {
@@ -145,15 +136,15 @@ uint32_t StringName::get_empty_hash() {
145136

146137
bool StringName::operator==(const String &p_name) const {
147138
if (_data) {
148-
return _data->operator==(p_name);
139+
return _data->name == p_name;
149140
}
150141

151142
return p_name.is_empty();
152143
}
153144

154145
bool StringName::operator==(const char *p_name) const {
155146
if (_data) {
156-
return _data->operator==(p_name);
147+
return _data->name == p_name;
157148
}
158149

159150
return p_name[0] == 0;
@@ -217,7 +208,7 @@ StringName::StringName(const StringName &p_name) {
217208
}
218209

219210
void StringName::assign_static_unique_class_name(StringName *ptr, const char *p_name) {
220-
MutexLock lock(mutex);
211+
MutexLock lock(Table::mutex);
221212
if (*ptr == StringName()) {
222213
*ptr = StringName(p_name, true);
223214
}
@@ -233,14 +224,14 @@ StringName::StringName(const char *p_name, bool p_static) {
233224
}
234225

235226
const uint32_t hash = String::hash(p_name);
236-
const uint32_t idx = hash & STRING_TABLE_MASK;
227+
const uint32_t idx = hash & Table::TABLE_MASK;
237228

238-
MutexLock lock(mutex);
239-
_data = _table[idx];
229+
MutexLock lock(Table::mutex);
230+
_data = Table::table[idx];
240231

241232
while (_data) {
242233
// compare hash first
243-
if (_data->hash == hash && _data->operator==(p_name)) {
234+
if (_data->hash == hash && _data->name == p_name) {
244235
break;
245236
}
246237
_data = _data->next;
@@ -264,8 +255,7 @@ StringName::StringName(const char *p_name, bool p_static) {
264255
_data->refcount.init();
265256
_data->static_count.set(p_static ? 1 : 0);
266257
_data->hash = hash;
267-
_data->idx = idx;
268-
_data->next = _table[idx];
258+
_data->next = Table::table[idx];
269259
_data->prev = nullptr;
270260

271261
#ifdef DEBUG_ENABLED
@@ -275,10 +265,10 @@ StringName::StringName(const char *p_name, bool p_static) {
275265
_data->static_count.increment();
276266
}
277267
#endif
278-
if (_table[idx]) {
279-
_table[idx]->prev = _data;
268+
if (Table::table[idx]) {
269+
Table::table[idx]->prev = _data;
280270
}
281-
_table[idx] = _data;
271+
Table::table[idx] = _data;
282272
}
283273

284274
StringName::StringName(const String &p_name, bool p_static) {
@@ -291,13 +281,13 @@ StringName::StringName(const String &p_name, bool p_static) {
291281
}
292282

293283
const uint32_t hash = p_name.hash();
294-
const uint32_t idx = hash & STRING_TABLE_MASK;
284+
const uint32_t idx = hash & Table::TABLE_MASK;
295285

296-
MutexLock lock(mutex);
297-
_data = _table[idx];
286+
MutexLock lock(Table::mutex);
287+
_data = Table::table[idx];
298288

299289
while (_data) {
300-
if (_data->hash == hash && _data->operator==(p_name)) {
290+
if (_data->hash == hash && _data->name == p_name) {
301291
break;
302292
}
303293
_data = _data->next;
@@ -321,8 +311,7 @@ StringName::StringName(const String &p_name, bool p_static) {
321311
_data->refcount.init();
322312
_data->static_count.set(p_static ? 1 : 0);
323313
_data->hash = hash;
324-
_data->idx = idx;
325-
_data->next = _table[idx];
314+
_data->next = Table::table[idx];
326315
_data->prev = nullptr;
327316
#ifdef DEBUG_ENABLED
328317
if (unlikely(debug_stringname)) {
@@ -332,10 +321,10 @@ StringName::StringName(const String &p_name, bool p_static) {
332321
}
333322
#endif
334323

335-
if (_table[idx]) {
336-
_table[idx]->prev = _data;
324+
if (Table::table[idx]) {
325+
Table::table[idx]->prev = _data;
337326
}
338-
_table[idx] = _data;
327+
Table::table[idx] = _data;
339328
}
340329

341330
StringName StringName::search(const char *p_name) {
@@ -347,14 +336,14 @@ StringName StringName::search(const char *p_name) {
347336
}
348337

349338
const uint32_t hash = String::hash(p_name);
350-
const uint32_t idx = hash & STRING_TABLE_MASK;
339+
const uint32_t idx = hash & Table::TABLE_MASK;
351340

352-
MutexLock lock(mutex);
353-
_Data *_data = _table[idx];
341+
MutexLock lock(Table::mutex);
342+
_Data *_data = Table::table[idx];
354343

355344
while (_data) {
356345
// compare hash first
357-
if (_data->hash == hash && _data->operator==(p_name)) {
346+
if (_data->hash == hash && _data->name == p_name) {
358347
break;
359348
}
360349
_data = _data->next;
@@ -382,14 +371,14 @@ StringName StringName::search(const char32_t *p_name) {
382371
}
383372

384373
const uint32_t hash = String::hash(p_name);
385-
const uint32_t idx = hash & STRING_TABLE_MASK;
374+
const uint32_t idx = hash & Table::TABLE_MASK;
386375

387-
MutexLock lock(mutex);
388-
_Data *_data = _table[idx];
376+
MutexLock lock(Table::mutex);
377+
_Data *_data = Table::table[idx];
389378

390379
while (_data) {
391380
// compare hash first
392-
if (_data->hash == hash && _data->operator==(p_name)) {
381+
if (_data->hash == hash && _data->name == p_name) {
393382
break;
394383
}
395384
_data = _data->next;
@@ -406,14 +395,14 @@ StringName StringName::search(const String &p_name) {
406395
ERR_FAIL_COND_V(p_name.is_empty(), StringName());
407396

408397
const uint32_t hash = p_name.hash();
409-
const uint32_t idx = hash & STRING_TABLE_MASK;
398+
const uint32_t idx = hash & Table::TABLE_MASK;
410399

411-
MutexLock lock(mutex);
412-
_Data *_data = _table[idx];
400+
MutexLock lock(Table::mutex);
401+
_Data *_data = Table::table[idx];
413402

414403
while (_data) {
415404
// compare hash first
416-
if (_data->hash == hash && _data->operator==(p_name)) {
405+
if (_data->hash == hash && _data->name == p_name) {
417406
break;
418407
}
419408
_data = _data->next;

core/string/string_name.h

+1-14
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,7 @@
3939
class Main;
4040

4141
class StringName {
42-
enum {
43-
STRING_TABLE_BITS = 16,
44-
STRING_TABLE_LEN = 1 << STRING_TABLE_BITS,
45-
STRING_TABLE_MASK = STRING_TABLE_LEN - 1
46-
};
42+
struct Table;
4743

4844
struct _Data {
4945
SafeRefCount refcount;
@@ -52,28 +48,19 @@ class StringName {
5248
#ifdef DEBUG_ENABLED
5349
uint32_t debug_references = 0;
5450
#endif
55-
const String &get_name() const { return name; }
56-
bool operator==(const String &p_name) const;
57-
bool operator!=(const String &p_name) const;
58-
bool operator==(const char *p_name) const;
59-
bool operator!=(const char *p_name) const;
6051

61-
int idx = 0;
6252
uint32_t hash = 0;
6353
_Data *prev = nullptr;
6454
_Data *next = nullptr;
6555
_Data() {}
6656
};
6757

68-
static inline _Data *_table[STRING_TABLE_LEN];
69-
7058
_Data *_data = nullptr;
7159

7260
void unref();
7361
friend void register_core_types();
7462
friend void unregister_core_types();
7563
friend class Main;
76-
static inline Mutex mutex;
7764
static void setup();
7865
static void cleanup();
7966
static uint32_t get_empty_hash();

0 commit comments

Comments
 (0)