Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Improvement: Optimize CompressingRequestBody memory usage by removing Okio and redundant buffering

Aperta
#10,052 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
48/100
Tipo di issue
Refactoring
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
java

Direzione di ricerca

Inizia individuando CompressingRequestBody.attemptWrite nel codice di profiling-uploader e analizza l'attuale percorso di copia di Okio, il flusso di compressione e il buffering. Verifica il comportamento di copia degli stream compatibile con Java 8 e conferma che la modifica mantenga l'output della compressione, la gestione della proprietà degli stream e la riduzione di memoria prevista.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

comp: profiling type: feature request
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:

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

Lingua principale
Java
Stelle
737
Fork
361
Merge medio
3g 20h
PR unite (30g)
173

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di DataDog/dd-trace-java

Tutte le issue di DataDog/dd-trace-java

Issue simili

Altre issue su Java

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.