Skip to content

_write() bypasses chunk batching in chunked mode #105

@VikramAditya33

Description

@VikramAditya33

UwsResponse._write() sends each chunk to uWS individually instead of using the existing writeChunk() batching system. For readable.pipe(res) with many small chunks (e.g., 4KB from fs.createReadStream), this results in hundreds of uWS.write() syscalls instead of ~4-16 batched writes.

Root cause: _write() calls streamChunk() directly:

_write(chunk, encoding, callback) {
    const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk, encoding);
    this.streamChunk(buffer, this.contentLengthTotal)
        .then(() => callback())
        .catch((error) => callback(error));
}

streamChunk() calls this.atomic(() => this.uwsRes.write(buffer)) per chunk with no batching.

Existing batching: writeChunk() already implements intelligent batching:

  • Accumulates chunks in pendingChunks[]
  • Flushes when HIGH_WATERMARK (128KB) reached
  • Flushes on FLUSH_INTERVAL (50ms) timeout
  • First chunk sent immediately for low latency

Metadata

Metadata

Labels

CRITICALCRITICAL bug/fix neededperformanceAny fix around optimization

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions