Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process: implement process.hrtime.bigint() #21256

Closed
wants to merge 2 commits into from

Conversation

joyeecheung
Copy link
Member

@joyeecheung joyeecheung commented Jun 11, 2018

The first commit is from #21255

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. labels Jun 11, 2018
@joyeecheung
Copy link
Member Author

message: 'The "time" argument must be of type bigint. Received type object'
});

// The default behavior, return an Array "tuple" of numbers
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, copy-paste error..

Copy link
Member

@devsnek devsnek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it still a perf boost to use the ArrayBuffer when it's just one value?

@joyeecheung
Copy link
Member Author

joyeecheung commented Jun 11, 2018

@devsnek I don't think it makes much difference, probably can save an allocation depending on the situation though.

@joyeecheung joyeecheung added the semver-minor PRs that contain new features and should be released in the next minor version. label Jun 11, 2018

setTimeout(() => {
const diff = process.hrtime.bigint(time);
// 1552n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this supposed to be 1000000552n?


If `time` is specified, it must be the result of a previous
`process.hrtime.bigint()` call. The returned result will be the difference
between the current time and the specified time.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While useful for the array-based method, I don’t think this feature is that useful now one can just subtract the times directly…

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TimothyGu Yeah come to think of it, it does seem unnecessary. I'll remove it.

src/node.cc Outdated
void HrtimeBigInt(const FunctionCallbackInfo<Value>& args) {
Local<ArrayBuffer> ab = args[0].As<BigUint64Array>()->Buffer();
uint64_t* fields = static_cast<uint64_t*>(ab->GetContents().Data());
fields[0] = uv_hrtime();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d also appreciate some benchmarking showing that the ArrayBuffer-based approach is faster than just returning the bigint.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TimothyGu There are no APIs for constructing a BigInt out of a uint64_t at the moment, it seems.

Copy link
Member Author

@joyeecheung joyeecheung Jun 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could add one for uint64_t, but there is a discussion going on about the general API design of BigInt: https://bugs.chromium.org/p/v8/issues/detail?id=7712 A simple overload would be ambiguous anyway.


Unlike [`process.hrtime()`][], it does not support an additional `time`
argument since the difference can just be computed directly
by substraction of the two `bigint`s.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subtraction

// 191052633396993n

console.log(`Benchmark took ${end - start} nanoseconds`);
// benchmark took 1154389282 nanoseconds
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

benchmark -> Benchmark?

Copy link
Member

@apapirovski apapirovski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm +1 on the addition but I'm not a fan of the scoping. Has there been some discussion around this? Personally I'm not a huge fan of putting bigint functions on top of existing functions. The naming ends up being pretty non-obvious plus it's an extra property lookup. Even hrtimeBigInt is more appealing to me, personally.

@joyeecheung
Copy link
Member Author

@apapirovski The naming is following the convention of fs.realpath.native, this is the best I can think of in terms of readability since adding another overload to the process.hrtime([time]) would be quite confusing and unlike the fs.*stats methods, it is unlikely to need another variant (e.g. temporal) in the future.

The extra lookup can be avoided if the user assign the function to an identifier first (also if they are already using process.hrtime then the overhead of that extra lookup may be irrelevant considering process is already pretty complex).

Another idea would be to expose a symbol similar to util.promisify.custom but that may bring in too much redirection overhead and it's hard to get it play along with other symbols if we stick to that pattern.

@joyeecheung
Copy link
Member Author

For reference: there is also whatwg/webidl#525 and w3c/IndexedDB#231

cc @littledan do you have any suggestion regarding how to expose variants of existing APIs that support BigInt?

Copy link
Member

@TimothyGu TimothyGu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM other than the doc nit.

src/node.cc Outdated
@@ -1580,6 +1581,9 @@ static void Kill(const FunctionCallbackInfo<Value>& args) {
#define NANOS_PER_SEC 1000000000

// Hrtime exposes libuv's uv_hrtime() high-resolution timer.

// This is the legacy version of hrtime before BigInt was introduced in
// JavaScript.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, seeing this we probably want a note in the documentation for process.hrtime() that the BigInt equivalent is preferred.

@joyeecheung
Copy link
Member Author

Rebased and addressed nits: https://ci.nodejs.org/job/node-test-pull-request/15494/

@joyeecheung
Copy link
Member Author

CI is green. @apapirovski is your review in #21256 (review) blocking?

Copy link
Member

@apapirovski apapirovski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joyeecheung nope, it wasn't. :)

@joyeecheung
Copy link
Member Author

Landed in 1d8a231, thanks!

joyeecheung added a commit that referenced this pull request Jun 17, 2018
PR-URL: #21256
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
@targos
Copy link
Member

targos commented Jun 19, 2018

Depends on #21105 to land on v10.x-staging.

targos pushed a commit that referenced this pull request Jul 14, 2018
PR-URL: #21256
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Anatoli Papirovski <apapirovski@mac.com>
targos added a commit that referenced this pull request Jul 17, 2018
Notable changes:

* console:
  * The `console.timeLog()` method has been implemented. (#21312)
* deps:
  * Upgrade to libuv 1.22.0. (#21731)
  * Upgrade to ICU 62.1 (Unicode 11, CLDR 33.1). (#21728)
* http:
  * Added support for passing both `timeout` and `agent` options to
    `http.request`. (#21204)
* napi:
  * Added experimental support for functions dealing with bigint numbers. (#21226)
* process:
  * The `process.hrtime.bigint()` method has been implemented. (#21256)
  * Added the `--title` command line argument to set the process title on
    startup. (#21477)
* trace_events:
  * Added process_name metadata. (#21477)
@targos targos mentioned this pull request Jul 17, 2018
targos added a commit that referenced this pull request Jul 18, 2018
Notable changes:

* console:
  * The `console.timeLog()` method has been implemented. (#21312)
* deps:
  * Upgrade to libuv 1.22.0. (#21731)
  * Upgrade to ICU 62.1 (Unicode 11, CLDR 33.1). (#21728)
* http:
  * Added support for passing both `timeout` and `agent` options to
    `http.request`. (#21204)
* napi:
  * Added experimental support for functions dealing with bigint numbers. (#21226)
* process:
  * The `process.hrtime.bigint()` method has been implemented. (#21256)
  * Added the `--title` command line argument to set the process title on
    startup. (#21477)
* trace_events:
  * Added process_name metadata. (#21477)
* Added new collaborators
  * codebytere - Shelley Vohr

PR-URL: #21851
targos added a commit that referenced this pull request Jul 18, 2018
Notable changes:

* console:
  * The `console.timeLog()` method has been implemented.
    (#21312)
* deps:
  * Upgrade to libuv 1.22.0. (#21731)
  * Upgrade to ICU 62.1 (Unicode 11, CLDR 33.1).
    (#21728)
* http:
  * Added support for passing both `timeout` and `agent` options to
    `http.request`. (#21204)
* inspector:
  * Expose the original console API in `require('inspector').console`.
    (#21659)
* napi:
  * Added experimental support for functions dealing with bigint numbers.
    (#21226)
* process:
  * The `process.hrtime.bigint()` method has been implemented.
    (#21256)
  * Added the `--title` command line argument to set the process title on
    startup. (#21477)
* trace_events:
  * Added process_name metadata.
    (#21477)
* Added new collaborators
  * codebytere - Shelley Vohr

PR-URL: #21851
targos added a commit that referenced this pull request Jul 18, 2018
Notable changes:

* console:
  * The `console.timeLog()` method has been implemented.
    (#21312)
* deps:
  * Upgrade to libuv 1.22.0. (#21731)
  * Upgrade to ICU 62.1 (Unicode 11, CLDR 33.1).
    (#21728)
* http:
  * Added support for passing both `timeout` and `agent` options to
    `http.request`. (#21204)
* inspector:
  * Expose the original console API in `require('inspector').console`.
    (#21659)
* napi:
  * Added experimental support for functions dealing with bigint numbers.
    (#21226)
* process:
  * The `process.hrtime.bigint()` method has been implemented.
    (#21256)
  * Added the `--title` command line argument to set the process title on
    startup. (#21477)
* trace_events:
  * Added process_name metadata.
    (#21477)
* Added new collaborators
  * codebytere - Shelley Vohr

PR-URL: #21851
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. semver-minor PRs that contain new features and should be released in the next minor version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.