Skip to content

Commit

Permalink
feat: Precache Database Option
Browse files Browse the repository at this point in the history
  • Loading branch information
colin969 committed Oct 28, 2023
1 parent 0963d74 commit 9c666de
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
"fallbackLanguageDesc": "Which language to use in place of an incomplete current language.",
"auto": "Auto ({0})",
"none": "None",
"precacheDatabase": "Precache Database",
"precacheDatabaseDesc": "Caches some database tables to memory on boot. Improves responsiveness on slower drives at the expense of additional memory usage and boot time. Requires Save & Restart to apply changes.",
"contentFiltersHeader": "Content Filters",
"flashpointHeader": "Flashpoint",
"flashpointPath": "Flashpoint Path",
Expand Down
5 changes: 5 additions & 0 deletions src/back/SocketServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,12 @@ export class SocketServer {

// Handle

const start = performance.now();
const [inc, out] = await api_handle_message(this.api, data, msg_event);
const end = performance.now();
if ('type' in data && data.type !== BackIn.KEEP_ALIVE) {
console.log(`${Math.floor(end - start)}ms - "${BackIn[data.type]}"`);
}

if (inc) {
const sent = client.sent.find(s => s.id === data.id);
Expand Down
11 changes: 11 additions & 0 deletions src/back/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { VERSION } from '@shared/version';
import {
createErrorProxy, deepCopy,
removeFileExtension,
sizeToString,
stringifyArray
} from '@shared/Util';
import { BackIn, BackInit, BackInitArgs, BackOut, BackResParams, ComponentState, ComponentStatus, DownloadDetails } from '@shared/back/types';
Expand Down Expand Up @@ -752,6 +753,16 @@ async function initialize() {
await AppDataSource.initialize();
// TypeORM forces on but breaks Playlist Game links to unimported games
await AppDataSource.query('PRAGMA foreign_keys=off;');
// Forcefully precache the tables into memory
if (state.config.precacheDatabase) {
const dbSize = await fs.promises.stat(databasePath).then((stats) => stats.size);
const start = performance.now();
await AppDataSource.query(`PRAGMA cache_size=${dbSize + 20000};`);
await AppDataSource.query('SELECT * FROM game;');
await AppDataSource.query('SELECT * FROM tag;');
const end = performance.now();
log.info('Launcher', `Database cache set to ${sizeToString(dbSize + 20000)} bytes in ${Math.floor(end - start)}ms`);
}
log.info('Launcher', 'Database connection established');
}

Expand Down
12 changes: 12 additions & 0 deletions src/renderer/components/pages/ConfigPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ type ConfigPageState = {
flashpointPath: string;
/** If the "use custom title bar" checkbox is checked. */
useCustomTitlebar: boolean;
precacheDatabase: boolean;
/** Currently editable Tag Filter Group */
editingTagFilterGroupIdx?: number;
editingTagFilterGroup?: TagFilterGroup;
Expand All @@ -94,6 +95,7 @@ export class ConfigPage extends React.Component<ConfigPageProps, ConfigPageState
useCustomTitlebar: configData.useCustomTitlebar,
editorOpen: false,
nukeInProgress: false,
precacheDatabase: configData.precacheDatabase,
};
}

Expand Down Expand Up @@ -201,6 +203,11 @@ export class ConfigPage extends React.Component<ConfigPageProps, ConfigPageState
value={this.props.preferencesData.currentLanguage || ''}
onChange={this.onCurrentLanguageSelect}
items={langOptions} />
<ConfigBoxCheckbox
title={strings.precacheDatabase}
description={strings.precacheDatabaseDesc}
checked={this.state.precacheDatabase}
onToggle={this.onPrecacheDatabaseToggle} />
</div>
</div>
{/* -- Content Filters -- */}
Expand Down Expand Up @@ -788,6 +795,10 @@ export class ConfigPage extends React.Component<ConfigPageProps, ConfigPageState
updatePreferencesData({ fancyAnimations: isChecked });
};

onPrecacheDatabaseToggle = (isChecked: boolean): void => {
this.setState({ precacheDatabase: isChecked });
};

onSearchLimitChange = (event: React.ChangeEvent<HTMLSelectElement>): void => {
updatePreferencesData({ searchLimit: num(event.target.value) });
};
Expand Down Expand Up @@ -1061,6 +1072,7 @@ export class ConfigPage extends React.Component<ConfigPageProps, ConfigPageState
window.Shared.back.request(BackIn.UPDATE_CONFIG, {
flashpointPath: this.state.flashpointPath,
useCustomTitlebar: this.state.useCustomTitlebar,
precacheDatabase: this.state.precacheDatabase,
}).then(() => { window.Shared.restart(); });
};

Expand Down
2 changes: 2 additions & 0 deletions src/shared/config/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const configDataDefaultBase: Readonly<AppConfigData> = Object.freeze({
gotdUrl: 'https://download.unstable.life/gotd.json',
gotdShowAll: false,
middlewareOverridePath: 'Legacy/middleware_overrides/',
precacheDatabase: false,
});

/**
Expand Down Expand Up @@ -81,6 +82,7 @@ export function overwriteConfigData(
parser.prop('gotdUrl', v => source.gotdUrl = str(v));
parser.prop('gotdShowAll', v => source.gotdShowAll = !!v);
parser.prop('middlewareOverridePath', v => source.middlewareOverridePath = str(v));
parser.prop('precacheDatabase', v => source.precacheDatabase = !!v);
// Do some alterations
source.flashpointPath = fixSlashes(source.flashpointPath); // (Clean path)
// Return
Expand Down
2 changes: 2 additions & 0 deletions src/shared/lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ const langTemplate = {
'fallbackLanguageDesc',
'auto',
'none',
'precacheDatabase',
'precacheDatabaseDesc',
'contentFiltersHeader',
'flashpointHeader',
'flashpointPath',
Expand Down
2 changes: 2 additions & 0 deletions typings/flashpoint-launcher.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,8 @@ declare module 'flashpoint-launcher' {
gotdShowAll: boolean;
/** Middleware override path */
middlewareOverridePath: string;
/** Whether to precache the Games and Tags tables in memory when booting */
precacheDatabase: boolean;
};

export type TagFilterGroup = {
Expand Down

0 comments on commit 9c666de

Please sign in to comment.