Files
UnrealEngineUWP/Engine/Source/Runtime/PakFile/Private/FilePackageStore.h
johan torp f910700701 Memory-optimized custom IOStore hash map - 65.7 -> 25.9MB for internal project on Gen5 console
Exploits that the map is large (~1M) and rarely modified

Memory saved by:
* TMap saves slot and next-in-slot using 8B / item. FPackageIdMap is built once presorted, next-in-slot is next item. Only use 1bit / item to mark slot end.
* Only store 3/4 of FPackageId key - 1/4 is implicit by slot, which requires min 128K capacity
* Up to 400% load factor using 4-stage lookup, slot -> 2B hash -> 4B hash -> value. 2B array fits 32 items / 64B cache line, enabling a high load factor with little perf loss
* More compact entry handle and entry memory layout
* Deduplicating some entries
* Carefully storing entry data so container ownership is inferable from offset allows dropping 7.5MB of FIoContainerHeader::PackageIds and non-deduped StoreEntries

Surprisingly lookup got >4.5x faster too, despite 4 memory indirections instead of 2. Lookup can be optimized further by vectorizing. Rebuilding the map is also faster.

#rb pj.kack

[CL 29906389 by johan torp in ue5-main branch]
2023-11-23 03:17:18 -05:00

134 lines
3.8 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "IO/PackageId.h"
#include "IO/PackageStore.h"
#include "Misc/PackagePath.h"
struct FIoContainerHeader;
struct FFilePackageStoreEntry;
namespace UE::FilePackageStorePrivate
{
struct FMountedDataRange
{
uint32 Begin = 0;
uint32 End = 0;
};
struct FMountedContainer
{
FIoContainerHeader* ContainerHeader;
uint32 Order;
uint32 Sequence;
uint32 NumMountedPackages = 0;
FMountedDataRange EntryDataRange;
};
struct FEntryHandle
{
static constexpr uint32 OffsetBits = 30;
uint32 Offset : OffsetBits;
uint32 HasPackageIds : 1;
uint32 HasShaderMaps : 1;
FEntryHandle()
{
FMemory::Memzero(*this);
}
};
// Memory-optimized immutable FPackageId -> FEntryHandle hash map
class FPackageIdMap
{
public:
FPackageIdMap() = default;
explicit FPackageIdMap(TArray<TPair<FPackageId, FEntryHandle>>&& Pairs);
FPackageIdMap(FPackageIdMap&& O) { *this = MoveTemp(O); }
FPackageIdMap& operator=(FPackageIdMap&& O);
~FPackageIdMap() { Empty(); }
void Empty();
const FEntryHandle* Find(FPackageId Key) const;
uint32 GetCapacity() const { return MaxValues; }
uint64 GetAllocatedSize() const;
class FConstIterator;
private:
uint32 MaxValues = 0;
uint32 SlotBits = 0;
uint32* Slots = nullptr;
uint16* Values = nullptr;
uint32 NumSlots() const
{
return SlotBits ? (1u << SlotBits) : 0u;
}
};
struct FUncookedPackage
{
FName PackageName;
EPackageExtension HeaderExtension;
};
} // namespace UE::FilePackageStorePrivate
/*
* File/container based package store.
*/
class FFilePackageStoreBackend : public IPackageStoreBackend
{
public:
virtual void OnMounted(TSharedRef<const FPackageStoreBackendContext>) override {}
virtual void BeginRead() override;
virtual void EndRead() override;
virtual EPackageStoreEntryStatus GetPackageStoreEntry(FPackageId PackageId, FName PackageName, FPackageStoreEntry& OutPackageStoreEntry) override;
virtual bool GetPackageRedirectInfo(FPackageId PackageId, FName& OutSourcePackageName, FPackageId& OutRedirectedToPackageId) override;
void Mount(FIoContainerHeader* ContainerHeader, uint32 Order);
void Unmount(const FIoContainerHeader* ContainerHeader);
private:
using FMountedDataRange = UE::FilePackageStorePrivate::FMountedDataRange;
using FMountedContainer = UE::FilePackageStorePrivate::FMountedContainer;
using FEntryHandle = UE::FilePackageStorePrivate::FEntryHandle;
using FPackageIdMap = UE::FilePackageStorePrivate::FPackageIdMap;
using FUncookedPackage = UE::FilePackageStorePrivate::FUncookedPackage;
FRWLock EntriesLock;
FCriticalSection UpdateLock;
TArray<FMountedContainer> MountedContainers;
TAtomic<uint32> NextSequence{ 0 };
FPackageIdMap PackageEntries;
TArray<uint32> EntryData;
TMap<FPackageId, TTuple<FName, FPackageId>> RedirectsPackageMap;
TMap<FPackageId, FName> LocalizedPackages;
bool bNeedsContainerUpdate = false;
#if WITH_EDITOR
FDelegateHandle OnContentPathMountedDelegateHandle;
FDelegateHandle OnContentPathDismountedDelegateHandle;
FCriticalSection UncookedPackageRootsLock;
TSet<FString> PendingAddUncookedPackageRoots;
TSet<FString> PendingRemoveUncookedPackageRoots;
TMap<FPackageId, FUncookedPackage> UncookedPackagesMap;
TMap<FPackageId, const FFilePackageStoreEntry*> OptionalSegmentStoreEntriesMap;
bool bNeedsUncookedPackagesUpdate = false;
uint64 AddUncookedPackagesFromRoot(const FString& RootPath);
uint64 RemoveUncookedPackagesFromRoot(const TSet<FString>& RootPath);
#endif //if WITH_EDITOR
void Update();
FEntryHandle AddNewEntryData(const FFilePackageStoreEntry& Entry);
FEntryHandle AddOldEntryData(FEntryHandle OldHandle, TConstArrayView<uint32> OldEntryData);
TConstArrayView<FPackageId> GetImportedPackages(FEntryHandle Handle);
TConstArrayView<FSHAHash> GetShaderHashes(FEntryHandle Handle);
};