From e31ed71006a1a66e0a2ad15b54cced5836497286 Mon Sep 17 00:00:00 2001 From: ThePhD Date: Fri, 19 Aug 2016 21:08:03 -0400 Subject: [PATCH] Add hinting optimization to arrays --- Optional | 2 +- single/sol/sol.hpp | 33 ++++++++++++++++++++++++++------- sol/optional.hpp | 2 +- sol/stack_core.hpp | 15 +++++++++++++++ sol/stack_get.hpp | 8 +++++++- sol/stack_push.hpp | 1 + 6 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Optional b/Optional index c386acbd..45112c08 160000 --- a/Optional +++ b/Optional @@ -1 +1 @@ -Subproject commit c386acbdf2d6ae368de7cba55801b06d362bdaac +Subproject commit 45112c085a4ca75b5591dde3bf46faf4969c3026 diff --git a/single/sol/sol.hpp b/single/sol/sol.hpp index f0ba01c1..f9b5eb9c 100644 --- a/single/sol/sol.hpp +++ b/single/sol/sol.hpp @@ -20,8 +20,8 @@ // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // This file was generated with a script. -// Generated 2016-08-19 03:51:21.744027 UTC -// This header was generated with sol v2.11.4 (revision c8ea9f7) +// Generated 2016-08-20 01:06:43.957511 UTC +// This header was generated with sol v2.11.4 (revision 1ae78e1) // https://github.com/ThePhD/sol2 #ifndef SOL_SINGLE_INCLUDE_HPP @@ -736,8 +736,6 @@ namespace sol { // beginning of sol/optional.hpp -#if defined(SOL_USE_BOOST) -#include // beginning of sol/in_place.hpp namespace sol { @@ -767,6 +765,8 @@ namespace sol { // end of sol/in_place.hpp +#if defined(SOL_USE_BOOST) +#include #else // beginning of Optional/optional.hpp @@ -3837,6 +3837,8 @@ namespace sol { // end of sol/tie.hpp +#include + namespace sol { namespace detail { struct as_reference_tag {}; @@ -3859,6 +3861,19 @@ namespace sol { (dx)(memory); return 0; } + + template + void reserve(T&, std::size_t) {} + + template + void reserve(std::vector& arr, std::size_t hint) { + arr.reserve(hint); + } + + template + void reserve(std::basic_string& arr, std::size_t hint) { + arr.reserve(hint); + } } // detail namespace stack { @@ -4842,8 +4857,13 @@ namespace sol { typedef typename T::value_type V; tracking.use(1); - T arr; index = lua_absindex(L, index); + T arr; + get_field(L, static_cast(-1), index); + optional sizehint = pop>(L); + if (sizehint) { + detail::reserve(arr, *sizehint); + } #if SOL_LUA_VERSION >= 503 // This method is HIGHLY performant over regular table iteration thanks to the Lua API changes in 5.3 for (lua_Integer i = 0; ; ++i, lua_pop(L, 1)) { @@ -5606,6 +5626,7 @@ namespace sol { for (auto&& i : cont) { set_field(L, index++, i, tableindex); } + set_field(L, -1, cont.size()); return 1; } }; @@ -9701,8 +9722,6 @@ namespace sol { // beginning of sol/simple_usertype_metatable.hpp -#include - namespace sol { struct simple_tag {} const simple{}; diff --git a/sol/optional.hpp b/sol/optional.hpp index ee88f129..823bdc3d 100644 --- a/sol/optional.hpp +++ b/sol/optional.hpp @@ -22,9 +22,9 @@ #ifndef SOL_OPTIONAL_HPP #define SOL_OPTIONAL_HPP +#include "in_place.hpp" #if defined(SOL_USE_BOOST) #include -#include "../sol/in_place.hpp" #else #include "../Optional/optional.hpp" #endif // Boost vs. Better optional diff --git a/sol/stack_core.hpp b/sol/stack_core.hpp index 8a8606ad..336a96d7 100644 --- a/sol/stack_core.hpp +++ b/sol/stack_core.hpp @@ -29,6 +29,8 @@ #include "tuple.hpp" #include "traits.hpp" #include "tie.hpp" +#include +#include namespace sol { namespace detail { @@ -52,6 +54,19 @@ namespace sol { (dx)(memory); return 0; } + + template + void reserve(T&, std::size_t) {} + + template + void reserve(std::vector& arr, std::size_t hint) { + arr.reserve(hint); + } + + template + void reserve(std::basic_string& arr, std::size_t hint) { + arr.reserve(hint); + } } // detail namespace stack { diff --git a/sol/stack_get.hpp b/sol/stack_get.hpp index fa34b810..dc7363eb 100644 --- a/sol/stack_get.hpp +++ b/sol/stack_get.hpp @@ -83,8 +83,14 @@ namespace sol { typedef typename T::value_type V; tracking.use(1); - T arr; + index = lua_absindex(L, index); + T arr; + get_field(L, static_cast(-1), index); + optional sizehint = pop>(L); + if (sizehint) { + detail::reserve(arr, *sizehint); + } #if SOL_LUA_VERSION >= 503 // This method is HIGHLY performant over regular table iteration thanks to the Lua API changes in 5.3 for (lua_Integer i = 0; ; ++i, lua_pop(L, 1)) { diff --git a/sol/stack_push.hpp b/sol/stack_push.hpp index a35318d6..68c21f4e 100644 --- a/sol/stack_push.hpp +++ b/sol/stack_push.hpp @@ -168,6 +168,7 @@ namespace sol { for (auto&& i : cont) { set_field(L, index++, i, tableindex); } + set_field(L, -1, cont.size()); return 1; } };