From a91b7c9251bdaffb262a9a6d08d3e752554c5b78 Mon Sep 17 00:00:00 2001 From: brunoais Date: Thu, 22 Feb 2024 19:40:07 +0000 Subject: [PATCH 1/2] Improving setEnvVariables() performance (1): Cache results --- src/common/classes/XDisplayRefreshRateController.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/common/classes/XDisplayRefreshRateController.ts b/src/common/classes/XDisplayRefreshRateController.ts index 67d99e5a..c514997d 100644 --- a/src/common/classes/XDisplayRefreshRateController.ts +++ b/src/common/classes/XDisplayRefreshRateController.ts @@ -23,6 +23,7 @@ import * as fs from "fs"; export class XDisplayRefreshRateController { private displayName: string; private isX11: boolean; + private dataIsFresh: boolean; private displayEnvVariable: string; private xAuthorityFile: string; public XDisplayRefreshRateController() { @@ -31,6 +32,9 @@ export class XDisplayRefreshRateController { } private setEnvVariables(): void { + if(this.dataIsFresh){ + return; + } const output = child_process .execSync( `ps -u $(id -u) -o pid= | xargs -I{} cat /proc/{}/environ 2>/dev/null | tr '\\0' '\\n'` @@ -66,6 +70,9 @@ export class XDisplayRefreshRateController { : sessionType === "wayland" ? false : undefined; + + this.dataIsFresh = true; + setTimeout(() => this.dataIsFresh = false, 60_000) } public getIsX11(): boolean { From 1603a2d5ebbef46590fecfbbd035cd8fa4f8576e Mon Sep 17 00:00:00 2001 From: brunoais Date: Thu, 22 Feb 2024 19:43:40 +0000 Subject: [PATCH 2/2] Improving setEnvVariables() performance (2): No need to list all processes. Just list some. With just 20 of the highest PID processes, the probability one has the intended data is quite high. Even then, after the intended data is available, stop reading input. --- .../classes/XDisplayRefreshRateController.ts | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/common/classes/XDisplayRefreshRateController.ts b/src/common/classes/XDisplayRefreshRateController.ts index c514997d..e5936a07 100644 --- a/src/common/classes/XDisplayRefreshRateController.ts +++ b/src/common/classes/XDisplayRefreshRateController.ts @@ -36,9 +36,18 @@ export class XDisplayRefreshRateController { return; } const output = child_process - .execSync( - `ps -u $(id -u) -o pid= | xargs -I{} cat /proc/{}/environ 2>/dev/null | tr '\\0' '\\n'` - ) + .execSync(` + ps -u $(id -u) -o pid= | \ + tail --lines 20 | \ + xargs -I{} cat /proc/{}/environ 2>/dev/null | \ + tr '\\0' '\\n' | \ + awk ' + /DISPLAY=/ && !countDisplay {print; countDisplay++} + /XAUTHORITY=/ && !countXAuthority {print; countXAuthority++} + /XDG_SESSION_TYPE=/ && !countSessionType {print; countSessionType++} + {if (countDisplay && countXAuthority && countSessionType) exit} + ' + `) .toString(); const displayMatch = output.match(/^DISPLAY=(.*)$/m);