From a66eaa43481e2cf7b2c79de9e7594f07502c910b Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Sat, 28 Nov 2015 11:42:01 -0800 Subject: [PATCH] Reduce overhead of setErrorX() calls. Checking the status code in the macro before "context" is used avoids significant overhead (string processing) in the common case when the code is zero. Change-Id: I69b8b220187ac1ab905cdf56dde5c4b6c61101b7 --- wpilibc/shared/include/ErrorBase.h | 62 ++++++++++++++++++------------ 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/wpilibc/shared/include/ErrorBase.h b/wpilibc/shared/include/ErrorBase.h index 0efa548132..4730a295fe 100644 --- a/wpilibc/shared/include/ErrorBase.h +++ b/wpilibc/shared/include/ErrorBase.h @@ -13,35 +13,47 @@ #include "llvm/StringRef.h" #define wpi_setErrnoErrorWithContext(context) \ - (this->SetErrnoError((context), __FILE__, __FUNCTION__, __LINE__)) -#define wpi_setErrnoError() (wpi_setErrnoErrorWithContext("")) -#define wpi_setImaqErrorWithContext(code, context) \ - (this->SetImaqError((code), (context), __FILE__, __FUNCTION__, __LINE__)) -#define wpi_setErrorWithContext(code, context) \ - (this->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__)) -#define wpi_setError(code) (wpi_setErrorWithContext(code, "")) -#define wpi_setStaticErrorWithContext(object, code, context) \ - (object->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__)) + this->SetErrnoError((context), __FILE__, __FUNCTION__, __LINE__) +#define wpi_setErrnoError() wpi_setErrnoErrorWithContext("") +#define wpi_setImaqErrorWithContext(code, context) \ + do { \ + if ((code) != 0) \ + this->SetImaqError((code), (context), __FILE__, __FUNCTION__, __LINE__); \ + } while (0) +#define wpi_setErrorWithContext(code, context) \ + do { \ + if ((code) != 0) \ + this->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \ + } while (0) +#define wpi_setError(code) wpi_setErrorWithContext(code, "") +#define wpi_setStaticErrorWithContext(object, code, context) \ + do { \ + if ((code) != 0) \ + object->SetError((code), (context), __FILE__, __FUNCTION__, __LINE__); \ + } while (0) #define wpi_setStaticError(object, code) \ - (wpi_setStaticErrorWithContext(object, code, "")) -#define wpi_setGlobalErrorWithContext(code, context) \ - (ErrorBase::SetGlobalError((code), (context), __FILE__, __FUNCTION__, \ - __LINE__)) -#define wpi_setGlobalError(code) (wpi_setGlobalErrorWithContext(code, "")) -#define wpi_setWPIErrorWithContext(error, context) \ - (this->SetWPIError((wpi_error_s_##error), (wpi_error_value_##error), \ - (context), __FILE__, __FUNCTION__, __LINE__)) + wpi_setStaticErrorWithContext(object, code, "") +#define wpi_setGlobalErrorWithContext(code, context) \ + do { \ + if ((code) != 0) \ + ErrorBase::SetGlobalError((code), (context), __FILE__, __FUNCTION__, \ + __LINE__); \ + } while (0) +#define wpi_setGlobalError(code) wpi_setGlobalErrorWithContext(code, "") +#define wpi_setWPIErrorWithContext(error, context) \ + this->SetWPIError((wpi_error_s_##error), (wpi_error_value_##error), \ + (context), __FILE__, __FUNCTION__, __LINE__) #define wpi_setWPIError(error) (wpi_setWPIErrorWithContext(error, "")) -#define wpi_setStaticWPIErrorWithContext(object, error, context) \ - (object->SetWPIError((wpi_error_s_##error), (context), __FILE__, \ - __FUNCTION__, __LINE__)) +#define wpi_setStaticWPIErrorWithContext(object, error, context) \ + object->SetWPIError((wpi_error_s_##error), (context), __FILE__, \ + __FUNCTION__, __LINE__) #define wpi_setStaticWPIError(object, error) \ - (wpi_setStaticWPIErrorWithContext(object, error, "")) -#define wpi_setGlobalWPIErrorWithContext(error, context) \ - (ErrorBase::SetGlobalWPIError((wpi_error_s_##error), (context), __FILE__, \ - __FUNCTION__, __LINE__)) + wpi_setStaticWPIErrorWithContext(object, error, "") +#define wpi_setGlobalWPIErrorWithContext(error, context) \ + ErrorBase::SetGlobalWPIError((wpi_error_s_##error), (context), __FILE__, \ + __FUNCTION__, __LINE__) #define wpi_setGlobalWPIError(error) \ - (wpi_setGlobalWPIErrorWithContext(error, "")) + wpi_setGlobalWPIErrorWithContext(error, "") /** * Base class for most objects.