diff options
author | Andrei Karas <akaras@inbox.ru> | 2015-05-13 20:59:09 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2015-05-13 20:59:45 +0300 |
commit | 1a6a12a8b3310400a103e0048b7ed747fc479398 (patch) | |
tree | ad969a9b12bb65e8072e5b2937f9d5c41a559841 | |
parent | 0984b817b1bfed9b8b475f7c79d1627e5037e914 (diff) | |
download | plus-1a6a12a8b3310400a103e0048b7ed747fc479398.tar.gz plus-1a6a12a8b3310400a103e0048b7ed747fc479398.tar.bz2 plus-1a6a12a8b3310400a103e0048b7ed747fc479398.tar.xz plus-1a6a12a8b3310400a103e0048b7ed747fc479398.zip |
Add initial support for custom stack class.
In some tests it 3 times faster in push than std::stack.
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/Makefile.am | 4 | ||||
-rw-r--r-- | src/resources/mstack.h | 104 | ||||
-rw-r--r-- | src/resources/mstack_unittest.cc | 165 | ||||
-rw-r--r-- | src/test/testlauncher.cpp | 89 | ||||
-rw-r--r-- | src/test/testlauncher.h | 2 |
6 files changed, 364 insertions, 1 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index a91fe48cb..1ce5f211b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -651,6 +651,7 @@ SET(SRCS resources/mapreader.h resources/modinfo.cpp resources/modinfo.h + resources/mstack.h resources/notificationinfo.h resources/notifications.h enums/resources/notifyflags.h diff --git a/src/Makefile.am b/src/Makefile.am index 6f2729a3e..16797e1a9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -787,6 +787,7 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ resources/mapreader.h \ resources/modinfo.cpp \ resources/modinfo.h \ + resources/mstack.h \ resources/notificationinfo.h \ resources/notifications.h \ enums/resources/notifyflags.h \ @@ -1493,7 +1494,8 @@ manaplus_SOURCES += \ utils/files_unittest.cc \ utils/stringutils_unittest.cc \ utils/xmlutils_unittest.cc \ - resources/dye_unittest.cc + resources/dye_unittest.cc \ + resources/mstack_unittest.cc endif EXTRA_DIST = CMakeLists.txt \ diff --git a/src/resources/mstack.h b/src/resources/mstack.h new file mode 100644 index 000000000..ba1a58eb2 --- /dev/null +++ b/src/resources/mstack.h @@ -0,0 +1,104 @@ +/* + * The ManaPlus Client + * Copyright (C) 2015 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef RESOURCES_MSTACK_H +#define RESOURCES_MSTACK_H + +#include "logger.h" + +#include "localconsts.h" + +template<typename T> +struct MStack final +{ + MStack(const size_t maxSize) : + mStack(new T[maxSize]), + mMaxSize(maxSize), + mPointer(mStack - 1), + mStartPointer(mStack - 1), + mEndPointer(mStack + maxSize - 1) + { + } + + ~MStack() + { + delete [] mStack; + } + + T &push() + { + if (mPointer == mEndPointer) + { + logger->log("error: max stack size reached"); + return *mPointer; + } + return *(++mPointer); + } + + T &getPop() + { + if (mPointer == mStartPointer) + logger->log("error: minimal stack size reached"); + else + mPointer --; + return *mPointer; + } + + const T &getPopConst() + { + if (mPointer == mStartPointer) + logger->log("error: minimal stack size reached"); + else + mPointer --; + return *mPointer; + } + + void pop() + { + if (mPointer == mStartPointer) + logger->log("error: minimal stack size reached"); + else + mPointer --; + } + + T &top() + { + return *mPointer; + } + + const T &topConst() const + { + return *mPointer; + } + + void clear() + { + mPointer = mStack - 1; + } + + T *mStack; + + size_t mMaxSize; + T *mPointer; + const T *const mStartPointer; + const T *const mEndPointer; +}; + +#endif // RESOURCES_MSTACK_H diff --git a/src/resources/mstack_unittest.cc b/src/resources/mstack_unittest.cc new file mode 100644 index 000000000..6146f28e5 --- /dev/null +++ b/src/resources/mstack_unittest.cc @@ -0,0 +1,165 @@ +/* + * The ManaPlus Client + * Copyright (C) 2015 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "gui/cliprect.h" + +#include "resources/mstack.h" + +#include "gtest/gtest.h" + +#include "debug.h" + +TEST(mstack, push1) +{ + MStack<ClipRect> stack(10); + EXPECT_EQ(-1, stack.mPointer - stack.mStack); + EXPECT_EQ(0, stack.mStack[0].xOffset); + + ClipRect &val1 = stack.push(); + val1.xOffset = 10; + ClipRect &val2 = stack.top(); + EXPECT_EQ(0, stack.mPointer - stack.mStack); + EXPECT_EQ(10, val2.xOffset); + EXPECT_EQ(10, stack.mStack[0].xOffset); + EXPECT_EQ(0, stack.mStack[1].xOffset); + + val2.yOffset = 2; + EXPECT_EQ(2, val1.yOffset); +} + +TEST(mstack, push2) +{ + MStack<ClipRect> stack(10); + ClipRect &val1 = stack.push(); + val1.xOffset = 10; + const ClipRect &val2 = stack.top(); + EXPECT_EQ(10, val2.xOffset); + EXPECT_EQ(10, stack.mStack[0].xOffset); + + val1.yOffset = 2; + EXPECT_EQ(2, val2.yOffset); + EXPECT_EQ(2, stack.mStack[0].yOffset); +} + +TEST(mstack, push3) +{ + MStack<ClipRect> stack(10); + ClipRect &val1 = stack.push(); + val1.xOffset = 10; + val1 = stack.top(); + EXPECT_EQ(10, val1.xOffset); + EXPECT_EQ(10, stack.mStack[0].xOffset); +} + +TEST(mstack, push4) +{ + MStack<ClipRect> stack(10); + ClipRect &val1 = stack.push(); + val1.xOffset = 10; + EXPECT_EQ(10, val1.xOffset); + EXPECT_EQ(10, stack.mStack[0].xOffset); + EXPECT_EQ(0, stack.mStack[1].xOffset); + EXPECT_EQ(0, stack.mStack[2].xOffset); + + ClipRect &val2 = stack.push(); + val2.xOffset = 20; + EXPECT_EQ(20, val2.xOffset); + EXPECT_EQ(10, stack.mStack[0].xOffset); + EXPECT_EQ(20, stack.mStack[1].xOffset); + EXPECT_EQ(0, stack.mStack[2].xOffset); + + ClipRect &val3 = stack.push(); + val3.xOffset = 30; + EXPECT_EQ(30, val3.xOffset); + EXPECT_EQ(10, stack.mStack[0].xOffset); + EXPECT_EQ(20, stack.mStack[1].xOffset); + EXPECT_EQ(30, stack.mStack[2].xOffset); +} + +TEST(mstack, pop1) +{ + MStack<ClipRect> stack(10); + ClipRect &val1 = stack.push(); + val1.xOffset = 10; + EXPECT_EQ(10, stack.mStack[0].xOffset); + + stack.pop(); + EXPECT_EQ(-1, stack.mPointer - stack.mStack); +} + +TEST(mstack, pop2) +{ + MStack<ClipRect> stack(10); + ClipRect &val1 = stack.push(); + EXPECT_EQ(0, stack.mPointer - stack.mStack); + + val1.xOffset = 10; + EXPECT_EQ(10, stack.mStack[0].xOffset); + + ClipRect &val2 = stack.push(); + EXPECT_EQ(1, stack.mPointer - stack.mStack); + + val2.xOffset = 20; + EXPECT_EQ(10, stack.mStack[0].xOffset); + EXPECT_EQ(20, stack.mStack[1].xOffset); + + stack.pop(); + EXPECT_EQ(0, stack.mPointer - stack.mStack); + EXPECT_EQ(10, stack.mStack[0].xOffset); + EXPECT_EQ(20, stack.mStack[1].xOffset); + + ClipRect &val3 = stack.top(); + EXPECT_EQ(0, stack.mPointer - stack.mStack); + EXPECT_EQ(10, val1.xOffset); + EXPECT_EQ(20, val2.xOffset); + EXPECT_EQ(10, val3.xOffset); + EXPECT_EQ(10, stack.mStack[0].xOffset); + EXPECT_EQ(20, stack.mStack[1].xOffset); + EXPECT_EQ(0, stack.mStack[2].xOffset); +} + +TEST(mstack, clear1) +{ + MStack<ClipRect> stack(10); + EXPECT_EQ(-1, stack.mPointer - stack.mStack); + EXPECT_EQ(0, stack.mStack[0].xOffset); + + ClipRect &val1 = stack.push(); + val1.xOffset = 10; + + stack.clear(); + EXPECT_EQ(-1, stack.mPointer - stack.mStack); + EXPECT_EQ(10, stack.mStack[0].xOffset); + EXPECT_EQ(0, stack.mStack[1].xOffset); +} + +TEST(mstack, getpop1) +{ + MStack<ClipRect> stack(10); + ClipRect &val1 = stack.push(); + val1.xOffset = 10; + EXPECT_EQ(10, stack.mStack[0].xOffset); + EXPECT_EQ(10, val1.xOffset); + + ClipRect &val2 = stack.getPop(); + EXPECT_EQ(-1, stack.mPointer - stack.mStack); + EXPECT_EQ(10, stack.mStack[0].xOffset); + EXPECT_EQ(10, val2.xOffset); +} diff --git a/src/test/testlauncher.cpp b/src/test/testlauncher.cpp index 244f8b8fd..ffe16e8e7 100644 --- a/src/test/testlauncher.cpp +++ b/src/test/testlauncher.cpp @@ -30,6 +30,8 @@ #include "gui/skin.h" #include "gui/theme.h" +#include "gui/cliprect.h" + #include "gui/fonts/font.h" #include "utils/physfscheckutils.h" @@ -39,6 +41,7 @@ #include "resources/dyepalette.h" #include "resources/image.h" #include "resources/imagewriter.h" +#include "resources/mstack.h" #include "resources/openglimagehelper.h" #include "resources/surfaceimagehelper.h" #include "resources/wallpaper.h" @@ -97,6 +100,8 @@ int TestLauncher::exec() return testFps3(); else if (mTest == "105") return testDyeSpeed(); + else if (mTest == "106") + return testStackSpeed(); return -1; } @@ -486,6 +491,90 @@ int TestLauncher::testDyeSpeed() return 0; } +int TestLauncher::testStackSpeed() +{ + const int sz = 100000; + const int k = 100; + const int sz2 = sz * k; + + std::stack<ClipRect> stack1; + MStack<ClipRect> stack2(sz2); + timespec time1; + timespec time2; + +#if defined __linux__ || defined __linux + + for (int d = 0; d < 100; d ++) + { + for (int f = 0; f < sz; f ++) + { + ClipRect rect; + rect.xOffset = f; + rect.yOffset = f; + stack1.push(rect); + } + } + while (!stack1.empty()) + stack1.pop(); + + clock_gettime(CLOCK_MONOTONIC, &time1); + + for (int d = 0; d < 100; d ++) + { + for (int f = 0; f < sz; f ++) + { + ClipRect rect; + rect.xOffset = f; + rect.yOffset = f; + stack1.push(rect); + } + } + + clock_gettime(CLOCK_MONOTONIC, &time2); + long diff = ((static_cast<long int>(time2.tv_sec) * 1000000000L + + static_cast<long int>(time2.tv_nsec)) / 1) - + ((static_cast<long int>(time1.tv_sec) * 1000000000L + + static_cast<long int>(time1.tv_nsec)) / 1); + printf("debug: %d\n", stack1.top().xOffset); + printf("stl time: %ld\n", diff); + + + + for (int d = 0; d < 100; d ++) + { + for (int f = 0; f < sz; f ++) + { + ClipRect &rect = stack2.push(); + rect.xOffset = f; + rect.yOffset = f; + } + } + stack2.clear(); + + clock_gettime(CLOCK_MONOTONIC, &time1); + + for (int d = 0; d < 100; d ++) + { + for (int f = 0; f < sz; f ++) + { + ClipRect &rect = stack2.push(); + rect.xOffset = f; + rect.yOffset = f; + } + } + + clock_gettime(CLOCK_MONOTONIC, &time2); + diff = ((static_cast<long int>(time2.tv_sec) * 1000000000L + + static_cast<long int>(time2.tv_nsec)) / 1) - + ((static_cast<long int>(time1.tv_sec) * 1000000000L + + static_cast<long int>(time1.tv_nsec)) / 1); + printf("debug: %d\n", stack2.top().xOffset); + printf("my time: %ld\n", diff); + +#endif + return 0; +} + int TestLauncher::testDraw() { Image *img[3]; diff --git a/src/test/testlauncher.h b/src/test/testlauncher.h index abb84483b..d82e48f3f 100644 --- a/src/test/testlauncher.h +++ b/src/test/testlauncher.h @@ -72,6 +72,8 @@ class TestLauncher final int testDyeSpeed(); + int testStackSpeed(); + private: std::string mTest; |