Skip to content

Commit

Permalink
add optimize (#76)
Browse files Browse the repository at this point in the history
* add optimize

* fix error of clang

* update README

* fix code style
  • Loading branch information
oathdruid authored Nov 7, 2024
1 parent 041fa25 commit fb67b9b
Show file tree
Hide file tree
Showing 36 changed files with 1,145 additions and 1,467 deletions.
2 changes: 2 additions & 0 deletions docs/arenastring.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ common --registry=https://baidu.github.io/babylon/registry
# in MODULE.bazel
bazel_dep(name = 'protobuf', version = '28.3.arenastring')
```

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.
2 changes: 2 additions & 0 deletions docs/arenastring.zh-cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ common --registry=https://baidu.github.io/babylon/registry
# in MODULE.bazel
bazel_dep(name = 'protobuf', version = '28.3.arenastring')
```

这里可以找到一个结合[brpc](https://github.com/apache/brpc)使用补丁的例子[use-arena-with-brpc](../example/use-arena-with-brpc)以及一些性能演示
2 changes: 1 addition & 1 deletion example/anyflow/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bazel_dep(name = 'babylon', version = '1.4.1')
bazel_dep(name = 'babylon', version = '1.4.2')
2 changes: 1 addition & 1 deletion example/depend-use-bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1 +1 @@
bazel_dep(name = 'babylon', version = '1.4.1')
bazel_dep(name = 'babylon', version = '1.4.2')
2 changes: 1 addition & 1 deletion example/depend-use-bzlmod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- 增加依赖项
```
# in MODULE.bazel
bazel_dep(name = 'babylon', version = '1.4.1')
bazel_dep(name = 'babylon', version = '1.4.2')
```

- 添加依赖的子模块到编译目标,全部可用子模块可以参照[模块功能文档](../../docs/README.zh-cn.md#模块功能文档),或者[BUILD](../../BUILD)文件,也可以直接添加All in One依赖目标`@babylon`
Expand Down
4 changes: 2 additions & 2 deletions example/depend-use-cmake-fetch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ set(BUILD_DEPS ON)
include(FetchContent)
FetchContent_Declare(
babylon
URL "https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz"
URL_HASH SHA256=930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8
URL "https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz"
URL_HASH SHA256=d60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1
)
FetchContent_MakeAvailable(babylon)

Expand Down
4 changes: 2 additions & 2 deletions example/depend-use-cmake-fetch/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ set(BUILD_DEPS ON)
include(FetchContent)
FetchContent_Declare(
babylon
URL "https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz"
URL_HASH SHA256=930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8
URL "https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz"
URL_HASH SHA256=d60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1
)
FetchContent_MakeAvailable(babylon)
```
Expand Down
4 changes: 2 additions & 2 deletions example/depend-use-cmake-find/build.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/sh
set -ex

URL=https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz
URL=https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz
NAME=babylon-1.4.1
SHA256=930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8
SHA256=d60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1
if ! echo "$SHA256 $NAME.tar.gz" | sha256sum -c; then
wget $URL --continue -O $NAME.tar.gz
fi
Expand Down
6 changes: 3 additions & 3 deletions example/depend-use-cmake-subdir/build.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/sh
set -ex

URL=https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz
NAME=babylon-1.4.1
SHA256=930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8
URL=https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz
NAME=babylon-1.4.2
SHA256=d60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1
if ! echo "$SHA256 $NAME.tar.gz" | sha256sum -c; then
wget $URL --continue -O $NAME.tar.gz
fi
Expand Down
6 changes: 3 additions & 3 deletions example/depend-use-workspace/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
# in WORKSPACE
http_archive(
name = 'com_baidu_babylon',
urls = ['https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz'],
strip_prefix = 'babylon-1.4.1',
sha256 = '930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8',
urls = ['https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz'],
strip_prefix = 'babylon-1.4.2',
sha256 = 'd60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1',
)
```

Expand Down
6 changes: 3 additions & 3 deletions example/depend-use-workspace/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ load('@bazel_tools//tools/build_defs/repo:http.bzl', 'http_archive')
# babylon
http_archive(
name = 'com_baidu_babylon',
urls = ['https://github.com/baidu/babylon/releases/download/v1.4.1/v1.4.1.tar.gz'],
strip_prefix = 'babylon-1.4.1',
sha256 = '930e8d24822a472466e8b616011a57c37021b02486ad19ee7b62c12bfef923b8',
urls = ['https://github.com/baidu/babylon/releases/download/v1.4.2/v1.4.2.tar.gz'],
strip_prefix = 'babylon-1.4.2',
sha256 = 'd60ee9cd86a777137bf021c8861e97438a69cc857659d5eb39af9e8464434cf1',
)
################################################################################

Expand Down
4 changes: 2 additions & 2 deletions example/use-anyflow-with-brpc/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
bazel_dep(name = 'babylon', version = '1.4.1')
bazel_dep(name = 'brpc', version = '1.10.0.bcr.1')
bazel_dep(name = 'babylon', version = '1.4.2')
bazel_dep(name = 'brpc', version = '1.11.0')
bazel_dep(name = 'yaml-cpp', version = '0.8.0')
2 changes: 1 addition & 1 deletion example/use-arena-with-brpc/.bazelversion
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.3.2
7.4.0
20 changes: 15 additions & 5 deletions example/use-arena-with-brpc/BUILD
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
cc_library(
name = 'reusable_rpc_protocol',
srcs = ['reusable_rpc_protocol.cpp', 'reusable_rpc_protocol.trick.cpp'],
hdrs = ['reusable_rpc_protocol.h'],
name = 'swiss_message_factory',
srcs = ['swiss_message_factory.cpp'],
hdrs = ['swiss_message_factory.h'],
deps = [
'@brpc//:brpc',
'@babylon//:concurrent_object_pool',
'@babylon//:concurrent_transient_hash_table',
'@babylon//:reusable',
],
)
Expand Down Expand Up @@ -35,7 +34,18 @@ cc_binary(
srcs = ['server.cpp'],
deps = [
':cc_echo_proto',
':reusable_rpc_protocol',
'@brpc//:brpc',
'@tcmalloc//tcmalloc',
],
)

cc_binary(
name = 'server_babylon',
srcs = ['server.cpp'],
copts = ['-DWITH_BABYLON=1'],
deps = [
':cc_echo_proto',
':swiss_message_factory',
'@brpc//:brpc',
'@tcmalloc//tcmalloc',
],
Expand Down
20 changes: 17 additions & 3 deletions example/use-arena-with-brpc/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
bazel_dep(name = 'babylon', version = '1.4.1')
bazel_dep(name = 'brpc', version = '1.9.0.bcr.1')
bazel_dep(name = 'babylon', version = '1.4.2')
bazel_dep(name = 'brpc', version = '1.11.0')
bazel_dep(name = 'tcmalloc', version = '0.0.0-20240411-5ed309d')
single_version_override(module_name = 'protobuf', version = '25.3.arenastring')

single_version_override(module_name = 'rules_fuzzing', version = '0.5.1')
#single_version_override(module_name = 'protobuf', version = '28.3')
single_version_override(module_name = 'protobuf', version = '28.3.arenastring')

archive_override(module_name = 'brpc',
urls = ['https://github.com/apache/brpc/archive/30a56dd153010c0be88d7c197712e5b95843ff9c.zip'],
integrity = 'sha256-94aE0H4NZo72DSB0wtq6YlMZc9fOpGAlK4QHgG0FvXg=',
strip_prefix = 'brpc-30a56dd153010c0be88d7c197712e5b95843ff9c',
)

local_path_override(
module_name = 'babylon',
path = '../..',
)
32 changes: 32 additions & 0 deletions example/use-arena-with-brpc/README.en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
**[[简体中文]](README.zh-cn.md)**

# Use Arena for brpc

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.

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).

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.

## Performance Demonstration

CPU: AMD EPYC 7W83 64-Core Processor, taskset 0-3 core

QPS: 800

- Default (mode: 0)
- latency_percentiles: "[2213,2523,3232,3670]"
- process_cpu_usage : 1.172
- process_memory_resident : 44978722
- Arena (mode: 1)
- latency_percentiles: "[1318,1490,1794,1984]"
- process_cpu_usage : 0.702
- process_memory_resident : 41421824
- Arena & ArenaString (mode: 1, arenastring patch)
- latency_percentiles: "[1055,1196,1416,1583]"
- process_cpu_usage : 0.572
- process_memory_resident : 39732770
- SwissMemoryResource & ArenaString (mode: 2, arenastring patch)
- latency_percentiles: "[1006,1139,1341,1478]"
- process_cpu_usage : 0.551
- process_memory_resident : 44763136
72 changes: 0 additions & 72 deletions example/use-arena-with-brpc/README.md

This file was deleted.

1 change: 1 addition & 0 deletions example/use-arena-with-brpc/README.md
32 changes: 32 additions & 0 deletions example/use-arena-with-brpc/README.zh-cn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
**[[English]](README.en.md)**

# Use arena for brpc

[brpc](https://github.com/apache/brpc)在调用用户的service前,需要在内部先完成Request和Response的实例构建,并在service前后执行对应的正反序列化。默认采用动态堆内存分配模式创建,对于比较复杂的结构,内存分配释放和Message结构的构建和析构可能也会带来可见的开销。

[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成员实现进一步加速。

除了使用原生的[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)通过可定制的定长分页重用机制,可以进一步提升灵活性。

## 性能演示

CPU: AMD EPYC 7W83 64-Core Processor, taskset 0-3 core

QPS: 800

- Default (mode: 0)
- latency_percentiles: "[2213,2523,3232,3670]"
- process_cpu_usage : 1.172
- process_memory_resident : 44978722
- Arena (mode: 1)
- latency_percentiles: "[1318,1490,1794,1984]"
- process_cpu_usage : 0.702
- process_memory_resident : 41421824
- Arena & ArenaString (mode: 1, arenastring patch)
- latency_percentiles: "[1055,1196,1416,1583]"
- process_cpu_usage : 0.572
- process_memory_resident : 39732770
- SwissMemoryResource & ArenaString (mode: 2, arenastring patch)
- latency_percentiles: "[1006,1139,1341,1478]"
- process_cpu_usage : 0.551
- process_memory_resident : 44763136
2 changes: 1 addition & 1 deletion example/use-arena-with-brpc/build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
set -ex

bazel build --registry=https://bcr.bazel.build --registry=https://baidu.github.io/babylon/registry --compilation_mode=opt --cxxopt=-std=c++17 client server
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
2 changes: 1 addition & 1 deletion example/use-arena-with-brpc/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ DEFINE_string(connection_type, "",
"Connection type. Available values: single, pooled, short");
DEFINE_string(server, "0.0.0.0:8000", "IP Address of server");
DEFINE_int32(timeout_ms, 500, "RPC timeout in milliseconds");
DEFINE_uint64(qps, 100, "");
DEFINE_uint64(qps, 750, "");

DEFINE_uint64(payload_scale, 10, "");

Expand Down
Loading

0 comments on commit fb67b9b

Please sign in to comment.