Improvement: Optimize CompressingRequestBody memory usage by removing Okio and redundant buffering
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 48/100
- issue の種類
- リファクタリング
- 明瞭さ
- 明確に書かれている
- 活発さ
- 停滞
- 技術スタック
- java
- 領域
- backend, performance
調査の方向性
まず profiling-uploader のコード内で CompressingRequestBody.attemptWrite を見つけ、現在の Okio のコピー経路、圧縮ストリーム、バッファリングを確認します。Java 8 互換のストリームコピー動作を検証し、変更によって圧縮出力、ストリームの所有権、意図したメモリ削減が維持されることを確認します。
索引モデルが issue の本文から書いたものです。
説明
Library Name
dd-java-agent (profiling-uploader)
Library Version(s)
v1.54.0
Describe the feature you'd like
I propose optimizing CompressingRequestBody.attemptWrite to reduce memory allocation.
Currently, the implementation uses Okio for stream copying and wraps the compression stream with an extra BufferedOutputStream. This results in excessive object creation (Segments, Buffers) and double buffering overhead.
Suggested Implementation:
I suggest replacing Okio with a standard Java IO loop using a fixed-size byte[] buffer and removing the redundant outer BufferedOutputStream.
private void attemptWrite(@Nonnull InputStream inputStream, @Nonnull OutputStream outputStream)
throws IOException {
// Keep the inner buffer to aggregate compressed bytes before sending to the socket
OutputStream bufferedInnerStream = new BufferedOutputStream(outputStream) {
@Override
public void close() throws IOException {
flush(); // Prevent closing the underlying stream
}
};
// Remove the outer BufferedOutputStream.
// Direct writing to the compression stream is efficient enough with a 8KB copy buffer.
OutputStream processingStream = isCompressed(inputStream)
? bufferedInnerStream
: outputStreamMapper.apply(bufferedInnerStream);
try (OutputStream out = processingStream) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, bytesRead);
}
}
// implicit close() on processingStream finalizes compression and flushes the inner stream
}
Is your feature request related to a problem?
Yes. While profiling our application, we noticed that CompressingRequestBody.attemptWrite is a major contributor to heap usage.
In our specific profile, this method accounted for approximately 94% of the Heap Live Size. The heavy use of Okio intermediate objects for simple stream copying seems to be the primary cause.
Please see the attached Flame Graph for evidence:
Describe alternatives you've considered
Using InputStream.transferTo(OutputStream) is an option for Java 9+, but the proposed byte-array loop ensures compatibility with Java 8, which I believe the agent still supports.
Additional context
Note: I am not a native English speaker, so I used an AI assistant to help draft this issue to ensure clarity. Thank you for your understanding.
- 主要言語
- Java
- スター
- 737
- フォーク
- 361
- 平均マージ
- 3日 20時間
- マージ済み PR(30日)
- 173
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
DataDog/dd-trace-java のほかの issue
-
type: feature request
難易度 1/5 1〜3時間 初心者へのやさしさ 70/100
DataDog/dd-trace-java#10245 · コメント 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 62/100
DataDog/dd-trace-java#12608 ·
-
type: bug report
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
DataDog/dd-trace-java#12597 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
DataDog/dd-trace-java#12540 · コメント 4 件 · 担当者 1 名 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 25/100
DataDog/dd-trace-java#12480 ·
DataDog/dd-trace-java の issue をすべて見る
似ている issue
-
area/plugin
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
kestra-io/plugin-kestra#190 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
google-ai-edge/LiteRT-LM#3739 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
integra-team-red/meet-map#249 ·
-
[Studio][Bug] Cancelled create-user dialog keeps the password and admin switch for the next attempt オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
apache/rocketmq-dashboard#5064 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
wso2/dpdp-accelerator#287 ·