Skip to content

Commit f1b18ba

Browse files
joyeecheungtargos
authored andcommitted
process: implement process.hrtime.bigint()
PR-URL: #21256 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]>
1 parent a68b7dd commit f1b18ba

File tree

7 files changed

+71
-3
lines changed

7 files changed

+71
-3
lines changed

doc/api/process.md

+32
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,9 @@ added: v0.7.6
11631163
* `time` {integer[]} The result of a previous call to `process.hrtime()`
11641164
* Returns: {integer[]}
11651165

1166+
This is the legacy version of [`process.hrtime.bigint()`][]
1167+
before `bigint` was introduced in JavaScript.
1168+
11661169
The `process.hrtime()` method returns the current high-resolution real time
11671170
in a `[seconds, nanoseconds]` tuple `Array`, where `nanoseconds` is the
11681171
remaining part of the real time that can't be represented in second precision.
@@ -1191,6 +1194,33 @@ setTimeout(() => {
11911194
}, 1000);
11921195
```
11931196

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+
11941224
## process.initgroups(user, extraGroup)
11951225
<!-- YAML
11961226
added: v0.9.4
@@ -2035,6 +2065,8 @@ cases:
20352065
[`process.execPath`]: #process_process_execpath
20362066
[`process.exit()`]: #process_process_exit_code
20372067
[`process.exitCode`]: #process_process_exitcode
2068+
[`process.hrtime()`]: #process_process_hrtime_time
2069+
[`process.hrtime.bigint()`]: #process_process_hrtime_bigint
20382070
[`process.kill()`]: #process_process_kill_pid_signal
20392071
[`process.setUncaughtExceptionCaptureCallback()`]: process.html#process_process_setuncaughtexceptioncapturecallback_fn
20402072
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch

lib/internal/bootstrap/node.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// object.
1919
{ _setupProcessObject, _setupNextTick,
2020
_setupPromises, _chdir, _cpuUsage,
21-
_hrtime, _memoryUsage, _rawDebug,
21+
_hrtime, _hrtimeBigInt,
22+
_memoryUsage, _rawDebug,
2223
_umask, _initgroups, _setegid, _seteuid,
2324
_setgid, _setuid, _setgroups,
2425
_shouldAbortOnUncaughtToggle },
@@ -66,7 +67,7 @@
6667
NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE,
6768
} = perf.constants;
6869

69-
_process.setup_hrtime(_hrtime);
70+
_process.setup_hrtime(_hrtime, _hrtimeBigInt);
7071
_process.setup_cpuUsage(_cpuUsage);
7172
_process.setupMemoryUsage(_memoryUsage);
7273
_process.setupKillAndExit();

lib/internal/process.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function setup_cpuUsage(_cpuUsage) {
9090
// The 3 entries filled in by the original process.hrtime contains
9191
// the upper/lower 32 bits of the second part of the value,
9292
// and the remaining nanoseconds of the value.
93-
function setup_hrtime(_hrtime) {
93+
function setup_hrtime(_hrtime, _hrtimeBigInt) {
9494
const hrValues = new Uint32Array(3);
9595

9696
process.hrtime = function hrtime(time) {
@@ -115,6 +115,14 @@ function setup_hrtime(_hrtime) {
115115
hrValues[2]
116116
];
117117
};
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+
};
118126
}
119127

120128
function setupMemoryUsage(_memoryUsage) {

src/bootstrapper.cc

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ void SetupBootstrapObject(Environment* env,
109109
BOOTSTRAP_METHOD(_chdir, Chdir);
110110
BOOTSTRAP_METHOD(_cpuUsage, CPUUsage);
111111
BOOTSTRAP_METHOD(_hrtime, Hrtime);
112+
BOOTSTRAP_METHOD(_hrtimeBigInt, HrtimeBigInt);
112113
BOOTSTRAP_METHOD(_memoryUsage, MemoryUsage);
113114
BOOTSTRAP_METHOD(_rawDebug, RawDebug);
114115
BOOTSTRAP_METHOD(_umask, Umask);

src/node_internals.h

+1
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,7 @@ void Chdir(const v8::FunctionCallbackInfo<v8::Value>& args);
939939
void CPUUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
940940
void Cwd(const v8::FunctionCallbackInfo<v8::Value>& args);
941941
void Hrtime(const v8::FunctionCallbackInfo<v8::Value>& args);
942+
void HrtimeBigInt(const v8::FunctionCallbackInfo<v8::Value>& args);
942943
void Kill(const v8::FunctionCallbackInfo<v8::Value>& args);
943944
void MemoryUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
944945
void RawDebug(const v8::FunctionCallbackInfo<v8::Value>& args);

src/node_process.cc

+11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ namespace node {
3131

3232
using v8::Array;
3333
using v8::ArrayBuffer;
34+
using v8::BigUint64Array;
3435
using v8::Float64Array;
3536
using v8::FunctionCallbackInfo;
3637
using v8::HeapStatistics;
@@ -114,7 +115,11 @@ void Cwd(const FunctionCallbackInfo<Value>& args) {
114115
args.GetReturnValue().Set(cwd);
115116
}
116117

118+
117119
// 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.
118123
// The value returned by uv_hrtime() is a 64-bit int representing nanoseconds,
119124
// so this function instead fills in an Uint32Array with 3 entries,
120125
// to avoid any integer overflow possibility.
@@ -133,6 +138,12 @@ void Hrtime(const FunctionCallbackInfo<Value>& args) {
133138
fields[2] = t % NANOS_PER_SEC;
134139
}
135140

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+
136147
void Kill(const FunctionCallbackInfo<Value>& args) {
137148
Environment* env = Environment::GetCurrent(args);
138149

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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);

0 commit comments

Comments
 (0)