Experimental CPU Optimize Techniques
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
import fs from 'fs'
|
||||
|
||||
const READ_BUF = Buffer.alloc(65536)
|
||||
const fdCache = new Map()
|
||||
|
||||
export function readFileCached(path) {
|
||||
let entry = fdCache.get(path)
|
||||
if (entry === undefined) {
|
||||
try {
|
||||
const fd = fs.openSync(path, 'r')
|
||||
entry = { fd }
|
||||
fdCache.set(path, entry)
|
||||
} catch {
|
||||
fdCache.set(path, null)
|
||||
return null
|
||||
}
|
||||
}
|
||||
if (!entry) return null
|
||||
try {
|
||||
const bytesRead = fs.readSync(entry.fd, READ_BUF, 0, READ_BUF.length, 0)
|
||||
return READ_BUF.toString('utf8', 0, bytesRead)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function closeFdCache() {
|
||||
for (const [path, entry] of fdCache) {
|
||||
if (entry) {
|
||||
try { fs.closeSync(entry.fd) } catch {}
|
||||
}
|
||||
}
|
||||
fdCache.clear()
|
||||
}
|
||||
Reference in New Issue
Block a user