From 86b47fa741408b061ab0bda784b8678bfd7dfa88 Mon Sep 17 00:00:00 2001 From: Akio Nakamura Date: Thu, 1 Feb 2018 19:34:50 +0900 Subject: speed up Unserialize_impl for prevector The unserializer for prevector uses resize() for reserve the area, but it's prefer to use reserve() because resize() have overhead to call its constructor many times. However, reserve() does not change the value of "_size" (a private member of prevector). This PR introduce resize_uninitialized() to prevector that similar to resize() but does not call constructor, and added elements are explicitly initialized in Unserialize_imple(). The changes are as follows: 1. prevector.h Add a public member function named 'resize_uninitialized'. This function processes like as resize() but does not call constructors. So added elemensts needs explicitly initialized after this returns. 2. serialize.h In the following two function: Unserialize_impl(Stream& is, prevector& v, const unsigned char&) Unserialize_impl(Stream& is, prevector& v, const V&) Calls resize_uninitialized() instead of resize() 3. test/prevector_tests.cpp Add a test for resize_uninitialized(). --- src/prevector.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/prevector.h') diff --git a/src/prevector.h b/src/prevector.h index 99e5751634..2f368a5c66 100644 --- a/src/prevector.h +++ b/src/prevector.h @@ -378,6 +378,21 @@ public: fill(ptr, first, last); } + inline void resize_uninitialized(size_type new_size) { + // resize_uninitialized changes the size of the prevector but does not initialize it. + // If size < new_size, the added elements must be initialized explicitly. + if (capacity() < new_size) { + change_capacity(new_size); + _size += new_size - size(); + return; + } + if (new_size < size()) { + erase(item_ptr(new_size), end()); + } else { + _size += new_size - size(); + } + } + iterator erase(iterator pos) { return erase(pos, pos + 1); } -- cgit v1.2.3