Skip to content

Performance: Excessive disk writes during active download stream #6

@windoze95

Description

@windoze95

File: lib/services/offline_service.dart
Line: ~53-61 (in downloadToDevice -> onReceiveProgress)

Description:
The downloadToDevice() method hooks Hive.box.put() synchronously onto the Dio onReceiveProgress callback:

onReceiveProgress: (received, total) {
  if (total > 0) {
    _box.put(videoId, {...});
  }
}

Since the progress callback is called extremely frequently (hundreds of times per second for fast network streams), it hammers the device's storage layer with disk write operations, causing severe lag and frame drops during downloads.

Impact:
Severe UI stuttering, frame drops, and unnecessary wear on device flash storage.

Suggested Fix:
Throttle the progress writes. Since downloadProgressProvider already handles realtime UI progress via WebSocket events, this disk state should only be updated periodically (e.g. every 500ms or 1MB written):

int lastUpdate = DateTime.now().millisecondsSinceEpoch;
onReceiveProgress: (received, total) {
  int now = DateTime.now().millisecondsSinceEpoch;
  if (now - lastUpdate > 500 || received == total) {
    lastUpdate = now;
    _box.put(videoId, ...);
  }
}

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions