mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-12 17:28:31 +03:00
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:
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user