Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use caching to improve performance of large profiles #1151

Merged
merged 1 commit into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/views/LocalModList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ import SearchUtils from '../../utils/SearchUtils';
}

getThunderstoreModFromMod(mod: ManifestV2) {
return ModBridge.getThunderstoreModFromMod(mod, this.thunderstorePackages);
return ModBridge.getCachedThunderstoreModFromMod(mod);
}

async moveUp(vueMod: any) {
Expand Down Expand Up @@ -360,8 +360,8 @@ import SearchUtils from '../../utils/SearchUtils';
this.filterModList();
}

isLatest(vueMod: any): boolean {
return ModBridge.isLatestVersion(vueMod);
isLatest(mod: ManifestV2): boolean {
return ModBridge.isCachedLatestVersion(mod);
}

getMissingDependencies(vueMod: any): string[] {
Expand Down
4 changes: 4 additions & 0 deletions src/model/VersionNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ export default class VersionNumber implements ReactiveObjectConverterInterface {
const patchCompare = Math.sign(this.patch - version.patch);
return (majorCompare === 0 && minorCompare === 0 && patchCompare === 0);
}

public isEqualOrNewerThan(version: VersionNumber): boolean {
return this.isEqualTo(version) || this.isNewerThan(version);
}
}
3 changes: 3 additions & 0 deletions src/r2mm/data/ThunderstorePackages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ThunderstoreMod from '../../model/ThunderstoreMod';
import Game from '../../model/game/Game';
import ApiResponse from '../../model/api/ApiResponse';
import ConnectionProvider from '../../providers/generic/connection/ConnectionProvider';
import ModBridge from '../mods/ModBridge';

export default class ThunderstorePackages {

Expand Down Expand Up @@ -29,6 +30,8 @@ export default class ThunderstorePackages {
.map(ThunderstoreMod.parseFromThunderstoreData)
.filter((mod) => !ThunderstorePackages.EXCLUSIONS.includes(mod.getFullName()));

ModBridge.clearCache();

ThunderstorePackages.PACKAGES_MAP = ThunderstorePackages.PACKAGES.reduce((map, pkg) => {
map.set(pkg.getFullName(), pkg);
return map;
Expand Down
58 changes: 49 additions & 9 deletions src/r2mm/mods/ModBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,25 @@ import ThunderstoreVersion from '../../model/ThunderstoreVersion';
import ManifestV2 from '../../model/ManifestV2';
import ThunderstorePackages from '../data/ThunderstorePackages';

interface CachedMod {
tsMod: ThunderstoreMod | undefined;
isLatest: boolean;
}

interface ModCache {
[key: string]: CachedMod;
}

export default class ModBridge {
private static CACHE: ModCache = {}

public static getLatestVersion(mod: ManifestV2, modList: ThunderstoreMod[]): ThunderstoreVersion | void {
public static getLatestVersion(mod: ManifestV2, modList: ThunderstoreMod[]): ThunderstoreVersion | undefined {
const matchingMod: ThunderstoreMod | undefined = modList.find((tsMod: ThunderstoreMod) => tsMod.getFullName() === mod.getName());
if (matchingMod === undefined) {
return;
}
// Compare version numbers and reduce.
return matchingMod.getVersions().reduce((v1: ThunderstoreVersion, v2: ThunderstoreVersion) => {
if (v1.getVersionNumber().isNewerThan(v2.getVersionNumber())) {
return v1;
}
return v2;
});
return matchingMod.getVersions().reduce(reduceToNewestVersion);
}

public static getThunderstoreModFromMod(mod: ManifestV2, modList: ThunderstoreMod[]): ThunderstoreMod | undefined {
Expand All @@ -27,10 +32,45 @@ export default class ModBridge {
const mod: ManifestV2 = new ManifestV2().fromReactive(vueMod);
const latestVersion: ThunderstoreVersion | void = ModBridge.getLatestVersion(mod, ThunderstorePackages.PACKAGES);
if (latestVersion instanceof ThunderstoreVersion) {
return mod.getVersionNumber()
.isEqualTo(latestVersion.getVersionNumber()) || mod.getVersionNumber().isNewerThan(latestVersion.getVersionNumber());
return mod.getVersionNumber().isEqualOrNewerThan(latestVersion.getVersionNumber());
}
return true;
}
Comment on lines 32 to 38
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this entire function be removed? Doesn't seem to be used outside of a single place now


private static getCached(mod: ManifestV2): CachedMod {
const cacheKey = `${mod.getName()}-${mod.getVersionNumber()}`;

if (ModBridge.CACHE[cacheKey] === undefined) {
const tsMod = ThunderstorePackages.PACKAGES.find((tsMod) => tsMod.getFullName() === mod.getName());

if (tsMod === undefined) {
ModBridge.CACHE[cacheKey] = { tsMod: undefined, isLatest: true };
} else {
const latestVersion = tsMod.getVersions().reduce(reduceToNewestVersion);
const isLatest = mod.getVersionNumber().isEqualOrNewerThan(latestVersion.getVersionNumber());
ModBridge.CACHE[cacheKey] = { tsMod, isLatest };
}
}

return ModBridge.CACHE[cacheKey];
}

public static getCachedThunderstoreModFromMod(mod: ManifestV2): ThunderstoreMod | undefined {
return ModBridge.getCached(mod).tsMod;
}

public static isCachedLatestVersion(mod: ManifestV2): boolean {
return ModBridge.getCached(mod).isLatest;
}

public static clearCache() {
ModBridge.CACHE = {};
}
}

const reduceToNewestVersion = (v1: ThunderstoreVersion, v2: ThunderstoreVersion) => {
if (v1.getVersionNumber().isNewerThan(v2.getVersionNumber())) {
return v1;
}
return v2;
};