Improvement: Optimize CompressingRequestBody memory usage by removing Okio and redundant buffering
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 48/100
- Loại issue
- Tái cấu trúc
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Đình trệ
- Công nghệ
- java
- Lĩnh vực
- backend, performance
Hướng nghiên cứu
Bắt đầu bằng cách tìm CompressingRequestBody.attemptWrite trong mã của profiling-uploader và kiểm tra đường dẫn sao chép Okio hiện tại, luồng nén và cơ chế đệm. Xác minh hành vi sao chép luồng tương thích với Java 8 và xác nhận rằng thay đổi này giữ nguyên đầu ra nén, quyền sở hữu luồng và mức giảm bộ nhớ dự kiến.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
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.
- Ngôn ngữ chính
- Java
- Star
- 737
- Fork
- 361
- Merge trung bình
- 3 ngày 20 giờ
- Pull request đã merge (30 ngày)
- 173
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của DataDog/dd-trace-java
-
type: feature request
Độ khó 1/5 1-3 giờ Mức phù hợp với người mới 70/100
DataDog/dd-trace-java#10245 · 1 bình luận ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 62/100
DataDog/dd-trace-java#12608 ·
-
type: bug report
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
DataDog/dd-trace-java#12597 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
DataDog/dd-trace-java#12540 · 4 bình luận · 1 người được giao ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 25/100
DataDog/dd-trace-java#12480 ·
Tất cả issue của DataDog/dd-trace-java
Issue tương tự
-
documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
inu-appcenter/memorIN-backend#288 ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 65/100
-
frontend maui-pilot pilot-ask question
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
executions.Query — startDate and timeRange filters are sent with inverted comparison operators Đang mởarea/plugin
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
kestra-io/plugin-kestra#190 ·