Added minimum copy/reference threshold.

Even if ref_size is given on vrefbuffer's constructor, the minimum size of vrefbuffer::ref_buffer is 10.
Because int64, uint64, and double's msgpack expression size equals 9, and those buffer is allocated on the stack internally.
This commit is contained in:
Takatoshi Kondo 2014-08-08 14:28:03 +09:00
parent dee68403df
commit 0201c21b9a
4 changed files with 163 additions and 28 deletions

View File

@ -40,8 +40,8 @@ struct iovec {
namespace msgpack { namespace msgpack {
namespace detail { namespace detail {
// int64, uint64, double
std::size_t const packer_max_buffer_size = 9;
} // detail } // detail
class vrefbuffer { class vrefbuffer {
@ -57,7 +57,8 @@ private:
public: public:
vrefbuffer(size_t ref_size = MSGPACK_VREFBUFFER_REF_SIZE, vrefbuffer(size_t ref_size = MSGPACK_VREFBUFFER_REF_SIZE,
size_t chunk_size = MSGPACK_VREFBUFFER_CHUNK_SIZE) size_t chunk_size = MSGPACK_VREFBUFFER_CHUNK_SIZE)
:m_ref_size(ref_size), m_chunk_size(chunk_size) :m_ref_size(std::max(ref_size, detail::packer_max_buffer_size + 1)),
m_chunk_size(chunk_size)
{ {
size_t nfirst = (sizeof(iovec) < 72/2) ? size_t nfirst = (sizeof(iovec) < 72/2) ?
72 / sizeof(iovec) : 8; 72 / sizeof(iovec) : 8;
@ -277,4 +278,3 @@ private:
} // namespace msgpack } // namespace msgpack
#endif /* msgpack/vrefbuffer.hpp */ #endif /* msgpack/vrefbuffer.hpp */

View File

@ -19,6 +19,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#define MSGPACK_PACKER_MAX_BUFFER_SIZE 9
struct msgpack_vrefbuffer_chunk { struct msgpack_vrefbuffer_chunk {
struct msgpack_vrefbuffer_chunk* next; struct msgpack_vrefbuffer_chunk* next;
/* data ... */ /* data ... */
@ -28,7 +30,9 @@ bool msgpack_vrefbuffer_init(msgpack_vrefbuffer* vbuf,
size_t ref_size, size_t chunk_size) size_t ref_size, size_t chunk_size)
{ {
vbuf->chunk_size = chunk_size; vbuf->chunk_size = chunk_size;
vbuf->ref_size = ref_size; vbuf->ref_size =
ref_size > MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ?
ref_size : MSGPACK_PACKER_MAX_BUFFER_SIZE + 1 ;
size_t nfirst = (sizeof(struct iovec) < 72/2) ? size_t nfirst = (sizeof(struct iovec) < 72/2) ?
72 / sizeof(struct iovec) : 8; 72 / sizeof(struct iovec) : 8;
@ -217,4 +221,3 @@ int msgpack_vrefbuffer_migrate(msgpack_vrefbuffer* vbuf, msgpack_vrefbuffer* to)
return 0; return 0;
} }

View File

@ -117,4 +117,3 @@ TEST(buffer, fbuffer_c)
EXPECT_EQ(EOF, fgetc(file)); EXPECT_EQ(EOF, fgetc(file));
fclose(file); fclose(file);
} }

View File

@ -1076,13 +1076,12 @@ TEST(MSGPACK_USER_DEFINED, simple_buffer_union_member)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
#define GEN_TEST_VREF(test_type) \ #define GEN_TEST_VREF(test_type, vbuf) \
do { \ do { \
vector<test_type> v; \ vector<test_type> v; \
v.push_back(0); \ v.push_back(0); \
for (unsigned int i = 0; i < v.size(); i++) { \ for (unsigned int i = 0; i < v.size(); i++) { \
test_type val1 = v[i]; \ test_type val1 = v[i]; \
msgpack::vrefbuffer vbuf; \
msgpack::pack(vbuf, val1); \ msgpack::pack(vbuf, val1); \
msgpack::sbuffer sbuf; \ msgpack::sbuffer sbuf; \
const struct iovec* cur = vbuf.vector(); \ const struct iovec* cur = vbuf.vector(); \
@ -1099,97 +1098,231 @@ TEST(MSGPACK_USER_DEFINED, simple_buffer_union_member)
TEST(MSGPACK, vrefbuffer_char) TEST(MSGPACK, vrefbuffer_char)
{ {
GEN_TEST_VREF(char); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(char, vbuf);
} }
TEST(MSGPACK, vrefbuffer_signed_char) TEST(MSGPACK, vrefbuffer_signed_char)
{ {
GEN_TEST_VREF(signed char); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(signed char, vbuf);
} }
TEST(MSGPACK, vrefbuffer_unsigned_char) TEST(MSGPACK, vrefbuffer_unsigned_char)
{ {
GEN_TEST_VREF(unsigned char); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(unsigned char, vbuf);
} }
TEST(MSGPACK, vrefbuffer_short) TEST(MSGPACK, vrefbuffer_short)
{ {
GEN_TEST_VREF(short); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(short, vbuf);
} }
TEST(MSGPACK, vrefbuffer_int) TEST(MSGPACK, vrefbuffer_int)
{ {
GEN_TEST_VREF(int); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(int, vbuf);
} }
TEST(MSGPACK, vrefbuffer_long) TEST(MSGPACK, vrefbuffer_long)
{ {
GEN_TEST_VREF(long); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(long, vbuf);
} }
TEST(MSGPACK, vrefbuffer_long_long) TEST(MSGPACK, vrefbuffer_long_long)
{ {
GEN_TEST_VREF(long long); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(long long, vbuf);
} }
TEST(MSGPACK, vrefbuffer_unsigned_short) TEST(MSGPACK, vrefbuffer_unsigned_short)
{ {
GEN_TEST_VREF(unsigned short); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(unsigned short, vbuf);
} }
TEST(MSGPACK, vrefbuffer_unsigned_int) TEST(MSGPACK, vrefbuffer_unsigned_int)
{ {
GEN_TEST_VREF(unsigned int); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(unsigned int, vbuf);
} }
TEST(MSGPACK, vrefbuffer_unsigned_long) TEST(MSGPACK, vrefbuffer_unsigned_long)
{ {
GEN_TEST_VREF(unsigned long); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(unsigned long, vbuf);
} }
TEST(MSGPACK, vrefbuffer_unsigned_long_long) TEST(MSGPACK, vrefbuffer_unsigned_long_long)
{ {
GEN_TEST_VREF(unsigned long long); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(unsigned long long, vbuf);
} }
TEST(MSGPACK, vrefbuffer_uint8) TEST(MSGPACK, vrefbuffer_uint8)
{ {
GEN_TEST_VREF(uint8_t); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(uint8_t, vbuf);
} }
TEST(MSGPACK, vrefbuffer_uint16) TEST(MSGPACK, vrefbuffer_uint16)
{ {
GEN_TEST_VREF(uint16_t); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(uint16_t, vbuf);
} }
TEST(MSGPACK, vrefbuffer_uint32) TEST(MSGPACK, vrefbuffer_uint32)
{ {
GEN_TEST_VREF(uint32_t); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(uint32_t, vbuf);
} }
TEST(MSGPACK, vrefbuffer_uint64) TEST(MSGPACK, vrefbuffer_uint64)
{ {
GEN_TEST_VREF(uint64_t); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(uint64_t, vbuf);
} }
TEST(MSGPACK, vrefbuffer_int8) TEST(MSGPACK, vrefbuffer_int8)
{ {
GEN_TEST_VREF(int8_t); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(int8_t, vbuf);
} }
TEST(MSGPACK, vrefbuffer_int16) TEST(MSGPACK, vrefbuffer_int16)
{ {
GEN_TEST_VREF(int16_t); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(int16_t, vbuf);
} }
TEST(MSGPACK, vrefbuffer_int32) TEST(MSGPACK, vrefbuffer_int32)
{ {
GEN_TEST_VREF(int32_t); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(int32_t, vbuf);
} }
TEST(MSGPACK, vrefbuffer_int64) TEST(MSGPACK, vrefbuffer_int64)
{ {
GEN_TEST_VREF(int64_t); msgpack::vrefbuffer vbuf;
GEN_TEST_VREF(int64_t, vbuf);
}
// small ref_size and chunk_size
TEST(MSGPACK, vrefbuffer_small_char)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(char, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_signed_char)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(signed char, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_unsigned_char)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(unsigned char, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_short)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(short, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_int)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(int, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_long)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(long, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_long_long)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(long long, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_unsigned_short)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(unsigned short, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_unsigned_int)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(unsigned int, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_unsigned_long)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(unsigned long, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_unsigned_long_long)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(unsigned long long, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_uint8)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(uint8_t, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_uint16)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(uint16_t, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_uint32)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(uint32_t, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_uint64)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(uint64_t, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_int8)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(int8_t, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_int16)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(int16_t, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_int32)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(int32_t, vbuf);
}
TEST(MSGPACK, vrefbuffer_small_int64)
{
msgpack::vrefbuffer vbuf(0, 0);
GEN_TEST_VREF(int64_t, vbuf);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------