Skip to content

Commit fb67b9b

Browse files
authoredNov 7, 2024··
add optimize (#76)
* add optimize * fix error of clang * update README * fix code style
1 parent 041fa25 commit fb67b9b

File tree

36 files changed

+1145
-1467
lines changed

36 files changed

+1145
-1467
lines changed
 

‎docs/arenastring.en.md

+2
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,5 @@ common --registry=https://baidu.github.io/babylon/registry
4141
# in MODULE.bazel
4242
bazel_dep(name = 'protobuf', version = '28.3.arenastring')
4343
```
44+
45+
Here is an example of using this patch with [brpc](https://github.com/apache/brpc): [use-arena-with-brpc](../example/use-arena-with-brpc), along with some performance demonstrations.

‎docs/arenastring.zh-cn.md

+2
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ common --registry=https://baidu.github.io/babylon/registry
3333
# in MODULE.bazel
3434
bazel_dep(name = 'protobuf', version = '28.3.arenastring')
3535
```
36+
37+
这里可以找到一个结合[brpc](https://github.com/apache/brpc)使用补丁的例子[use-arena-with-brpc](../example/use-arena-with-brpc)以及一些性能演示

‎example/anyflow/MODULE.bazel

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bazel_dep(name = 'babylon', version = '1.4.1')
1+
bazel_dep(name = 'babylon', version = '1.4.2')
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
bazel_dep(name = 'babylon', version = '1.4.1')
1+
bazel_dep(name = 'babylon', version = '1.4.2')

‎example/depend-use-bzlmod/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- 增加依赖项
66
```
77
# in MODULE.bazel
8-
bazel_dep(name = 'babylon', version = '1.4.1')
8+
bazel_dep(name = 'babylon', version = '1.4.2')
99
```
1010

1111
- 添加依赖的子模块到编译目标,全部可用子模块可以参照[模块功能文档](../../docs/README.zh-cn.md#模块功能文档),或者[BUILD](../../BUILD)文件,也可以直接添加All in One依赖目标`@babylon`

‎example/depend-use-cmake-fetch/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ set(BUILD_DEPS ON)
55
include(FetchContent)
66
FetchContent_Declare(
77
babylon
8-
URL "https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz"
9-
URL_HASH SHA256=930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8
8+
URL "https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz"
9+
URL_HASH SHA256=d60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1
1010
)
1111
FetchContent_MakeAvailable(babylon)
1212

‎example/depend-use-cmake-fetch/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ set(BUILD_DEPS ON)
1010
include(FetchContent)
1111
FetchContent_Declare(
1212
babylon
13-
URL "https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz"
14-
URL_HASH SHA256=930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8
13+
URL "https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz"
14+
URL_HASH SHA256=d60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1
1515
)
1616
FetchContent_MakeAvailable(babylon)
1717
```

‎example/depend-use-cmake-find/build.sh

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/bin/sh
22
set -ex
33

4-
URL=https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz
4+
URL=https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz
55
NAME=babylon-1.4.1
6-
SHA256=930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8
6+
SHA256=d60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1
77
if ! echo "$SHA256 $NAME.tar.gz" | sha256sum -c; then
88
wget $URL --continue -O $NAME.tar.gz
99
fi

‎example/depend-use-cmake-subdir/build.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/bin/sh
22
set -ex
33

4-
URL=https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz
5-
NAME=babylon-1.4.1
6-
SHA256=930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8
4+
URL=https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz
5+
NAME=babylon-1.4.2
6+
SHA256=d60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1
77
if ! echo "$SHA256 $NAME.tar.gz" | sha256sum -c; then
88
wget $URL --continue -O $NAME.tar.gz
99
fi

‎example/depend-use-workspace/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
# in WORKSPACE
88
http_archive(
99
name = 'com_baidu_babylon',
10-
urls = ['https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz'],
11-
strip_prefix = 'babylon-1.4.1',
12-
sha256 = '930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8',
10+
urls = ['https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz'],
11+
strip_prefix = 'babylon-1.4.2',
12+
sha256 = 'd60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1',
1313
)
1414
```
1515

‎example/depend-use-workspace/WORKSPACE

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ load('@bazel_tools//tools/build_defs/repo:http.bzl', 'http_archive')
66
# babylon
77
http_archive(
88
name = 'com_baidu_babylon',
9-
urls = ['https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz'],
10-
strip_prefix = 'babylon-1.4.1',
11-
sha256 = '930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8',
9+
urls = ['https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz'],
10+
strip_prefix = 'babylon-1.4.2',
11+
sha256 = 'd60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1',
1212
)
1313
################################################################################
1414

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
bazel_dep(name = 'babylon', version = '1.4.1')
2-
bazel_dep(name = 'brpc', version = '1.10.0.bcr.1')
1+
bazel_dep(name = 'babylon', version = '1.4.2')
2+
bazel_dep(name = 'brpc', version = '1.11.0')
33
bazel_dep(name = 'yaml-cpp', version = '0.8.0')
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.3.2
1+
7.4.0

‎example/use-arena-with-brpc/BUILD

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
cc_library(
2-
name = 'reusable_rpc_protocol',
3-
srcs = ['reusable_rpc_protocol.cpp', 'reusable_rpc_protocol.trick.cpp'],
4-
hdrs = ['reusable_rpc_protocol.h'],
2+
name = 'swiss_message_factory',
3+
srcs = ['swiss_message_factory.cpp'],
4+
hdrs = ['swiss_message_factory.h'],
55
deps = [
66
'@brpc//:brpc',
77
'@babylon//:concurrent_object_pool',
8-
'@babylon//:concurrent_transient_hash_table',
98
'@babylon//:reusable',
109
],
1110
)
@@ -35,7 +34,18 @@ cc_binary(
3534
srcs = ['server.cpp'],
3635
deps = [
3736
':cc_echo_proto',
38-
':reusable_rpc_protocol',
37+
'@brpc//:brpc',
38+
'@tcmalloc//tcmalloc',
39+
],
40+
)
41+
42+
cc_binary(
43+
name = 'server_babylon',
44+
srcs = ['server.cpp'],
45+
copts = ['-DWITH_BABYLON=1'],
46+
deps = [
47+
':cc_echo_proto',
48+
':swiss_message_factory',
3949
'@brpc//:brpc',
4050
'@tcmalloc//tcmalloc',
4151
],
+17-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
bazel_dep(name = 'babylon', version = '1.4.1')
2-
bazel_dep(name = 'brpc', version = '1.9.0.bcr.1')
1+
bazel_dep(name = 'babylon', version = '1.4.2')
2+
bazel_dep(name = 'brpc', version = '1.11.0')
33
bazel_dep(name = 'tcmalloc', version = '0.0.0-20240411-5ed309d')
4-
single_version_override(module_name = 'protobuf', version = '25.3.arenastring')
4+
5+
single_version_override(module_name = 'rules_fuzzing', version = '0.5.1')
6+
#single_version_override(module_name = 'protobuf', version = '28.3')
7+
single_version_override(module_name = 'protobuf', version = '28.3.arenastring')
8+
9+
archive_override(module_name = 'brpc',
10+
urls = ['https://github.com/apache/brpc/archive/30a56dd153010c0be88d7c197712e5b95843ff9c.zip'],
11+
integrity = 'sha256-94aE0H4NZo72DSB0wtq6YlMZc9fOpGAlK4QHgG0FvXg=',
12+
strip_prefix = 'brpc-30a56dd153010c0be88d7c197712e5b95843ff9c',
13+
)
14+
15+
local_path_override(
16+
module_name = 'babylon',
17+
path = '../..',
18+
)
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
**[[简体中文]](README.zh-cn.md)**
2+
3+
# Use Arena for brpc
4+
5+
Before invoking a user's service, [brpc](https://github.com/apache/brpc) needs to construct instances of `Request` and `Response` internally, as well as perform corresponding serialization and deserialization operations. By default, it uses dynamic heap memory allocation. For complex structures, the allocation and deallocation of memory, along with the construction and destruction of `Message` structures, can lead to noticeable overhead.
6+
7+
Since version 3.x, [Protobuf](https://github.com/protocolbuffers/protobuf) has introduced [Arena](https://protobuf.dev/reference/cpp/arenas) allocation, which enables aggregated allocation and deallocation for complex structures. More recent versions of [brpc](https://github.com/apache/brpc) also support the [Protobuf arena](https://github.com/apache/brpc/blob/master/docs/cn/server.md#protobuf-arena) component. Based on this, further acceleration for `string` members can be achieved by applying [arenastring](../../docs/arenastring.zh-cn.md).
8+
9+
In addition to using native [Arena](https://protobuf.dev/reference/cpp/arenas), you can also employ [babylon::SwissMemoryResource](../../docs/reusable/memory_resource.zh-cn.md#swissmemoryresource) for memory pool acceleration. [babylon::SwissMemoryResource](../../docs/reusable/memory_resource.zh-cn.md#swissmemoryresource) enables further flexibility through a customizable fixed-size paging reuse mechanism.
10+
11+
## Performance Demonstration
12+
13+
CPU: AMD EPYC 7W83 64-Core Processor, taskset 0-3 core
14+
15+
QPS: 800
16+
17+
- Default (mode: 0)
18+
- latency_percentiles: "[2213,2523,3232,3670]"
19+
- process_cpu_usage : 1.172
20+
- process_memory_resident : 44978722
21+
- Arena (mode: 1)
22+
- latency_percentiles: "[1318,1490,1794,1984]"
23+
- process_cpu_usage : 0.702
24+
- process_memory_resident : 41421824
25+
- Arena & ArenaString (mode: 1, arenastring patch)
26+
- latency_percentiles: "[1055,1196,1416,1583]"
27+
- process_cpu_usage : 0.572
28+
- process_memory_resident : 39732770
29+
- SwissMemoryResource & ArenaString (mode: 2, arenastring patch)
30+
- latency_percentiles: "[1006,1139,1341,1478]"
31+
- process_cpu_usage : 0.551
32+
- process_memory_resident : 44763136

‎example/use-arena-with-brpc/README.md

-72
This file was deleted.

‎example/use-arena-with-brpc/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
README.zh-cn.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
**[[English]](README.en.md)**
2+
3+
# Use arena for brpc
4+
5+
[brpc](https://github.com/apache/brpc)在调用用户的service前,需要在内部先完成Request和Response的实例构建,并在service前后执行对应的正反序列化。默认采用动态堆内存分配模式创建,对于比较复杂的结构,内存分配释放和Message结构的构建和析构可能也会带来可见的开销。
6+
7+
[Protobuf](https://github.com/protocolbuffers/protobuf)在3.x之后增加了[Arena](https://protobuf.dev/reference/cpp/arenas)分配功能,针对复杂结构提供了聚集分配和释放能力。较新版本的[brpc](https://github.com/apache/brpc)也提供了[Protobuf arena](https://github.com/apache/brpc/blob/master/docs/cn/server.md#protobuf-arena)组件进行支持。在这些基础上,通过应用[arenastirng](../../docs/arenastring.zh-cn.md)可以针对string成员实现进一步加速。
8+
9+
除了使用原生的[Arena](https://protobuf.dev/reference/cpp/arenas),也可以使用[babylon::SwissMemoryResource](../../docs/reusable/memory_resource.zh-cn.md#swissmemoryresource)实现内存池加速。[babylon::SwissMemoryResource](../../docs/reusable/memory_resource.zh-cn.md#swissmemoryresource)通过可定制的定长分页重用机制,可以进一步提升灵活性。
10+
11+
## 性能演示
12+
13+
CPU: AMD EPYC 7W83 64-Core Processor, taskset 0-3 core
14+
15+
QPS: 800
16+
17+
- Default (mode: 0)
18+
- latency_percentiles: "[2213,2523,3232,3670]"
19+
- process_cpu_usage : 1.172
20+
- process_memory_resident : 44978722
21+
- Arena (mode: 1)
22+
- latency_percentiles: "[1318,1490,1794,1984]"
23+
- process_cpu_usage : 0.702
24+
- process_memory_resident : 41421824
25+
- Arena & ArenaString (mode: 1, arenastring patch)
26+
- latency_percentiles: "[1055,1196,1416,1583]"
27+
- process_cpu_usage : 0.572
28+
- process_memory_resident : 39732770
29+
- SwissMemoryResource & ArenaString (mode: 2, arenastring patch)
30+
- latency_percentiles: "[1006,1139,1341,1478]"
31+
- process_cpu_usage : 0.551
32+
- process_memory_resident : 44763136

‎example/use-arena-with-brpc/build.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/bin/sh
22
set -ex
33

4-
bazel build --registry=https://bcr.bazel.build --registry=https://baidu.github.io/babylon/registry --compilation_mode=opt --cxxopt=-std=c++17 client server
4+
bazel build --registry=file:///home/oathdruid/src/babylon/registry --registry=https://bcr.bazel.build --registry=https://baidu.github.io/babylon/registry --compilation_mode=opt --cxxopt=-std=c++17 client server server_babylon

‎example/use-arena-with-brpc/client.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ DEFINE_string(connection_type, "",
1010
"Connection type. Available values: single, pooled, short");
1111
DEFINE_string(server, "0.0.0.0:8000", "IP Address of server");
1212
DEFINE_int32(timeout_ms, 500, "RPC timeout in milliseconds");
13-
DEFINE_uint64(qps, 100, "");
13+
DEFINE_uint64(qps, 750, "");
1414

1515
DEFINE_uint64(payload_scale, 10, "");
1616

0 commit comments

Comments
 (0)
Please sign in to comment.