Skip to content

Commit a093bdb

Browse files
author
L'In20Cible
committed
Increased performance of StringTable.__contains__.
1 parent 805daf5 commit a093bdb

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

addons/source-python/packages/source-python/engines/sound.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ def is_precached(self):
325325
"""
326326
# We can't use engine_sound.is_sound_precached here because it always
327327
# returns True.
328-
return string_tables.soundprecache[self.sample] != INVALID_STRING_INDEX
328+
return self.sample in string_tables.soundprecache
329329

330330

331331
class StreamSound(_BaseSound):
@@ -372,5 +372,4 @@ def is_precached(self):
372372
"""
373373
# We can't use engine_sound.is_sound_precached here because it always
374374
# returns True.
375-
index = string_tables.soundprecache[self._stream_sample]
376-
return index != INVALID_STRING_INDEX
375+
return self._stream_sample in string_tables.soundprecache

src/core/modules/stringtables/stringtables_wrap.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ int GetStringUserDataLength( INetworkStringTable *pTable, const char *string )
150150
return GetStringIndexUserDataLength(pTable, index);
151151
}
152152

153+
//---------------------------------------------------------------------------------
154+
// Returns whether or not the given string is in the table
155+
//---------------------------------------------------------------------------------
156+
bool __contains__( INetworkStringTable *pTable, const char *string )
157+
{
158+
return pTable->FindStringIndex(string) != INVALID_STRING_INDEX;
159+
}
160+
153161
//---------------------------------------------------------------------------------
154162
// Exposes INetworkStringTable.
155163
//---------------------------------------------------------------------------------
@@ -184,6 +192,11 @@ void export_stringtable(scope _stringtables)
184192
"Returns the length of the table."
185193
)
186194

195+
.def("__contains__",
196+
&__contains__,
197+
"Returns whether or not the given string is contained in the table."
198+
)
199+
187200
.add_property("max_strings",
188201
&INetworkStringTable::GetMaxStrings,
189202
"Returns the max length of the table (read-only)."

0 commit comments

Comments
 (0)