-
Notifications
You must be signed in to change notification settings - Fork 81
[GEN][ZH] Reduce cost of GameMessage::getCommandTypeAsAsciiString by 70% #1259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
[GEN][ZH] Reduce cost of GameMessage::getCommandTypeAsAsciiString by 70% #1259
Conversation
Reviewing will probably be easier per commit. I tried to keep the commits as clean as possible; the first two only change the macro name and nothing else. |
Can you measure the cost of this function please? |
Measure what exactly? In isolation or in-game? It's going to be an improvement either way, but very minimal. This code is only used in debug mode (well, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks correct.
is the point of the original code to be a stupid hack to check the enum against the enum name? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it does not yeild a measurable performance boost then its not a change worth making now especially if it only affects debug builds and can be done better after dropping vc6.
But there are other reasons to consider updating the code. The original code does looks very hacky, relying on some weird macro tricks.
We should investigate what is really happening here and see if it can be done with more standard syntax.
Actually the implementation is correct but since we have access to modern text editors and can edit multiple lines at the same time, I think its better to skip the macro altogether and just use switch statements directly. Having to write the enum name twice is really not much work with find replace and regex |
I'm hardly a fan of macros but think it's fine here for the time being. It's simple and it's local to the function so it doesn't leak through to other code.
This would introduce the potential for typos or string literals that don't match (changed) enum values, that's just bad for maintainability imo. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its inherently problamatic code so unfortunately there isn't really a good local fix especially with just c++98 so I think your implementation is a decent enough improvement for the time being
@@ -230,421 +230,435 @@ AsciiString GameMessage::getCommandAsAsciiString( void ) | |||
|
|||
AsciiString GameMessage::getCommandTypeAsAsciiString(GameMessage::Type t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another oprimization we can do here is return const char* instead of AsciiString, because it is not necessary to allocate here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. I can change that. That does require a change to getContentsAsAsciiString
as well. Should I just do the change or remove the entire function? (#1259 (comment))
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can remove the functions if you like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed return type to const char*
and removed function AsciiString getContentsAsAsciiString
.
@@ -228,456 +228,469 @@ AsciiString GameMessage::getCommandAsAsciiString( void ) | |||
return getCommandTypeAsAsciiString(m_type); | |||
} | |||
|
|||
AsciiString GameMessage::getCommandTypeAsAsciiString(GameMessage::Type t) | |||
const char* GameMessage::getCommandTypeAsAsciiString(GameMessage::Type t) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about we remove this function as well? It is unused. Or did I look wrong?
Other than that, the function name is getCommandTypeAsAsciiString
but it now returns const char*
. It would have to be named getCommandTypeName
then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That one is still used. I updated the names and return types of both functions, as well as the function calls for getCommandAsAsciiString
.
Performance measurement{
std::srand(111);
int64_t begin = GetTickCount64();
GameMessage::Type t = GameMessage::Type(std::rand() % GameMessage::MSG_COUNT);
//GameMessage::Type t = GameMessage::Type(GameMessage::MSG_COUNT / 2);
for (Int i = 0; i < 10000000; ++i)
{
AsciiString str = GameMessage::getCommandTypeAsAsciiString(t);
DEBUG_ASSERTCRASH(!str.isEmpty());
}
int64_t time = GetTickCount64() - begin;
DEBUG_LOG(("time old %lld ms", time));
}
{
std::srand(111);
int64_t begin = GetTickCount64();
GameMessage::Type t = GameMessage::Type(std::rand() % GameMessage::MSG_COUNT);
//GameMessage::Type t = GameMessage::Type(GameMessage::MSG_COUNT / 2);
for (Int i = 0; i < 10000000; ++i)
{
AsciiString str = GameMessage::getCommandTypeAsString(t);
DEBUG_ASSERTCRASH(!str.isEmpty());
}
int64_t time = GetTickCount64() - begin;
DEBUG_LOG(("time new %lld ms", time));
} Result
Over 70% faster. |
GeneralsGameCode/GeneralsMD/Code/GameEngine/Source/Common/MessageStream.cpp
Lines 231 to 250 in 9527b54
This PR changes a long list of if statements in
getCommandTypeAsAsciiString
into a single switch statement for slightly better performance.TODO