summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArseny Kapoulkine <arseny.kapoulkine@gmail.com>2015-09-20 00:24:07 -0700
committerArseny Kapoulkine <arseny.kapoulkine@gmail.com>2015-09-20 00:43:37 -0700
commit9c539f92ab9420bc94037681da3d1fce4878e01b (patch)
tree350b5feff333c74513c198cea288adcb74e495fb
parent5750b7cc198924d21fb92926f19c92ec791f0f26 (diff)
Eliminate redundant strlen calls during set_value/set
Since we use manual integer conversion we know the length of the string. This makes set_value(int) ~30% faster for 4-digit numbers.
-rw-r--r--src/pugixml.cpp35
1 files changed, 19 insertions, 16 deletions
diff --git a/src/pugixml.cpp b/src/pugixml.cpp
index 7b5dedc..da0e9e4 100644
--- a/src/pugixml.cpp
+++ b/src/pugixml.cpp
@@ -4541,25 +4541,24 @@ PUGI__NS_BEGIN
#endif
template <typename U>
- PUGI__FN char_t* integer_to_string(char_t (&buf)[64], U value, bool negative)
+ PUGI__FN char_t* integer_to_string(char_t* begin, char_t* end, U value, bool negative)
{
- char_t* end = buf + sizeof(buf) / sizeof(buf[0]) - 1;
+ char_t* result = end - 1;
U rest = negative ? 0 - value : value;
- *end-- = 0;
-
do
{
- *end-- = static_cast<char_t>('0' + (rest % 10));
+ *result-- = static_cast<char_t>('0' + (rest % 10));
rest /= 10;
}
while (rest);
- assert(end >= buf);
+ assert(result >= begin);
+ (void)begin;
- *end = '-';
+ *result = '-';
- return end + !negative;
+ return result + !negative;
}
// set value with conversion functions
@@ -4580,18 +4579,20 @@ PUGI__NS_BEGIN
PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, int value)
{
char_t buf[64];
- char_t* begin = integer_to_string<unsigned int>(buf, value, value < 0);
+ char_t* end = buf + sizeof(buf) / sizeof(buf[0]);
+ char_t* begin = integer_to_string<unsigned int>(buf, end, value, value < 0);
- return strcpy_insitu(dest, header, header_mask, begin, strlength(begin));
+ return strcpy_insitu(dest, header, header_mask, begin, end - begin);
}
template <typename String, typename Header>
PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, unsigned int value)
{
char_t buf[64];
- char_t* begin = integer_to_string<unsigned int>(buf, value, false);
+ char_t* end = buf + sizeof(buf) / sizeof(buf[0]);
+ char_t* begin = integer_to_string<unsigned int>(buf, end, value, false);
- return strcpy_insitu(dest, header, header_mask, begin, strlength(begin));
+ return strcpy_insitu(dest, header, header_mask, begin, end - begin);
}
template <typename String, typename Header>
@@ -4623,18 +4624,20 @@ PUGI__NS_BEGIN
PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, long long value)
{
char_t buf[64];
- char_t* begin = integer_to_string<unsigned long long>(buf, value, value < 0);
+ char_t* end = buf + sizeof(buf) / sizeof(buf[0]);
+ char_t* begin = integer_to_string<unsigned long long>(buf, end, value, value < 0);
- return strcpy_insitu(dest, header, header_mask, begin, strlength(begin));
+ return strcpy_insitu(dest, header, header_mask, begin, end - begin);
}
template <typename String, typename Header>
PUGI__FN bool set_value_convert(String& dest, Header& header, uintptr_t header_mask, unsigned long long value)
{
char_t buf[64];
- char_t* begin = integer_to_string<unsigned long long>(buf, value, false);
+ char_t* end = buf + sizeof(buf) / sizeof(buf[0]);
+ char_t* begin = integer_to_string<unsigned long long>(buf, end, value, false);
- return strcpy_insitu(dest, header, header_mask, begin, strlength(begin));
+ return strcpy_insitu(dest, header, header_mask, begin, end - begin);
}
#endif