Skip to content

Commit 289db61

Browse files
Introduce constexpr for the XBeeAddress64 constructors
This allows the compiler to better optimize (global) XBeeAdress64 instances. With the examples sketches, this saves between 44 and 114 bytes of program space, compiling for the Uno. Even sketches that do not directly use any addresses shrink, because the initialization of RemoteAtCommandRequest::broadcastAddress64 can be simplified. This feature requires C++11 to be enabled. This isn't currently the case (with Arduino 1.6.4), but is expected to be enabled in a future release. A preprocessor check is used to ensure that the code compiles with and without C++11.
1 parent 50ef472 commit 289db61

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

XBee.h

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,20 @@
142142
#define PACKET_EXCEEDS_BYTE_ARRAY_LENGTH 2
143143
#define UNEXPECTED_START_BYTE 3
144144

145+
/**
146+
* C++11 introduced the constexpr as a hint to the compiler that things
147+
* can be evaluated at compiletime. This can help to remove
148+
* startup code for global objects, or otherwise help the compiler to
149+
* optimize. Since the keyword is introduced in C++11, but supporting
150+
* older compilers is a matter of removing the keyword, we use a macro
151+
* for this.
152+
*/
153+
#if __cplusplus >= 201103L
154+
#define CONSTEXPR constexpr
155+
#else
156+
#define CONSTEXPR
157+
#endif
158+
145159
/**
146160
* The super class of all XBee responses (RX packets)
147161
* Users should never attempt to create an instance of this class; instead
@@ -287,7 +301,7 @@ class XBeeResponse {
287301

288302
class XBeeAddress {
289303
public:
290-
XBeeAddress() {};
304+
CONSTEXPR XBeeAddress() {};
291305
};
292306

293307
/**
@@ -299,9 +313,9 @@ class XBeeAddress {
299313
*/
300314
class XBeeAddress64 : public XBeeAddress {
301315
public:
302-
XBeeAddress64(uint64_t addr) : _msb(addr >> 32), _lsb(addr) {}
303-
XBeeAddress64(uint32_t msb, uint32_t lsb) : _msb(msb), _lsb(lsb) {}
304-
XBeeAddress64() {}
316+
CONSTEXPR XBeeAddress64(uint64_t addr) : _msb(addr >> 32), _lsb(addr) {}
317+
CONSTEXPR XBeeAddress64(uint32_t msb, uint32_t lsb) : _msb(msb), _lsb(lsb) {}
318+
CONSTEXPR XBeeAddress64() : _msb(0), _lsb(0) {}
305319
uint32_t getMsb() {return _msb;}
306320
uint32_t getLsb() {return _lsb;}
307321
uint64_t get() {return (static_cast<uint64_t>(_msb) << 32) | _lsb;}

0 commit comments

Comments
 (0)