Commit 3940a453 by Austin Kinross Committed by Geoff Lang

Only call std::string constructor in gl::Error when necessary

std::string's constructor is very expensive in debug builds. In some applications we have seen it use 10% of the CPU. This change makes (e)gl::Error store a pointer to an std::string, and only create a string when necessary. Change-Id: I78bfbda86568944681517fe32df9d1556e4ebee7 Reviewed-on: https://chromium-review.googlesource.com/240495Tested-by: 's avatarAustin Kinross <aukinros@microsoft.com> Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Reviewed-by: 's avatarGeoff Lang <geofflang@chromium.org>
parent 7700ff65
......@@ -18,33 +18,68 @@ namespace gl
Error::Error(GLenum errorCode)
: mCode(errorCode),
mMessage()
mMessage(NULL)
{
}
Error::Error(GLenum errorCode, const char *msg, ...)
: mCode(errorCode),
mMessage()
mMessage(NULL)
{
va_list vararg;
va_start(vararg, msg);
mMessage = FormatString(msg, vararg);
createMessageString();
*mMessage = FormatString(msg, vararg);
va_end(vararg);
}
Error::Error(const Error &other)
: mCode(other.mCode),
mMessage(other.mMessage)
mMessage(NULL)
{
if (other.mMessage != NULL)
{
createMessageString();
*mMessage = *(other.mMessage);
}
}
Error::~Error()
{
SafeDelete(mMessage);
}
Error &Error::operator=(const Error &other)
{
mCode = other.mCode;
mMessage = other.mMessage;
if (other.mMessage != NULL)
{
createMessageString();
*mMessage = *(other.mMessage);
}
else
{
SafeDelete(mMessage);
}
return *this;
}
void Error::createMessageString() const
{
if (mMessage != NULL)
{
mMessage = new std::string();
}
}
const std::string &Error::getMessage() const
{
createMessageString();
return *mMessage;
}
}
namespace egl
......@@ -52,31 +87,66 @@ namespace egl
Error::Error(EGLint errorCode)
: mCode(errorCode),
mMessage()
mMessage(NULL)
{
}
Error::Error(EGLint errorCode, const char *msg, ...)
: mCode(errorCode),
mMessage()
mMessage(NULL)
{
va_list vararg;
va_start(vararg, msg);
mMessage = FormatString(msg, vararg);
createMessageString();
*mMessage = FormatString(msg, vararg);
va_end(vararg);
}
Error::Error(const Error &other)
: mCode(other.mCode),
mMessage(other.mMessage)
mMessage(NULL)
{
if (other.mMessage != NULL)
{
createMessageString();
*mMessage = *(other.mMessage);
}
}
Error::~Error()
{
SafeDelete(mMessage);
}
Error &Error::operator=(const Error &other)
{
mCode = other.mCode;
mMessage = other.mMessage;
if (other.mMessage != NULL)
{
createMessageString();
*mMessage = *(other.mMessage);
}
else
{
SafeDelete(mMessage);
}
return *this;
}
void Error::createMessageString() const
{
if (mMessage != NULL)
{
mMessage = new std::string();
}
}
const std::string &Error::getMessage() const
{
createMessageString();
return *mMessage;
}
}
......@@ -23,16 +23,19 @@ class Error
explicit Error(GLenum errorCode);
Error(GLenum errorCode, const char *msg, ...);
Error(const Error &other);
~Error();
Error &operator=(const Error &other);
GLenum getCode() const { return mCode; }
bool isError() const { return (mCode != GL_NO_ERROR); }
const std::string &getMessage() const { return mMessage; }
const std::string &getMessage() const;
private:
void createMessageString() const;
GLenum mCode;
std::string mMessage;
mutable std::string *mMessage;
};
}
......@@ -46,16 +49,19 @@ class Error
explicit Error(EGLint errorCode);
Error(EGLint errorCode, const char *msg, ...);
Error(const Error &other);
~Error();
Error &operator=(const Error &other);
EGLint getCode() const { return mCode; }
bool isError() const { return (mCode != EGL_SUCCESS); }
const std::string &getMessage() const { return mMessage; }
const std::string &getMessage() const;
private:
void createMessageString() const;
EGLint mCode;
std::string mMessage;
mutable std::string *mMessage;
};
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment