Reduce footprint by using string literals.
authorOssama Othman <ossama.othman@intel.com>
Tue, 13 Jan 2015 18:24:49 +0000 (10:24 -0800)
committerOssama Othman <ossama.othman@intel.com>
Tue, 13 Jan 2015 23:23:13 +0000 (23:23 +0000)
This change replaces the std::string based exception description
string constants in resource/include/StringConstants.h with
string literals instead.  Switching to string literals has a number of
advantages in this case:

- reduced start-up time for binaries that link against liboc
  since the static std::string constants no longer have to
  be initialized at run-time.
- the compiler is free to place the strings in read-only
  memory.
- reduced static and run-time footprint.

Footprint reductions in text, data and bss are as follows:

filename                        text
                        std::   const
                        string  char[]  Diff    Change
InProcClientWrapper.o   81458   77684   -3774   -4.63%
InProcServerWrapper.o   43292   40122   -3170   -7.32%
OCException.o           5399    1956    -3443   -63.77%
OCPlatform_impl.o       25054   21803   -3251   -12.98%
OCPlatform.o            9574    5535    -4039   -42.19%
OCRepresentation.o      97156   93782   -3374   -3.47%
OCResource.o            28565   25048   -3517   -12.31%
OCUtilities.o           19844   15805   -4039   -20.35%
liboc.so                453688  434400  -19288  -4.25%

filename                        data
                        std::   const
                        string  char[]  Diff    Change
InProcClientWrapper.o   1916    1916    0       0.00%
InProcServerWrapper.o   1368    1368    0       0.00%
OCException.o           4       4       0       0.00%
OCPlatform_impl.o       552     552     0       0.00%
OCPlatform.o            8       8       0       0.00%
OCRepresentation.o      388     388     0       0.00%
OCResource.o            100     100     0       0.00%
OCUtilities.o           344     344     0       0.00%
liboc.so                6636    6652    16      0.24%

filename                        bss
                        std::   const
                        string  char[]  Diff    Change
InProcClientWrapper.o   577     309     -268    -46.45%
InProcServerWrapper.o   657     389     -268    -40.79%
OCException.o           320     52      -268    -83.75%
OCPlatform_impl.o       420     152     -268    -63.81%
OCPlatform.o            340     72      -268    -78.82%
OCRepresentation.o      328     60      -268    -81.71%
OCResource.o            340     72      -268    -78.82%
OCUtilities.o           340     72      -268    -78.82%
liboc.so                3340    1192    -2148   -64.31%

filename                        dec
                        std::   const
                        string  char[]  Diff    Change
InProcClientWrapper.o   83951   79909   -4042   -4.81%
InProcServerWrapper.o   45317   41879   -3438   -7.59%
OCException.o           5723    2012    -3711   -64.84%
OCPlatform_impl.o       26026   22507   -3519   -13.52%
OCPlatform.o            9922    5615    -4307   -43.41%
OCRepresentation.o      97872   94230   -3642   -3.72%
OCResource.o            29005   25220   -3785   -13.05%
OCUtilities.o           20528   16221   -4307   -20.98%
liboc.so                463664  442244  -21420  -4.62%

Overall liboc.so drops in size by 20 kilobytes, a 4.62%
reduction in size with these changes in place.

Change-Id: I3e24f56bf702a0ea134b113d542f61e45f8af613
Signed-off-by: Ossama Othman <ossama.othman@intel.com>
resource/include/StringConstants.h

index 048a476..57a9236 100644 (file)
 
 #include <string>
 
-using namespace std;
-
 namespace OC
 {
     namespace InitException
     {
-        static const std::string NO_ERROR                   = "No Error";
-        static const std::string INVALID_URI                = "Invalid URI";
-        static const std::string INVALID_PORT               = "Invalid Port";
-        static const std::string INVALID_IP                 = "Invalid IP";
-        static const std::string INVALID_CB                 = "Invalid Callback";
-        static const std::string INVALID_METHOD             = "Invalid Method";
-        static const std::string GENERAL_FAULT              = "General Fault";
-        static const std::string UNKNOWN_ERROR              = "Unknown Error";
+        static const char NO_ERROR[]                   = "No Error";
+        static const char INVALID_URI[]                = "Invalid URI";
+        static const char INVALID_PORT[]               = "Invalid Port";
+        static const char INVALID_IP[]                 = "Invalid IP";
+        static const char INVALID_CB[]                 = "Invalid Callback";
+        static const char INVALID_METHOD[]             = "Invalid Method";
+        static const char GENERAL_FAULT[]              = "General Fault";
+        static const char UNKNOWN_ERROR[]              = "Unknown Error";
 
-        static const std::string STACK_INIT_ERROR           = "Error Initializing Stack";
-        static const std::string NOT_CONFIGURED_AS_SERVER   =
+        static const char STACK_INIT_ERROR[]           = "Error Initializing Stack";
+        static const char NOT_CONFIGURED_AS_SERVER[]   =
                           "Cannot static construct a Server when configured as a client";
-        static const std::string INVALID_PARAM              = "Invalid Param";
-        static const std::string MISSING_URI                = "Missing URI;";
-        static const std::string MISSING_TYPE               = "Missing Resource Type;";
-        static const std::string MISSING_INTERFACE          = "Missing Interface;";
-        static const std::string MISSING_CLIENT_WRAPPER     = "Missing ClientWrapper;";
+        static const char INVALID_PARAM[]              = "Invalid Param";
+        static const char MISSING_URI[]                = "Missing URI;";
+        static const char MISSING_TYPE[]               = "Missing Resource Type;";
+        static const char MISSING_INTERFACE[]          = "Missing Interface;";
+        static const char MISSING_CLIENT_WRAPPER[]     = "Missing ClientWrapper;";
     }
 
     namespace Exception // Not To Be Confused With 'InitException'
     {
-        static const std::string SVCTYPE_OUTOFPROC          = "ServiceType::OutOfProc";
-        static const std::string BIND_TYPE_FAILED           = "Bind Type to resource failed";
-        static const std::string BIND_INTERFACE_FAILED      = "Bind Interface to resource failed";
-        static const std::string START_PRESENCE_FAILED      = "startPresence failed";
-        static const std::string END_PRESENCE_FAILED        = "stopPresence failed";
-        static const std::string INVALID_ARRAY              = "Array type should have at least []";
-        static const std::string STR_NULL_RESPONSE          = "Response is NULL";
-        static const std::string STR_PAYLOAD_OVERFLOW       = "Payload overflow";
-        static const std::string NIL_GUARD_NULL             = "nullptr at nil_guard()";
-        static const std::string GENERAL_JSON_PARSE_FAILED  = "JSON Parser Error";
-        static const std::string RESOURCE_UNREG_FAILED      = "Unregistering resource failed";
-        static const std::string OPTION_ID_RANGE_INVALID    =
+        static const char SVCTYPE_OUTOFPROC[]          = "ServiceType::OutOfProc";
+        static const char BIND_TYPE_FAILED[]           = "Bind Type to resource failed";
+        static const char BIND_INTERFACE_FAILED[]      = "Bind Interface to resource failed";
+        static const char START_PRESENCE_FAILED[]      = "startPresence failed";
+        static const char END_PRESENCE_FAILED[]        = "stopPresence failed";
+        static const char INVALID_ARRAY[]              = "Array type should have at least []";
+        static const char STR_NULL_RESPONSE[]          = "Response is NULL";
+        static const char STR_PAYLOAD_OVERFLOW[]       = "Payload overflow";
+        static const char NIL_GUARD_NULL[]             = "nullptr at nil_guard()";
+        static const char GENERAL_JSON_PARSE_FAILED[]  = "JSON Parser Error";
+        static const char RESOURCE_UNREG_FAILED[]      = "Unregistering resource failed";
+        static const char OPTION_ID_RANGE_INVALID[]    =
                             "Error: OptionID valid only from 2048 to 3000 inclusive.";
 
-        static const std::string NO_ERROR                   = "No Error";
-        static const std::string RESOURCE_CREATED           = "Resource Created";
-        static const std::string RESOURCE_DELETED           = "Resource Deleted";
-        static const std::string INVALID_URI                = "Invalid URI";
-        static const std::string INVALID_IP                 = "Invalid IP";
-        static const std::string INVALID_PORT               = "Invalid Port";
-        static const std::string INVALID_CB                 = "Invalid Callback";
-        static const std::string INVALID_METHOD             = "Invalid Method";
-        static const std::string INVALID_QUERY              = "Invalid Query";
-        static const std::string INVALID_PARAM              = "Invalid Param";
-        static const std::string INVALID_OBESERVE           = "Invalid Observe Param";
-        static const std::string NO_MEMORY                  = "No Memory";
-        static const std::string COMM_ERROR                 = "Communication Error";
-        static const std::string NOT_IMPL                   = "Not Implemented";
-        static const std::string NOT_FOUND                  = "Resource Not Found";
-        static const std::string RESOURCE_ERROR             = "Resource Error";
-        static const std::string SLOW_RESOURCE              = "Slow Resource";
-        static const std::string NO_OBSERVERS               = "No Observers";
-        static const std::string OBSV_NO_FOUND              = "Stack observer not found";
-        static const std::string OBSV_NOT_ADDED             = "Stack observer not added";
-        static const std::string OBSV_NOT_REMOVED           = "Stack observer not removed";
-        static const std::string STACK_RESOURCE_DELETED     = "The specified resource has been deleted";
-        static const std::string PRESENCE_STOPPED           = "Stack presence stopped";
-        static const std::string PRESENCE_TIMEOUT           = "Stack presence timed out";
-        static const std::string PRESENCE_NOT_HANDLED       = "Stack presence should not be handled";
-        static const std::string INVALID_OPTION             = "Invalid option";
-        static const std::string GENERAL_FAULT              = "General Fault";
-        static const std::string MALFORMED_STACK_RESPONSE   = "Response from OC_STACK is malformed";
-        static const std::string VIRTUAL_DO_NOT_HANDLE      = "Virtual Do Not Handle";
-        static const std::string PERSISTENT_BUFFER_REQUIRED = "Persistent response buffer required";
-        static const std::string STACK_CONTINUE             = "Stack continue";
-        static const std::string INVALID_REQUEST_HANDLE     = "Invalid request handle";
-        static const std::string UNKNOWN_ERROR              = "Unknown Error";
-        static const std::string INVALID_REPRESENTATION     = "Invalid Payload JSON";
-        static const std::string INVALID_JSON_TYPE          = "Unrecognized JSON Type ";
-        static const std::string INVALID_JSON_NUMERIC       = "Unrecognized JSON Numeric ";
-        static const std::string INVALID_JSON_ARRAY_DEPTH   = "Max JSON Array Depth exceeded";
-        static const std::string INVALID_JSON_TYPE_TAG      = "Invalid JSON Type Tag";
-        static const std::string INVALID_ATTRIBUTE          = "Invalid Attribute: ";
+        static const char NO_ERROR[]                   = "No Error";
+        static const char RESOURCE_CREATED[]           = "Resource Created";
+        static const char RESOURCE_DELETED[]           = "Resource Deleted";
+        static const char INVALID_URI[]                = "Invalid URI";
+        static const char INVALID_IP[]                 = "Invalid IP";
+        static const char INVALID_PORT[]               = "Invalid Port";
+        static const char INVALID_CB[]                 = "Invalid Callback";
+        static const char INVALID_METHOD[]             = "Invalid Method";
+        static const char INVALID_QUERY[]              = "Invalid Query";
+        static const char INVALID_PARAM[]              = "Invalid Param";
+        static const char INVALID_OBESERVE[]           = "Invalid Observe Param";
+        static const char NO_MEMORY[]                  = "No Memory";
+        static const char COMM_ERROR[]                 = "Communication Error";
+        static const char NOT_IMPL[]                   = "Not Implemented";
+        static const char NOT_FOUND[]                  = "Resource Not Found";
+        static const char RESOURCE_ERROR[]             = "Resource Error";
+        static const char SLOW_RESOURCE[]              = "Slow Resource";
+        static const char NO_OBSERVERS[]               = "No Observers";
+        static const char OBSV_NO_FOUND[]              = "Stack observer not found";
+        static const char OBSV_NOT_ADDED[]             = "Stack observer not added";
+        static const char OBSV_NOT_REMOVED[]           = "Stack observer not removed";
+        static const char STACK_RESOURCE_DELETED[]     = "The specified resource has been deleted";
+        static const char PRESENCE_STOPPED[]           = "Stack presence stopped";
+        static const char PRESENCE_TIMEOUT[]           = "Stack presence timed out";
+        static const char PRESENCE_NOT_HANDLED[]       = "Stack presence should not be handled";
+        static const char INVALID_OPTION[]             = "Invalid option";
+        static const char GENERAL_FAULT[]              = "General Fault";
+        static const char MALFORMED_STACK_RESPONSE[]   = "Response from OC_STACK is malformed";
+        static const char VIRTUAL_DO_NOT_HANDLE[]      = "Virtual Do Not Handle";
+        static const char PERSISTENT_BUFFER_REQUIRED[] = "Persistent response buffer required";
+        static const char STACK_CONTINUE[]             = "Stack continue";
+        static const char INVALID_REQUEST_HANDLE[]     = "Invalid request handle";
+        static const char UNKNOWN_ERROR[]              = "Unknown Error";
+        static const char INVALID_REPRESENTATION[]     = "Invalid Payload JSON";
+        static const char INVALID_JSON_TYPE[]          = "Unrecognized JSON Type ";
+        static const char INVALID_JSON_NUMERIC[]       = "Unrecognized JSON Numeric ";
+        static const char INVALID_JSON_ARRAY_DEPTH[]   = "Max JSON Array Depth exceeded";
+        static const char INVALID_JSON_TYPE_TAG[]      = "Invalid JSON Type Tag";
+        static const char INVALID_ATTRIBUTE[]          = "Invalid Attribute: ";
     }
 
     namespace Error
     {
-        static const std::string INVALID_IP                 = "Invalid IP";
+        static const char INVALID_IP[]                 = "Invalid IP";
     }
 
     namespace PlatformCommands
     {
-        static const std::string GET                        = "GET";
-        static const std::string PUT                        = "PUT";
-        static const std::string POST                       = "POST";
-        static const std::string DELETE                     = "DELETE";
+        static const std::string GET                   = "GET";
+        static const std::string PUT                   = "PUT";
+        static const std::string POST                  = "POST";
+        static const std::string DELETE                = "DELETE";
     }
 
     namespace Key
     {
-        static const std::string OCKEY                      = "oc";
-        static const std::string URIKEY                     = "href";
-        static const std::string OBSERVABLEKEY              = "obs";
-        static const std::string RESOURCETYPESKEY           = "rt";
-        static const std::string INTERFACESKEY              = "if";
-        static const std::string PROPERTYKEY                = "prop";
-        static const std::string REPKEY                     = "rep";
-        static const std::string SECUREKEY                  = "sec";
-        static const std::string PORTKEY                    = "port";
+        static const std::string OCKEY                 = "oc";
+        static const std::string URIKEY                = "href";
+        static const std::string OBSERVABLEKEY         = "obs";
+        static const std::string RESOURCETYPESKEY      = "rt";
+        static const std::string INTERFACESKEY         = "if";
+        static const std::string PROPERTYKEY           = "prop";
+        static const std::string REPKEY                = "rep";
+        static const std::string SECUREKEY             = "sec";
+        static const std::string PORTKEY               = "port";
   }
 
 }