-
Notifications
You must be signed in to change notification settings - Fork 530
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
[C++] Compiler remove for-loop in putString methods #668
Comments
https://godbolt.org/z/Ih_3Ye |
This sounds more like it is the result of the compiler finding out how it is being used and removing it. So, it is probably more the usage that can control that than the actual generated code. |
Looks like the problem in generated code, because
|
Also one of string modify methods return copy of *this instead of reference |
@ksergey which variant returns a copy? |
If |
@mjpt777 looks like the issue solved in 1.12.4 @tmontgomery <messageSchema package="sbe" id="0" version="0" byteOrder="littleEndian">
<types>
<type name="String20" primitiveType="char" length="20" semanticType="String"/>
<composite name="messageHeader" description="Template ID and length of message root">
<type name="blockLength" primitiveType="uint16"/>
<type name="templateId" primitiveType="uint16"/>
<type name="schemaId" primitiveType="uint16"/>
<type name="version" primitiveType="uint16"/>
</composite>
</types>
<message name="Message" id="1">
<field name="Text" id="1" type="String20"/>
</message>
</messageSchema> Generated code ( ...
#if __cplusplus >= 201703L [9/930]
Message &putText(const std::string_view str)
{
const size_t srcLength = str.length();
if (srcLength > 20)
{
throw std::runtime_error("string too large for putText [E106]");
}
size_t length = srcLength < 20 ? srcLength : 20;
std::memcpy(m_buffer + m_offset + 0, str.data(), length);
for (size_t start = srcLength; start < 20; ++start)
{
m_buffer[m_offset + 0 + start] = 0;
}
return *this;
}
#else
Message &putText(const std::string& str)
{
const size_t srcLength = str.length();
if (srcLength > 20)
{
throw std::runtime_error("string too large for putText [E106]");
}
size_t length = srcLength < 20 ? srcLength : 20;
std::memcpy(m_buffer + m_offset + 0, str.c_str(), length);
for (size_t start = srcLength; start < 20; ++start)
{
m_buffer[m_offset + 0 + start] = 0;
}
return *this;
}
#endif
... Looks like this issue resolved too in Feel free to close the issue. |
AH. I see. I was looking at the current code and not the version you mentioned. Glad it is taken care of. |
Hello devs!
I have a message:
The sbe-tool generate c++ code:
The problem is compiler (gcc 7 in my case) removes for-loop in case of flag
-O2
set.Is it possible to fix the issue inside sbe-tool?
Thanks
The text was updated successfully, but these errors were encountered: