diff options
| author | 2023-07-26 21:35:49 -0700 | |
|---|---|---|
| committer | 2023-07-26 21:35:49 -0700 | |
| commit | 86633e0af47dc9ff62e9cef23962a855d061bde8 (patch) | |
| tree | 991683656af75e39cd041b4631d6b0d196c77a44 /src/js | |
| parent | ec2cf38ad85d05bb15080d88febd2476548987c8 (diff) | |
| download | bun-86633e0af47dc9ff62e9cef23962a855d061bde8.tar.gz bun-86633e0af47dc9ff62e9cef23962a855d061bde8.tar.zst bun-86633e0af47dc9ff62e9cef23962a855d061bde8.zip | |
Start time performance improvements to build tools (#3797)
* Make os.cpus() faster on Linux
* Fix crash
See https://github.com/ziglang/zig/issues/16540
* Handle watcher_count == 0
* Add assertion
* Clean up lifetimes of fs watcher a little
* :scissors:
* Use `errdefer`
* Make the error better
* Make os.cpus() more lazy
* Please don't translate-c on the entire C standard library
* immediately closing works correctly is still bug
* ops
* fmt+fixeup
* add back verbose
* free instead of destroy
* remove destroy option for watcher tasks
* flush verbose and add debug log
* fixup files
* use log for debug
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: cirospaciari <ciro.spaciari@gmail.com>
Diffstat (limited to 'src/js')
| -rw-r--r-- | src/js/node/os.js | 64 | ||||
| -rw-r--r-- | src/js/out/modules/node/os.js | 55 |
2 files changed, 116 insertions, 3 deletions
diff --git a/src/js/node/os.js b/src/js/node/os.js index 2ff98beea..40250ef9a 100644 --- a/src/js/node/os.js +++ b/src/js/node/os.js @@ -14,10 +14,72 @@ export var tmpdir = function () { return tmpdir(); }; +// os.cpus() is super expensive +// Specifically: getting the CPU speed on Linux is very expensive +// Some packages like FastGlob only bother to read the length of the array +// so instead of actually populating the entire object +// we turn them into getters +function lazyCpus({ cpus }) { + return () => { + const array = new Array(navigator.hardwareConcurrency); + function populate() { + const results = cpus(); + const length = results.length; + array.length = length; + for (let i = 0; i < length; i++) { + array[i] = results[i]; + } + } + + for (let i = 0; i < array.length; i++) { + // This is technically still observable via + // Object.getOwnPropertyDescriptors(), but it should be okay. + const instance = { + get model() { + if (array[i] === instance) populate(); + return array[i].model; + }, + set model(value) { + if (array[i] === instance) populate(); + array[i].model = value; + }, + + get speed() { + if (array[i] === instance) populate(); + return array[i].speed; + }, + + set speed(value) { + if (array[i] === instance) populate(); + array[i].speed = value; + }, + + get times() { + if (array[i] === instance) populate(); + return array[i].times; + }, + set times(value) { + if (array[i] === instance) populate(); + array[i].times = value; + }, + + toJSON() { + if (array[i] === instance) populate(); + return array[i]; + }, + }; + + array[i] = instance; + } + + return array; + }; +} + function bound(obj) { return { arch: obj.arch.bind(obj), - cpus: obj.cpus.bind(obj), + cpus: lazyCpus(obj), endianness: obj.endianness.bind(obj), freemem: obj.freemem.bind(obj), getPriority: obj.getPriority.bind(obj), diff --git a/src/js/out/modules/node/os.js b/src/js/out/modules/node/os.js index cc457e06d..005e4df66 100644 --- a/src/js/out/modules/node/os.js +++ b/src/js/out/modules/node/os.js @@ -1,7 +1,58 @@ -var bound = function(obj) { +var lazyCpus = function({ cpus }) { + return () => { + const array = new Array(navigator.hardwareConcurrency); + function populate() { + const results = cpus(), length = results.length; + array.length = length; + for (let i = 0;i < length; i++) + array[i] = results[i]; + } + for (let i = 0;i < array.length; i++) { + const instance = { + get model() { + if (array[i] === instance) + populate(); + return array[i].model; + }, + set model(value) { + if (array[i] === instance) + populate(); + array[i].model = value; + }, + get speed() { + if (array[i] === instance) + populate(); + return array[i].speed; + }, + set speed(value) { + if (array[i] === instance) + populate(); + array[i].speed = value; + }, + get times() { + if (array[i] === instance) + populate(); + return array[i].times; + }, + set times(value) { + if (array[i] === instance) + populate(); + array[i].times = value; + }, + toJSON() { + if (array[i] === instance) + populate(); + return array[i]; + } + }; + array[i] = instance; + } + return array; + }; +}, bound = function(obj) { return { arch: obj.arch.bind(obj), - cpus: obj.cpus.bind(obj), + cpus: lazyCpus(obj), endianness: obj.endianness.bind(obj), freemem: obj.freemem.bind(obj), getPriority: obj.getPriority.bind(obj), |
