GeezerVison is showing github.com — process: implement process.hrtime.bigint() · nodejs/node@f1b18ba
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/process.md +32Lines changed: 32 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -1163,6 +1163,9 @@ added: v0.7.6 | 1163 | 1163 | * `time` {integer[]} The result of a previous call to `process.hrtime()` | 1164 | 1164 | * Returns: {integer[]} | 1165 | 1165 |
| | 1166 | +This is the legacy version of [`process.hrtime.bigint()`][] | | 1167 | +before `bigint` was introduced in JavaScript. | | 1168 | + | 1166 | 1169 | The `process.hrtime()` method returns the current high-resolution real time | 1167 | 1170 | in a `[seconds, nanoseconds]` tuple `Array`, where `nanoseconds` is the | 1168 | 1171 | remaining part of the real time that can't be represented in second precision. | @@ -1191,6 +1194,33 @@ setTimeout(() => { | 1191 | 1194 | }, 1000); | 1192 | 1195 | ``` | 1193 | 1196 |
| | 1197 | +## process.hrtime.bigint() | | 1198 | +<!-- YAML | | 1199 | +added: REPLACEME | | 1200 | +--> | | 1201 | + | | 1202 | +* Returns: {bigint} | | 1203 | + | | 1204 | +The `bigint` version of the [`process.hrtime()`][] method returning the | | 1205 | +current high-resolution real time in a `bigint`. | | 1206 | + | | 1207 | +Unlike [`process.hrtime()`][], it does not support an additional `time` | | 1208 | +argument since the difference can just be computed directly | | 1209 | +by subtraction of the two `bigint`s. | | 1210 | + | | 1211 | +```js | | 1212 | +const start = process.hrtime.bigint(); | | 1213 | +// 191051479007711n | | 1214 | + | | 1215 | +setTimeout(() => { | | 1216 | + const end = process.hrtime.bigint(); | | 1217 | + // 191052633396993n | | 1218 | + | | 1219 | + console.log(`Benchmark took ${end - start} nanoseconds`); | | 1220 | + // Benchmark took 1154389282 nanoseconds | | 1221 | +}, 1000); | | 1222 | +``` | | 1223 | + | 1194 | 1224 | ## process.initgroups(user, extraGroup) | 1195 | 1225 | <!-- YAML | 1196 | 1226 | added: v0.9.4 | @@ -2035,6 +2065,8 @@ cases: | 2035 | 2065 | [`process.execPath`]: #process_process_execpath | 2036 | 2066 | [`process.exit()`]: #process_process_exit_code | 2037 | 2067 | [`process.exitCode`]: #process_process_exitcode | | 2068 | +[`process.hrtime()`]: #process_process_hrtime_time | | 2069 | +[`process.hrtime.bigint()`]: #process_process_hrtime_bigint | 2038 | 2070 | [`process.kill()`]: #process_process_kill_pid_signal | 2039 | 2071 | [`process.setUncaughtExceptionCaptureCallback()`]: process.html#process_process_setuncaughtexceptioncapturecallback_fn | 2040 | 2072 | [`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/bootstrap/node.js +3-2Lines changed: 3 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 18 | 18 | // object. | 19 | 19 | { _setupProcessObject, _setupNextTick, | 20 | 20 | _setupPromises, _chdir, _cpuUsage, | 21 | | - _hrtime, _memoryUsage, _rawDebug, | | 21 | + _hrtime, _hrtimeBigInt, | | 22 | + _memoryUsage, _rawDebug, | 22 | 23 | _umask, _initgroups, _setegid, _seteuid, | 23 | 24 | _setgid, _setuid, _setgroups, | 24 | 25 | _shouldAbortOnUncaughtToggle }, | | 66 | 67 | NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE, | 67 | 68 | } = perf.constants; | 68 | 69 |
| 69 | | - _process.setup_hrtime(_hrtime); | | 70 | + _process.setup_hrtime(_hrtime, _hrtimeBigInt); | 70 | 71 | _process.setup_cpuUsage(_cpuUsage); | 71 | 72 | _process.setupMemoryUsage(_memoryUsage); | 72 | 73 | _process.setupKillAndExit(); | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/process.js +9-1Lines changed: 9 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -90,7 +90,7 @@ function setup_cpuUsage(_cpuUsage) { | 90 | 90 | // The 3 entries filled in by the original process.hrtime contains | 91 | 91 | // the upper/lower 32 bits of the second part of the value, | 92 | 92 | // and the remaining nanoseconds of the value. | 93 | | -function setup_hrtime(_hrtime) { | | 93 | +function setup_hrtime(_hrtime, _hrtimeBigInt) { | 94 | 94 | const hrValues = new Uint32Array(3); | 95 | 95 |
| 96 | 96 | process.hrtime = function hrtime(time) { | @@ -115,6 +115,14 @@ function setup_hrtime(_hrtime) { | 115 | 115 | hrValues[2] | 116 | 116 | ]; | 117 | 117 | }; | | 118 | + | | 119 | + // Use a BigUint64Array in the closure because V8 does not have an API for | | 120 | + // creating a BigInt out of a uint64_t yet. | | 121 | + const hrBigintValues = new BigUint64Array(1); | | 122 | + process.hrtime.bigint = function() { | | 123 | + _hrtimeBigInt(hrBigintValues); | | 124 | + return hrBigintValues[0]; | | 125 | + }; | 118 | 126 | } | 119 | 127 |
| 120 | 128 | function setupMemoryUsage(_memoryUsage) { | |
Collapse file Copy file name to clipboardExpand all lines: src/bootstrapper.cc +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -109,6 +109,7 @@ void SetupBootstrapObject(Environment* env, | 109 | 109 | BOOTSTRAP_METHOD(_chdir, Chdir); | 110 | 110 | BOOTSTRAP_METHOD(_cpuUsage, CPUUsage); | 111 | 111 | BOOTSTRAP_METHOD(_hrtime, Hrtime); | | 112 | + BOOTSTRAP_METHOD(_hrtimeBigInt, HrtimeBigInt); | 112 | 113 | BOOTSTRAP_METHOD(_memoryUsage, MemoryUsage); | 113 | 114 | BOOTSTRAP_METHOD(_rawDebug, RawDebug); | 114 | 115 | BOOTSTRAP_METHOD(_umask, Umask); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_internals.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -939,6 +939,7 @@ void Chdir(const v8::FunctionCallbackInfo<v8::Value>& args); | 939 | 939 | void CPUUsage(const v8::FunctionCallbackInfo<v8::Value>& args); | 940 | 940 | void Cwd(const v8::FunctionCallbackInfo<v8::Value>& args); | 941 | 941 | void Hrtime(const v8::FunctionCallbackInfo<v8::Value>& args); | | 942 | +void HrtimeBigInt(const v8::FunctionCallbackInfo<v8::Value>& args); | 942 | 943 | void Kill(const v8::FunctionCallbackInfo<v8::Value>& args); | 943 | 944 | void MemoryUsage(const v8::FunctionCallbackInfo<v8::Value>& args); | 944 | 945 | void RawDebug(const v8::FunctionCallbackInfo<v8::Value>& args); | |
Collapse file Copy file name to clipboardExpand all lines: src/node_process.cc +11Lines changed: 11 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -31,6 +31,7 @@ namespace node { | 31 | 31 |
| 32 | 32 | using v8::Array; | 33 | 33 | using v8::ArrayBuffer; | | 34 | +using v8::BigUint64Array; | 34 | 35 | using v8::Float64Array; | 35 | 36 | using v8::FunctionCallbackInfo; | 36 | 37 | using v8::HeapStatistics; | @@ -114,7 +115,11 @@ void Cwd(const FunctionCallbackInfo<Value>& args) { | 114 | 115 | args.GetReturnValue().Set(cwd); | 115 | 116 | } | 116 | 117 |
| | 118 | + | 117 | 119 | // Hrtime exposes libuv's uv_hrtime() high-resolution timer. | | 120 | + | | 121 | +// This is the legacy version of hrtime before BigInt was introduced in | | 122 | +// JavaScript. | 118 | 123 | // The value returned by uv_hrtime() is a 64-bit int representing nanoseconds, | 119 | 124 | // so this function instead fills in an Uint32Array with 3 entries, | 120 | 125 | // to avoid any integer overflow possibility. | @@ -133,6 +138,12 @@ void Hrtime(const FunctionCallbackInfo<Value>& args) { | 133 | 138 | fields[2] = t % NANOS_PER_SEC; | 134 | 139 | } | 135 | 140 |
| | 141 | +void HrtimeBigInt(const FunctionCallbackInfo<Value>& args) { | | 142 | + Local<ArrayBuffer> ab = args[0].As<BigUint64Array>()->Buffer(); | | 143 | + uint64_t* fields = static_cast<uint64_t*>(ab->GetContents().Data()); | | 144 | + fields[0] = uv_hrtime(); | | 145 | +} | | 146 | + | 136 | 147 | void Kill(const FunctionCallbackInfo<Value>& args) { | 137 | 148 | Environment* env = Environment::GetCurrent(args); | 138 | 149 |
| |
Collapse file Copy file name to clipboard +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +// Tests that process.hrtime.bigint() works. | | 4 | + | | 5 | +require('../common'); | | 6 | +const assert = require('assert'); | | 7 | + | | 8 | +const start = process.hrtime.bigint(); | | 9 | +assert.strictEqual(typeof start, 'bigint'); | | 10 | + | | 11 | +const end = process.hrtime.bigint(); | | 12 | +assert.strictEqual(typeof end, 'bigint'); | | 13 | + | | 14 | +assert(end - start >= 0n); |
|