feat(lib/assets): micro-optimizations

This commit is contained in:
PalmDevs 2025-08-22 02:37:18 +07:00
parent 63fd1938cf
commit bb7b1ae05c
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
2 changed files with 14 additions and 5 deletions

View file

@ -15,6 +15,9 @@ export {
AssetsRegistryModuleId,
} from './preinit'
// Resolve reference once and keep in closure
const metroRequire = __r
// iOS cannot display SVGs
let _preferredType: Asset['type'] = Platform.OS === 'ios' ? 'png' : 'svg'
/**
@ -47,7 +50,7 @@ export function* getCustomAssets(): Generator<CustomAsset> {
export function* getPackagerAssets(): Generator<PackagerAsset> {
for (const reg of Object.values(cache))
for (const moduleId of Object.values(reg))
yield AssetsRegistry.getAssetByID(__r(moduleId))
yield AssetsRegistry.getAssetByID(metroRequire(moduleId))
}
/**
@ -79,7 +82,9 @@ export function getAssetsByName(
return Object.entries(reg).reduce(
(acc, [type, mid]) => {
acc[type as Asset['type']] = AssetsRegistry.getAssetByID(__r(mid))!
acc[type as Asset['type']] = AssetsRegistry.getAssetByID(
metroRequire(mid),
)!
return acc
},
{} as Record<Asset['type'], Asset>,
@ -106,13 +111,13 @@ export function getAssetIdByName(
if (type !== undefined) {
const mid = reg[type]
return mid && __r(mid)
return mid && metroRequire(mid)
}
let mid = reg[_preferredType]
mid ??= Object.values(reg)[0]
return mid && __r(mid)
return mid && metroRequire(mid)
}
/**

View file

@ -55,6 +55,9 @@ const unsubAR = waitForModules(
// TODO(lib/assets/caches): Ideally we should not wait for the cache promise here, see the promise impl for more info.
cached.then(cached => {
if (!cached) {
// Resolve reference once and keep in closure
const metroRequire = __r
// More fragile way, but also more performant:
// There is exactly one asset before the reexported asset registry :/
const firstAssetModuleId = id - 1
@ -62,7 +65,8 @@ const unsubAR = waitForModules(
if (mId < firstAssetModuleId) continue
const deps = getModuleDependencies(mId)!
if (deps.length === 1 && deps[0] === id) __r(mId)
if (deps.length === 1 && deps[0] === id)
metroRequire(mId)
}
}
})