Skip to content

Parallel batch loading + PrefabIndex + dynamic batch size#2

Merged
Tribow merged 5 commits into
mainfrom
car-cache-only
Jul 9, 2026
Merged

Parallel batch loading + PrefabIndex + dynamic batch size#2
Tribow merged 5 commits into
mainfrom
car-cache-only

Conversation

@HectorMu

@HectorMu HectorMu commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

Replaces sequential one-by-one asset-bundle scanning with parallel batched loading, a lightweight prefab-name index, and several performance optimizations.

Changes (5 files, code only)

Parallel bundle loading (CarBuilder.cs — modified)

The core loading method was rewritten to support parallel batch processing:

  • ThreadPool batchesThreadPool.QueueUserWorkItem loads bundles in parallel, synchronized via ManualResetEvent. Overlaps disk I/O across bundles.
  • 30-second timeout — prevents a single corrupted file from stalling startup; incomplete results are logged and skipped.
  • Dynamic batch sizeMath.Max(2, Environment.ProcessorCount) replaces the old hardcoded 4. Adapts to each user's core count automatically.
  • Magic byte validation — reads the first 8 bytes of each file, checks for UnityFS/UnityRaw/UnityWeb headers before calling AssetBundle.LoadFromFile. Rejects non-bundles in microseconds instead of timing out.
  • Bundle unloadingbundle.Unload(false) after extracting prefabs, freeing compressed data while keeping loaded GameObjects valid.
  • Material array fixrenderer.materials captured once into a local variable instead of accessed repeatedly (avoided per-iteration array allocation).

Prefab name index (PrefabIndex.cs — NEW, ~120 lines)

A lightweight JSON index that avoids the expensive GetAllAssetNames() call for unchanged files:

  • Key by path — maps each bundle file path to its last-write time + discovered prefab names.
  • Up-to-date check — compares File.GetLastWriteTimeUtc ticks. If unchanged, prefab names are read from the index; GetAllAssetNames() is skipped entirely.
  • Stale cleanup — after loading, entries for deleted or replaced files are removed from the index.
  • Persistence — stored in Settings/prefab_index.json via JsonFx (same serializer already used by the project).

Much simpler than the previous CarCache approach — no signature hashing, no invalid-file tracking, no combined hash. Just a cached list of prefab names per file.

Assets.cs — cached reflection

AssetBundleBridge.AssetBundleType was calling Kernel.FindTypeByFullName() on every access (247+ times). The type and MethodInfo are now resolved once in the static constructor. The "Loaded asset bundle" log was also removed.

Section.cs + DictionaryExtensions.cs — factory overload for GetOrCreate

GetOrCreate<T>(string key, Func<T> factory) avoids allocating the default value when the key already exists. The extension method on Dictionary<string, object> mirrors the Section instance method.

ProfileCarColors.cs — use factory overload + cleanup

Switched all three GetOrCreate calls to the factory overload. Removed two LogInfo messages ("Assigning Profile Name...", "Assigning Vehicle Name...") that produced 500+ log lines at startup.

@HectorMu HectorMu requested review from REHERC and Tribow July 7, 2026 00:00
@HectorMu HectorMu changed the title CarCache + parallel batch loading + performance optimizations Parallel batch loading + performance optimizations Jul 8, 2026
@HectorMu HectorMu changed the title Parallel batch loading + performance optimizations Parallel batch loading + PrefabIndex + dynamic batch size Jul 8, 2026
@Tribow Tribow merged commit 2ca2ca3 into main Jul 9, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants