refactor: improve performance of song lookups

use lookup tables to optimise looking up songs; the former solution
was O(n^2), this one is O(1) with a small hit to memory.
This commit is contained in:
zkldi
2022-09-04 20:03:31 +01:00
parent c560df37d8
commit 67485e9837
2 changed files with 16 additions and 4 deletions
+10
View File
@@ -253,3 +253,13 @@ export function FormatSieglindePMS(sgl: number): string {
return `●${(sgl - 12).toFixed(2)}`;
}
export function CreateSongMap<G extends Game = Game>(songs: Array<SongDocument<G>>) {
const songMap = new Map<integer, SongDocument<G>>();
for (const song of songs) {
songMap.set(song.id, song);
}
return songMap;
}
@@ -1,6 +1,8 @@
import { Router } from "express";
import db from "external/mongo/db";
import CreateLogCtx from "lib/logger/logger";
import { CreateSongMap } from "tachi-common";
import { integer, SongDocument } from "tachi-common/types";
import { GetFolderCharts, GetFoldersFromTable } from "utils/folder";
import { GetTachiData } from "utils/req-tachi-data";
import type { Request, Response } from "express";
@@ -85,10 +87,10 @@ CreateAndMountTable(
for (const folder of folders) {
const data = await GetFolderCharts(folder, {}, true);
const charts = data.charts as Array<ChartDocument<"bms:7K">>;
const songs = data.songs;
const songMap = CreateSongMap(data.songs);
for (const chart of charts) {
const song = songs.find((song) => song.id === chart.songID);
const song = songMap.get(chart.songID);
if (!song) {
response.push({
@@ -146,10 +148,10 @@ CreateAndMountTable(
for (const folder of folders) {
const data = await GetFolderCharts(folder, {}, true);
const charts = data.charts as Array<ChartDocument<"bms:7K">>;
const songs = data.songs;
const songMap = CreateSongMap(data.songs);
for (const chart of charts) {
const song = songs.find((song) => song.id === chart.songID);
const song = songMap.get(chart.songID);
if (!song) {
response.push({