Skip to content

Commit dbf189e

Browse files
committed
Lock the StringName::mutex after hashing the string, to spend less time hoarding it unnecessarily.
1 parent d19147e commit dbf189e

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

core/string/string_name.cpp

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,10 @@ StringName::StringName(const char *p_name, bool p_static) {
269269
return; //empty, ignore
270270
}
271271

272-
MutexLock lock(mutex);
273-
274-
uint32_t hash = String::hash(p_name);
275-
276-
uint32_t idx = hash & STRING_TABLE_MASK;
272+
const uint32_t hash = String::hash(p_name);
273+
const uint32_t idx = hash & STRING_TABLE_MASK;
277274

275+
MutexLock lock(mutex);
278276
_data = _table[idx];
279277

280278
while (_data) {
@@ -328,12 +326,10 @@ StringName::StringName(const StaticCString &p_static_string, bool p_static) {
328326

329327
ERR_FAIL_COND(!p_static_string.ptr || !p_static_string.ptr[0]);
330328

331-
MutexLock lock(mutex);
332-
333-
uint32_t hash = String::hash(p_static_string.ptr);
334-
335-
uint32_t idx = hash & STRING_TABLE_MASK;
329+
const uint32_t hash = String::hash(p_static_string.ptr);
330+
const uint32_t idx = hash & STRING_TABLE_MASK;
336331

332+
MutexLock lock(mutex);
337333
_data = _table[idx];
338334

339335
while (_data) {
@@ -388,11 +384,10 @@ StringName::StringName(const String &p_name, bool p_static) {
388384
return;
389385
}
390386

391-
MutexLock lock(mutex);
392-
393-
uint32_t hash = p_name.hash();
394-
uint32_t idx = hash & STRING_TABLE_MASK;
387+
const uint32_t hash = p_name.hash();
388+
const uint32_t idx = hash & STRING_TABLE_MASK;
395389

390+
MutexLock lock(mutex);
396391
_data = _table[idx];
397392

398393
while (_data) {
@@ -446,11 +441,10 @@ StringName StringName::search(const char *p_name) {
446441
return StringName();
447442
}
448443

449-
MutexLock lock(mutex);
450-
451-
uint32_t hash = String::hash(p_name);
452-
uint32_t idx = hash & STRING_TABLE_MASK;
444+
const uint32_t hash = String::hash(p_name);
445+
const uint32_t idx = hash & STRING_TABLE_MASK;
453446

447+
MutexLock lock(mutex);
454448
_Data *_data = _table[idx];
455449

456450
while (_data) {
@@ -482,12 +476,10 @@ StringName StringName::search(const char32_t *p_name) {
482476
return StringName();
483477
}
484478

485-
MutexLock lock(mutex);
486-
487-
uint32_t hash = String::hash(p_name);
488-
489-
uint32_t idx = hash & STRING_TABLE_MASK;
479+
const uint32_t hash = String::hash(p_name);
480+
const uint32_t idx = hash & STRING_TABLE_MASK;
490481

482+
MutexLock lock(mutex);
491483
_Data *_data = _table[idx];
492484

493485
while (_data) {
@@ -508,12 +500,10 @@ StringName StringName::search(const char32_t *p_name) {
508500
StringName StringName::search(const String &p_name) {
509501
ERR_FAIL_COND_V(p_name.is_empty(), StringName());
510502

511-
MutexLock lock(mutex);
512-
513-
uint32_t hash = p_name.hash();
514-
515-
uint32_t idx = hash & STRING_TABLE_MASK;
503+
const uint32_t hash = p_name.hash();
504+
const uint32_t idx = hash & STRING_TABLE_MASK;
516505

506+
MutexLock lock(mutex);
517507
_Data *_data = _table[idx];
518508

519509
while (_data) {

0 commit comments

Comments
 (0)