Skip to content

Feature Request: Add option to disable HTTPCache creation in Setup() #227

@godhedin

Description

@godhedin

Problem

HTTPManager.Setup() creates HTTPCache in its constructor, which performs synchronous disk I/O on the main thread:

  • 4x DirectoryExists + DirectoryCreate
  • 3x CreateFileStream(OpenReadWrite) — .db, .freelist, .metadata files
  • FreeListManager.Load() — reads full free list from disk
  • MetadataService.LoadFrom(stream) — reads ALL metadata entries in a while loop

On budget Android devices (Samsung A05, Redmi, Tecno, itel — 2-3 GB RAM, slow eMMC storage), this blocks the main thread for 2-5+ seconds, causing a black screen during app startup.

Our app does not use HTTP response caching (no cache headers, all API calls return dynamic data), but there is no way to skip cache creation in Setup() without modifying the library.

Current workarounds and their limitations

  1. Setting LocalCache before Setup() — requires creating an HTTPCache instance, which still triggers the heavy constructor I/O.
  2. Background thread pre-creation — race conditions with Setup(), IsSetupCalled is not thread-safe.
  3. BESTHTTP_DISABLE_CACHING define — does not exist in Best HTTP 3.x (was in 2.x).

Proposed solution

Add a Disabled property to HTTPCacheOptions:

// HTTPCacheOptions.cs
public sealed class HTTPCacheOptions
{
    /// <summary>
    /// When true, the cache is created in a disabled state — no disk I/O occurs in the constructor.
    /// All cache operations become no-ops.
    /// </summary>
    public bool Disabled { get; set; }

    // ... existing properties ...
}

And an early return in HTTPCache constructor:

// HTTPCache.cs constructor
public HTTPCache(HTTPCacheOptions options)
{
    this.Options = options ?? new HTTPCacheOptions();

    if (this.Options.Disabled)
    {
        _isSupported = false;
        _database = null;
        return;
    }

    // ... existing heavy I/O code (unchanged) ...
}

This follows the existing pattern already used for WebGL:

#if UNITY_WEBGL && !UNITY_EDITOR
    this._isSupported = false;
    this._database = null;
#endif

All 17 public methods in HTTPCache already check _isSupported before performing any work, so the disabled cache is a safe no-op object.

Usage

// Before Setup() — creates a no-op cache with zero disk I/O
HTTPManager.LocalCache = new HTTPCache(new HTTPCacheOptions { Disabled = true });
CookieJar.IsEnabled = false;

// Setup() sees LocalCache != null -> skips cache creation
// CookieJar.Load() sees IsEnabled == false -> skips cookie I/O
// Result: Setup() completes in < 1ms
HTTPManager.Setup();

Environment

  • Best HTTP version: 3.0.17
  • Unity: 6000.0.58f2
  • Platform: Android (ARM64 + ARMv7)
  • Affected devices: budget Android with 2-3 GB RAM and slow eMMC storage

Impact

This issue affects ~315 users in our app (80K+ ANR events from the slow Setup() call). The fix is minimal (1 property + 5 lines) and backward-compatible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions