1515 */
1616
1717import { execSync } from 'child_process' ;
18- import { existsSync } from 'fs' ;
18+ import fs from 'fs' ;
19+
20+ /** Cached output of `ldconfig -p` to avoid repeated subprocess calls. */
21+ let _ldconfigCache = null ;
1922
2023// ── CUDA (linux/x64) ─────────────────────────────────────────────────────────
2124
@@ -64,15 +67,17 @@ function cudaSearchDirs() {
6467 */
6568function findLib ( libName ) {
6669 for ( const dir of cudaSearchDirs ( ) ) {
67- if ( existsSync ( `${ dir } /${ libName } ` ) ) return `${ dir } /${ libName } ` ;
70+ if ( fs . existsSync ( `${ dir } /${ libName } ` ) ) return `${ dir } /${ libName } ` ;
6871 }
6972 try {
70- const output = execSync ( 'ldconfig -p' , {
71- stdio : [ 'ignore' , 'pipe' , 'ignore' ] ,
72- encoding : 'utf8' ,
73- timeout : 3000 ,
74- } ) ;
75- for ( const line of output . split ( '\n' ) ) {
73+ if ( _ldconfigCache === null ) {
74+ _ldconfigCache = execSync ( 'ldconfig -p' , {
75+ stdio : [ 'ignore' , 'pipe' , 'ignore' ] ,
76+ encoding : 'utf8' ,
77+ timeout : 3000 ,
78+ } ) ;
79+ }
80+ for ( const line of _ldconfigCache . split ( '\n' ) ) {
7681 if ( line . includes ( libName ) && line . includes ( '=>' ) ) {
7782 const match = line . match ( / = > \s * ( .+ ) / ) ;
7883 if ( match ) return match [ 1 ] . trim ( ) ;
@@ -91,8 +96,8 @@ function findLib(libName) {
9196 * @returns {Promise<void> }
9297 * @throws {Error } If NVIDIA GPU is not detected or required CUDA libraries are missing.
9398 */
94- async function activateCuda ( ) {
95- if ( ! existsSync ( '/dev/nvidiactl' ) ) {
99+ function activateCuda ( ) {
100+ if ( ! fs . existsSync ( '/dev/nvidiactl' ) ) {
96101 throw new Error (
97102 'No NVIDIA GPU detected (/dev/nvidiactl not found).\n' +
98103 'Ensure NVIDIA drivers are installed. Verify with: nvidia-smi' ,
@@ -124,7 +129,7 @@ async function activateCuda() {
124129 * @returns {Promise<void> }
125130 * @throws {Error } If not running on Windows.
126131 */
127- async function activateDml ( ) {
132+ function activateDml ( ) {
128133 if ( process . platform !== 'win32' ) {
129134 throw new Error (
130135 `DirectML is only available on Windows (current platform: ${ process . platform } ).` ,
0 commit comments