Skip to content

Commit 1d8a231

Browse files
committed
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 54ee8cb commit 1d8a231

File tree

7 files changed

+71
-3
lines changed

7 files changed

+71
-3
lines changed

doc/api/process.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,6 +1159,9 @@ added: v0.7.6
11591159
* `time` {integer[]} The result of a previous call to `process.hrtime()`
11601160
* Returns: {integer[]}
11611161

1162+
This is the legacy version of [`process.hrtime.bigint()`][]
1163+
before `bigint` was introduced in JavaScript.
1164+
11621165
The `process.hrtime()` method returns the current high-resolution real time
11631166
in a `[seconds, nanoseconds]` tuple `Array`, where `nanoseconds` is the
11641167
remaining part of the real time that can't be represented in second precision.
@@ -1187,6 +1190,33 @@ setTimeout(() => {
11871190
}, 1000);
11881191
```
11891192

1193+
## process.hrtime.bigint()
1194+
<!-- YAML
1195+
added: REPLACEME
1196+
-->
1197+
1198+
* Returns: {bigint}
1199+
1200+
The `bigint` version of the [`process.hrtime()`][] method returning the
1201+
current high-resolution real time in a `bigint`.
1202+
1203+
Unlike [`process.hrtime()`][], it does not support an additional `time`
1204+
argument since the difference can just be computed directly
1205+
by subtraction of the two `bigint`s.
1206+
1207+
```js
1208+
const start = process.hrtime.bigint();
1209+
// 191051479007711n
1210+
1211+
setTimeout(() => {
1212+
const end = process.hrtime.bigint();
1213+
// 191052633396993n
1214+
1215+
console.log(`Benchmark took ${end - start} nanoseconds`);
1216+
// Benchmark took 1154389282 nanoseconds
1217+
}, 1000);
1218+
```
1219+
11901220
## process.initgroups(user, extraGroup)
11911221
<!-- YAML
11921222
added: v0.9.4
@@ -2030,6 +2060,8 @@ cases:
20302060
[`process.execPath`]: #process_process_execpath
20312061
[`process.exit()`]: #process_process_exit_code
20322062
[`process.exitCode`]: #process_process_exitcode
2063+
[`process.hrtime()`]: #process_process_hrtime_time
2064+
[`process.hrtime.bigint()`]: #process_process_hrtime_bigint
20332065
[`process.kill()`]: #process_process_kill_pid_signal
20342066
[`process.setUncaughtExceptionCaptureCallback()`]: process.html#process_process_setuncaughtexceptioncapturecallback_fn
20352067
[`promise.catch()`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch

lib/internal/bootstrap/node.js

Lines changed: 3 additions & 2 deletions
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 },
@@ -70,7 +71,7 @@
7071
NODE_PERFORMANCE_MILESTONE_BOOTSTRAP_COMPLETE,
7172
} = perf.constants;
7273

73-
_process.setup_hrtime(_hrtime);
74+
_process.setup_hrtime(_hrtime, _hrtimeBigInt);
7475
_process.setup_cpuUsage(_cpuUsage);
7576
_process.setupMemoryUsage(_memoryUsage);
7677
_process.setupKillAndExit();

lib/internal/process.js

Lines changed: 9 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,7 @@ void Chdir(const v8::FunctionCallbackInfo<v8::Value>& args);
905905
void CPUUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
906906
void Cwd(const v8::FunctionCallbackInfo<v8::Value>& args);
907907
void Hrtime(const v8::FunctionCallbackInfo<v8::Value>& args);
908+
void HrtimeBigInt(const v8::FunctionCallbackInfo<v8::Value>& args);
908909
void Kill(const v8::FunctionCallbackInfo<v8::Value>& args);
909910
void MemoryUsage(const v8::FunctionCallbackInfo<v8::Value>& args);
910911
void RawDebug(const v8::FunctionCallbackInfo<v8::Value>& args);

src/node_process.cc

Lines changed: 11 additions & 0 deletions
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;
@@ -113,7 +114,11 @@ void Cwd(const FunctionCallbackInfo<Value>& args) {
113114
args.GetReturnValue().Set(cwd);
114115
}
115116

117+
116118
// Hrtime exposes libuv's uv_hrtime() high-resolution timer.
119+
120+
// This is the legacy version of hrtime before BigInt was introduced in
121+
// JavaScript.
117122
// The value returned by uv_hrtime() is a 64-bit int representing nanoseconds,
118123
// so this function instead fills in an Uint32Array with 3 entries,
119124
// to avoid any integer overflow possibility.
@@ -132,6 +137,12 @@ void Hrtime(const FunctionCallbackInfo<Value>& args) {
132137
fields[2] = t % NANOS_PER_SEC;
133138
}
134139

140+
void HrtimeBigInt(const FunctionCallbackInfo<Value>& args) {
141+
Local<ArrayBuffer> ab = args[0].As<BigUint64Array>()->Buffer();
142+
uint64_t* fields = static_cast<uint64_t*>(ab->GetContents().Data());
143+
fields[0] = uv_hrtime();
144+
}
145+
135146
void Kill(const FunctionCallbackInfo<Value>& args) {
136147
Environment* env = Environment::GetCurrent(args);
137148

Lines changed: 14 additions & 0 deletions
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)