Skip to content

Commit 33de35b

Browse files
committed
improvement dbuf pool's performance; add dbuf_pool sample
1 parent 7926910 commit 33de35b

File tree

12 files changed

+162
-12
lines changed

12 files changed

+162
-12
lines changed

lib_acl/include/stdlib/acl_dbuf_pool.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ typedef struct ACL_DBUF_POOL ACL_DBUF_POOL;
99

1010
/* public */
1111
ACL_API ACL_DBUF_POOL *acl_dbuf_pool_create(size_t block_size);
12-
ACL_API void acl_dbuf_pool_reset(ACL_DBUF_POOL *pool);
12+
ACL_API void acl_dbuf_pool_reset(ACL_DBUF_POOL *pool, size_t off);
1313
ACL_API void acl_dbuf_pool_destroy(ACL_DBUF_POOL *pool);
1414
ACL_API void *acl_dbuf_pool_alloc(ACL_DBUF_POOL *pool, size_t length);
1515
ACL_API void *acl_dbuf_pool_calloc(ACL_DBUF_POOL *pool, size_t length);

lib_acl/src/json/acl_json.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ void acl_json_reset(ACL_JSON *json)
443443
acl_dbuf_pool_destroy(json->dbuf);
444444
json->dbuf = acl_dbuf_pool_create(81920);
445445
#else
446-
acl_dbuf_pool_reset(json->dbuf);
446+
acl_dbuf_pool_reset(json->dbuf, 0);
447447
#endif
448448

449449
json->root = acl_json_node_alloc(json);

lib_acl/src/stdlib/memory/acl_dbuf_pool.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <stdio.h>
1111
#include "stdlib/acl_sys_patch.h"
1212
#include "stdlib/acl_mymalloc.h"
13+
#include "stdlib/acl_msg.h"
1314
#include "stdlib/acl_dbuf_pool.h"
1415

1516
#endif
@@ -88,7 +89,7 @@ void acl_dbuf_pool_destroy(ACL_DBUF_POOL *pool)
8889
#endif
8990
}
9091

91-
void acl_dbuf_pool_reset(ACL_DBUF_POOL *pool)
92+
void acl_dbuf_pool_reset(ACL_DBUF_POOL *pool, size_t off)
9293
{
9394
ACL_DBUF *iter = pool->head, *tmp;
9495

@@ -105,7 +106,13 @@ void acl_dbuf_pool_reset(ACL_DBUF_POOL *pool)
105106
}
106107
pool->head = (ACL_DBUF*) pool->buf_addr;
107108
pool->head->next = NULL;
108-
pool->head->ptr = pool->head->buf_addr;
109+
110+
if (pool->head->buf_addr + off >= (char*) pool + pool->block_size)
111+
acl_msg_fatal("%s(%d) off(%ld) too big, should < %ld",
112+
__FUNCTION__, __LINE__, (long) off, (char*) pool
113+
+ pool->block_size - pool->head->buf_addr);
114+
115+
pool->head->ptr = pool->head->buf_addr + off;
109116
}
110117

111118
static ACL_DBUF *acl_dbuf_alloc(ACL_DBUF_POOL *pool, size_t length)

lib_acl_cpp/changes.txt

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
------------------------------------------------------------------------
44
329) 2015.7.21
55
329.1) feature: db ģ��IJ�ѯ�� query ������ double/float ����
6+
329.2) perfomance: dbuf_pool �����Ҳ�������ڴ���ϣ��Ӷ��ٴμ��� malloc/free ����
67

78
328) 2015.7.19
89
328.1) bugfix: redis_command.cpp �е� run(redis_client_cluster*, size_t) ����

lib_acl_cpp/include/acl_cpp/stdlib/dbuf_pool.hpp

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ namespace acl
1414
class ACL_CPP_API dbuf_pool
1515
{
1616
public:
17-
dbuf_pool(size_t block_size = 8192);
17+
dbuf_pool();
1818
~dbuf_pool();
1919

20+
/**
21+
* 重载了 new/delete 操作符,在 new dbuf_pool 对象时,使之创建在内存池上,
22+
* 从而减少了 malloc/free 的次数
23+
*/
24+
void *operator new(size_t size);
25+
void operator delete(void* ptr);
26+
2027
/**
2128
* 重置内存池的状态以便于重复使用该内存池对象
2229
*/
@@ -55,6 +62,7 @@ class ACL_CPP_API dbuf_pool
5562

5663
private:
5764
ACL_DBUF_POOL* pool_;
65+
size_t mysize_;
5866
};
5967

6068
} // namespace acl

lib_acl_cpp/samples/dbuf/Makefile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
UTIL = $(wildcard ../*.cpp)
2+
PROG = dbuf
3+
include ../Makefile.in
4+
CFLAGS += -I../

lib_acl_cpp/samples/dbuf/main.cpp

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// xml.cpp : 定义控制台应用程序的入口点。
2+
//
3+
#include "stdafx.h"
4+
#include <sys/time.h>
5+
#include "util.h"
6+
7+
class test_buf
8+
{
9+
public:
10+
test_buf(acl::dbuf_pool* pool)
11+
: pool_(pool)
12+
{
13+
}
14+
15+
~test_buf()
16+
{
17+
}
18+
19+
void *operator new(size_t size, acl::dbuf_pool* pool)
20+
{
21+
return pool->dbuf_alloc(size);
22+
}
23+
24+
static void operator delete(void*)
25+
{
26+
}
27+
28+
private:
29+
acl::dbuf_pool* pool_;
30+
};
31+
32+
static void usage(const char* procname)
33+
{
34+
printf("usage: %s -h [help] -n loop -c count -p [use memory pool]\r\n", procname);
35+
}
36+
37+
int main(int argc, char* argv[])
38+
{
39+
bool use_pool = false;
40+
int ch, n = 100, c = 10000;
41+
42+
while ((ch = getopt(argc, argv, "hpn:c:")) > 0)
43+
{
44+
switch (ch)
45+
{
46+
case 'h':
47+
usage(argv[0]);
48+
return 0;
49+
case 'p':
50+
use_pool = true;
51+
break;
52+
case 'n':
53+
n = atoi(optarg);
54+
break;
55+
case 'c':
56+
c = atoi(optarg);
57+
break;
58+
default:
59+
break;
60+
}
61+
}
62+
63+
if (use_pool)
64+
{
65+
acl::dbuf_pool* pool = new acl::dbuf_pool;
66+
for (int i = 0; i < n; i++)
67+
{
68+
for (int j = 0; j < c; j++)
69+
{
70+
test_buf* buf = new (pool) test_buf(pool);
71+
delete buf;
72+
}
73+
74+
pool->dbuf_reset();
75+
}
76+
delete pool;
77+
}
78+
else
79+
{
80+
for (int i = 0; i < n; i++)
81+
{
82+
for (int j = 0; j < c; j++)
83+
{
84+
test_buf* buf = (test_buf*) malloc(sizeof(test_buf));
85+
free(buf);
86+
}
87+
}
88+
}
89+
90+
printf("total: %d\r\n", n * c);
91+
92+
return 0;
93+
}

lib_acl_cpp/samples/dbuf/stdafx.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// stdafx.cpp : 只包括标准包含文件的源文件
2+
// xml.pch 将成为预编译头
3+
// stdafx.obj 将包含预编译类型信息
4+
5+
#include "stdafx.h"
6+
7+
// TODO: 在 STDAFX.H 中
8+
//引用任何所需的附加头文件,而不是在此文件中引用

lib_acl_cpp/samples/dbuf/stdafx.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// stdafx.h : 标准系统包含文件的包含文件,
2+
// 或是常用但不常更改的项目特定的包含文件
3+
//
4+
5+
#pragma once
6+
7+
//
8+
//#include <iostream>
9+
//#include <tchar.h>
10+
11+
// TODO: 在此处引用程序要求的附加头文件
12+
#include "acl_cpp/lib_acl.hpp"
13+
#include "lib_acl.h"

lib_acl_cpp/samples/dbuf/valgrind.sh

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
valgrind --tool=memcheck --leak-check=yes -v ./dbuf -p

lib_acl_cpp/src/redis/redis_command.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ redis_command::redis_command()
3232
, slice_res_(false)
3333
, result_(NULL)
3434
{
35-
pool_ = NEW dbuf_pool(128000);
35+
pool_ = new dbuf_pool();
3636
addr_[0] = 0;
3737
}
3838

@@ -54,7 +54,7 @@ redis_command::redis_command(redis_client* conn)
5454
, slice_res_(false)
5555
, result_(NULL)
5656
{
57-
pool_ = NEW dbuf_pool(128000);
57+
pool_ = new dbuf_pool();
5858
if (conn != NULL)
5959
set_client_addr(*conn);
6060
else
@@ -76,7 +76,7 @@ redis_command::redis_command(redis_client_cluster* cluster, size_t max_conns)
7676
, slice_res_(false)
7777
, result_(NULL)
7878
{
79-
pool_ = NEW dbuf_pool(128000);
79+
pool_ = new dbuf_pool();
8080
addr_[0] = 0;
8181

8282
if (cluster != NULL)

lib_acl_cpp/src/stdlib/dbuf_pool.cpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,32 @@
44
namespace acl
55
{
66

7-
dbuf_pool::dbuf_pool(size_t block_size /* = 8192 */)
7+
dbuf_pool::dbuf_pool()
88
{
9-
pool_ = acl_dbuf_pool_create(block_size);
109
}
1110

1211
dbuf_pool::~dbuf_pool()
1312
{
14-
acl_dbuf_pool_destroy(pool_);
13+
}
14+
15+
void *dbuf_pool::operator new(size_t size)
16+
{
17+
ACL_DBUF_POOL* pool = acl_dbuf_pool_create(8192);
18+
dbuf_pool* dbuf = (dbuf_pool*) acl_dbuf_pool_alloc(pool, size);
19+
dbuf->pool_ = pool;
20+
dbuf->mysize_ = size;
21+
return dbuf;
22+
}
23+
24+
void dbuf_pool::operator delete(void* ptr)
25+
{
26+
dbuf_pool* dbuf = (dbuf_pool*) ptr;
27+
acl_dbuf_pool_destroy(dbuf->pool_);
1528
}
1629

1730
void dbuf_pool::dbuf_reset()
1831
{
19-
acl_dbuf_pool_reset(pool_);
32+
acl_dbuf_pool_reset(pool_, mysize_);
2033
}
2134

2235
void* dbuf_pool::dbuf_alloc(size_t len)

0 commit comments

Comments
 (0)