33
33
#include " core/os/os.h"
34
34
#include " core/string/print_string.h"
35
35
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 ;
43
40
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
+ };
51
44
52
45
void StringName::setup () {
53
46
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 ;
56
49
}
57
50
configured = true ;
58
51
}
59
52
60
53
void StringName::cleanup () {
61
- MutexLock lock (mutex);
54
+ MutexLock lock (Table:: mutex);
62
55
63
56
#ifdef DEBUG_ENABLED
64
57
if (unlikely (debug_stringname)) {
65
58
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];
68
61
while (d) {
69
62
data.push_back (d);
70
63
d = d->next ;
@@ -77,7 +70,7 @@ void StringName::cleanup() {
77
70
int unreferenced_stringnames = 0 ;
78
71
int rarely_referenced_stringnames = 0 ;
79
72
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 ));
81
74
if (data[i]->debug_references == 0 ) {
82
75
unreferenced_stringnames += 1 ;
83
76
} else if (data[i]->debug_references < 5 ) {
@@ -90,9 +83,9 @@ void StringName::cleanup() {
90
83
}
91
84
#endif
92
85
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];
96
89
if (d->static_count .get () != d->refcount .get ()) {
97
90
lost_strings++;
98
91
@@ -101,7 +94,7 @@ void StringName::cleanup() {
101
94
}
102
95
}
103
96
104
- _table [i] = _table [i]->next ;
97
+ Table::table [i] = Table::table [i]->next ;
105
98
memdelete (d);
106
99
}
107
100
}
@@ -115,18 +108,16 @@ void StringName::unref() {
115
108
ERR_FAIL_COND (!configured);
116
109
117
110
if (_data && _data->refcount .unref ()) {
118
- MutexLock lock (mutex);
111
+ MutexLock lock (Table:: mutex);
119
112
120
113
if (CoreGlobals::leak_reporting_enabled && _data->static_count .get () > 0 ) {
121
114
ERR_PRINT (" BUG: Unreferenced static string to 0: " + _data->name );
122
115
}
123
116
if (_data->prev ) {
124
117
_data->prev ->next = _data->next ;
125
118
} 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 ;
130
121
}
131
122
132
123
if (_data->next ) {
@@ -145,15 +136,15 @@ uint32_t StringName::get_empty_hash() {
145
136
146
137
bool StringName::operator ==(const String &p_name) const {
147
138
if (_data) {
148
- return _data->operator ==( p_name) ;
139
+ return _data->name == p_name;
149
140
}
150
141
151
142
return p_name.is_empty ();
152
143
}
153
144
154
145
bool StringName::operator ==(const char *p_name) const {
155
146
if (_data) {
156
- return _data->operator ==( p_name) ;
147
+ return _data->name == p_name;
157
148
}
158
149
159
150
return p_name[0 ] == 0 ;
@@ -217,7 +208,7 @@ StringName::StringName(const StringName &p_name) {
217
208
}
218
209
219
210
void StringName::assign_static_unique_class_name (StringName *ptr, const char *p_name) {
220
- MutexLock lock (mutex);
211
+ MutexLock lock (Table:: mutex);
221
212
if (*ptr == StringName ()) {
222
213
*ptr = StringName (p_name, true );
223
214
}
@@ -233,14 +224,14 @@ StringName::StringName(const char *p_name, bool p_static) {
233
224
}
234
225
235
226
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 ;
237
228
238
- MutexLock lock (mutex);
239
- _data = _table [idx];
229
+ MutexLock lock (Table:: mutex);
230
+ _data = Table::table [idx];
240
231
241
232
while (_data) {
242
233
// compare hash first
243
- if (_data->hash == hash && _data->operator ==( p_name) ) {
234
+ if (_data->hash == hash && _data->name == p_name) {
244
235
break ;
245
236
}
246
237
_data = _data->next ;
@@ -264,8 +255,7 @@ StringName::StringName(const char *p_name, bool p_static) {
264
255
_data->refcount .init ();
265
256
_data->static_count .set (p_static ? 1 : 0 );
266
257
_data->hash = hash;
267
- _data->idx = idx;
268
- _data->next = _table[idx];
258
+ _data->next = Table::table[idx];
269
259
_data->prev = nullptr ;
270
260
271
261
#ifdef DEBUG_ENABLED
@@ -275,10 +265,10 @@ StringName::StringName(const char *p_name, bool p_static) {
275
265
_data->static_count .increment ();
276
266
}
277
267
#endif
278
- if (_table [idx]) {
279
- _table [idx]->prev = _data;
268
+ if (Table::table [idx]) {
269
+ Table::table [idx]->prev = _data;
280
270
}
281
- _table [idx] = _data;
271
+ Table::table [idx] = _data;
282
272
}
283
273
284
274
StringName::StringName (const String &p_name, bool p_static) {
@@ -291,13 +281,13 @@ StringName::StringName(const String &p_name, bool p_static) {
291
281
}
292
282
293
283
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 ;
295
285
296
- MutexLock lock (mutex);
297
- _data = _table [idx];
286
+ MutexLock lock (Table:: mutex);
287
+ _data = Table::table [idx];
298
288
299
289
while (_data) {
300
- if (_data->hash == hash && _data->operator ==( p_name) ) {
290
+ if (_data->hash == hash && _data->name == p_name) {
301
291
break ;
302
292
}
303
293
_data = _data->next ;
@@ -321,8 +311,7 @@ StringName::StringName(const String &p_name, bool p_static) {
321
311
_data->refcount .init ();
322
312
_data->static_count .set (p_static ? 1 : 0 );
323
313
_data->hash = hash;
324
- _data->idx = idx;
325
- _data->next = _table[idx];
314
+ _data->next = Table::table[idx];
326
315
_data->prev = nullptr ;
327
316
#ifdef DEBUG_ENABLED
328
317
if (unlikely (debug_stringname)) {
@@ -332,10 +321,10 @@ StringName::StringName(const String &p_name, bool p_static) {
332
321
}
333
322
#endif
334
323
335
- if (_table [idx]) {
336
- _table [idx]->prev = _data;
324
+ if (Table::table [idx]) {
325
+ Table::table [idx]->prev = _data;
337
326
}
338
- _table [idx] = _data;
327
+ Table::table [idx] = _data;
339
328
}
340
329
341
330
StringName StringName::search (const char *p_name) {
@@ -347,14 +336,14 @@ StringName StringName::search(const char *p_name) {
347
336
}
348
337
349
338
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 ;
351
340
352
- MutexLock lock (mutex);
353
- _Data *_data = _table [idx];
341
+ MutexLock lock (Table:: mutex);
342
+ _Data *_data = Table::table [idx];
354
343
355
344
while (_data) {
356
345
// compare hash first
357
- if (_data->hash == hash && _data->operator ==( p_name) ) {
346
+ if (_data->hash == hash && _data->name == p_name) {
358
347
break ;
359
348
}
360
349
_data = _data->next ;
@@ -382,14 +371,14 @@ StringName StringName::search(const char32_t *p_name) {
382
371
}
383
372
384
373
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 ;
386
375
387
- MutexLock lock (mutex);
388
- _Data *_data = _table [idx];
376
+ MutexLock lock (Table:: mutex);
377
+ _Data *_data = Table::table [idx];
389
378
390
379
while (_data) {
391
380
// compare hash first
392
- if (_data->hash == hash && _data->operator ==( p_name) ) {
381
+ if (_data->hash == hash && _data->name == p_name) {
393
382
break ;
394
383
}
395
384
_data = _data->next ;
@@ -406,14 +395,14 @@ StringName StringName::search(const String &p_name) {
406
395
ERR_FAIL_COND_V (p_name.is_empty (), StringName ());
407
396
408
397
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 ;
410
399
411
- MutexLock lock (mutex);
412
- _Data *_data = _table [idx];
400
+ MutexLock lock (Table:: mutex);
401
+ _Data *_data = Table::table [idx];
413
402
414
403
while (_data) {
415
404
// compare hash first
416
- if (_data->hash == hash && _data->operator ==( p_name) ) {
405
+ if (_data->hash == hash && _data->name == p_name) {
417
406
break ;
418
407
}
419
408
_data = _data->next ;
0 commit comments