From 8d4f5a9f40fe8c4b8dc116369ac01be3096fe383 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Sun, 3 Nov 2013 07:18:27 +0000 Subject: [PATCH] i know its crazy, but attempt performance gains by avoiding use of int64 as much as possible. --- source/shared_lib/include/graphics/vec.h | 11 +++++++++- source/shared_lib/include/util/conversion.h | 4 ++-- source/shared_lib/sources/util/conversion.cpp | 21 +++++++++++++++---- source/shared_lib/sources/xml/xml_parser.cpp | 10 ++++----- 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/source/shared_lib/include/graphics/vec.h b/source/shared_lib/include/graphics/vec.h index 2b3e1a86..de972bfd 100644 --- a/source/shared_lib/include/graphics/vec.h +++ b/source/shared_lib/include/graphics/vec.h @@ -20,6 +20,7 @@ #include #include #include +#include #include "byte_order.h" //#include //using namespace std::tr1; @@ -59,8 +60,16 @@ inline T truncateDecimal(const T &value, int precision=6) { precNum = std::pow((T)10,(T)precision); } - int64 resultInt = (T)value * (T)precNum; + // See if we can avoid using an int64 for speed + static int MAX_INT_VALUE = std::numeric_limits::max(); + if((T)value * (T)precNum <= MAX_INT_VALUE) { + int resultInt = (T)value * (T)precNum; + T result = (T)resultInt / precNum; + return result; + } + // Must use an int64 since the result is large + int64 resultInt = (T)value * (T)precNum; T result = (long double)resultInt / precNum; return result; } diff --git a/source/shared_lib/include/util/conversion.h b/source/shared_lib/include/util/conversion.h index 956d464a..e2b420a5 100644 --- a/source/shared_lib/include/util/conversion.h +++ b/source/shared_lib/include/util/conversion.h @@ -33,8 +33,8 @@ bool strToUInt(const string &s, uint32 *i); bool strToFloat(const string &s, float *f); string boolToStr(bool b); -string uIntToStr(uint32 i); -string intToStr(int64 i); +string uIntToStr(const uint64 &value); +string intToStr(const int64 &value); string intToHex(int i); string floatToStr(float f,int precsion=2); string doubleToStr(double f,int precsion=2); diff --git a/source/shared_lib/sources/util/conversion.cpp b/source/shared_lib/sources/util/conversion.cpp index 05cffbcb..fb4ca929 100644 --- a/source/shared_lib/sources/util/conversion.cpp +++ b/source/shared_lib/sources/util/conversion.cpp @@ -19,6 +19,7 @@ #include #include #include "platform_util.h" +#include #include "leak_dumper.h" using namespace std; @@ -132,15 +133,27 @@ string boolToStr(bool b) { } } -string intToStr(int64 i) { +string intToStr(const int64 &value) { char str[strSize]=""; - snprintf(str, strSize-1, "%lld", (long long int)i); + static int MAX_INT_VALUE = std::numeric_limits::max(); + if(value <= MAX_INT_VALUE) { + snprintf(str, strSize-1, "%d", (int)value); + } + else { + snprintf(str, strSize-1, "%lld", (long long int)value); + } return (str[0] != '\0' ? str : ""); } -string uIntToStr(uint32 i) { +string uIntToStr(const uint64 &value) { char str[strSize]=""; - snprintf(str, strSize-1, "%u", i); + static unsigned int MAX_UNSIGNED_INT_VALUE = std::numeric_limits::max(); + if(value <= MAX_UNSIGNED_INT_VALUE) { + snprintf(str, strSize-1, "%u", (int)value); + } + else { + snprintf(str, strSize-1, "%llu", (long long unsigned int)value); + } return (str[0] != '\0' ? str : ""); } diff --git a/source/shared_lib/sources/xml/xml_parser.cpp b/source/shared_lib/sources/xml/xml_parser.cpp index 133c4e3d..9067e80d 100644 --- a/source/shared_lib/sources/xml/xml_parser.cpp +++ b/source/shared_lib/sources/xml/xml_parser.cpp @@ -502,7 +502,7 @@ XmlTree::XmlTree(xml_engine_parser_type engine_type) { break; default: - throw megaglest_runtime_error("Invalid XML parser engine: " + intToStr(engine_type)); + throw megaglest_runtime_error("Invalid XML parser engine: " + intToStr((int)engine_type)); } this->engine_type = engine_type; @@ -689,7 +689,7 @@ XmlNode::~XmlNode() { XmlAttribute *XmlNode::getAttribute(unsigned int i) const { if(i >= attributes.size()) { - throw megaglest_runtime_error(getName()+" node doesn't have "+intToStr(i)+" attributes"); + throw megaglest_runtime_error(getName()+" node doesn't have " + uIntToStr(i) + " attributes"); } return attributes[i]; } @@ -733,7 +733,7 @@ int XmlNode::clearChild(const string &childName) { XmlNode *XmlNode::getChild(unsigned int i) const { assert(!superNode); if(i >= children.size()) { - throw megaglest_runtime_error("\"" + getName()+"\" node doesn't have "+intToStr(i+1)+" children"); + throw megaglest_runtime_error("\"" + getName()+"\" node doesn't have "+ uIntToStr(i+1) + " children"); } return children[i]; } @@ -754,7 +754,7 @@ XmlNode *XmlNode::getChild(const string &childName, unsigned int i) const { return superNode->getChild(childName,i); } if(i >= children.size()) { - throw megaglest_runtime_error("\"" + name + "\" node doesn't have "+intToStr(i+1)+" children named \"" + childName + "\"\n\nTree: "+getTreeString()); + throw megaglest_runtime_error("\"" + name + "\" node doesn't have " + uIntToStr(i+1) +" children named \"" + childName + "\"\n\nTree: "+getTreeString()); } int count= 0; @@ -767,7 +767,7 @@ XmlNode *XmlNode::getChild(const string &childName, unsigned int i) const { } } - throw megaglest_runtime_error("Node \""+getName()+"\" doesn't have "+intToStr(i+1)+" children named \""+childName+"\"\n\nTree: "+getTreeString()); + throw megaglest_runtime_error("Node \""+getName()+"\" doesn't have " + uIntToStr(i+1) + " children named \""+childName+"\"\n\nTree: "+getTreeString()); } bool XmlNode::hasChildNoSuper(const string &childName) const {