Optimize TTR
, TTRN
, RTR
, RTRN
to accept StringName
instead of String
for the translation key.
#101547
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This avoids unnecessary allocation, and should make calls to translate slightly faster.
This PR has the side effect of crashing the program if
TTR
(or any of the other functions) are called before theStringName
table is initialized. Unit tests should fail if this were the case anywhere in the codebase, especially becauseTTR
calls will fail before the translation server is started anyway.(I needed to fix one instance of this problem recently in #101592).
Reasoning
Let's look at the current implementation of
TTR
as an example.On call, the
char *
is converted toString
by allocating memory and copying into it.Then, it could go 2 ways. If
TranslationServer
exists,tool_translate
is called. The function accepts aStringName
, and thus has to convertString
toStringName
. IfTranslationServer
does not exist, the string is returned unchanged.To summarize, a
String
object is always allocated, and is usually converted toStringName
.Let's change the function to take
StringName
:On call, the
char *
is converted toStringName
by searching theStringName
table without allocation. This is (nearly) guaranteed because whatever is passed intoTTR
corresponds to aStringName
that was already created for the translation server.Then, it could go 2 ways. If
TranslationServer
exists,tool_translate
is called.p_text
is passed to it without change. IfTranslationServer
does not exist, theStringName
has to be converted toString
.To summarize, a
StringName
object is always used, and is rarely converted toString
.Verdict
The new implementation is almost always cheaper than the old implementation, except when
TranslationServer
does not exist, and the input is already aString
(instead ofchar *
). In this case, theStringName
database is unnecessarily searched (though no unnecessary data is allocated, and thus not much time is lost). I don't think this case is important, because searches forTTR\([^"]
and similar do not yield many results.Caveats
Running the engine seems to be failing with this change:
This is probably because
RTR
is called before theStringName
table could be set up. This could be solved if theStringName
table is set up earlier.It could also be solved by changing the parameter type of the
RTR
-like functions toStrRange
, but thenStrName
arguments would have to be hashed unnecessarily (which is the case on current master anyway, but alas).