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

feat: improved v4 performance #435

Merged
merged 4 commits into from May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions README_js.md
Expand Up @@ -8,9 +8,8 @@ runmd.onRequire = (path) => {
runmd.Date.now = () => 1551914748172;

let seed = 0xdefaced;
require('crypto').randomBytes = function () {
const a = [];
for (let i = 0; i < 16; i++) a.push((seed = (seed * 0x41a7) & 0x7fffffff) & 0xff);
require('crypto').randomFillSync = function (a) {
for (let i = 0; i < 16; i++) a[i] = (seed = (seed * 0x41a7) & 0x7fffffff) & 0xff;
return a;
};
```
Expand Down
4 changes: 3 additions & 1 deletion src/rng.js
@@ -1,5 +1,7 @@
import crypto from 'crypto';

const rnds8 = new Uint8Array(16);

export default function rng() {
return crypto.randomBytes(16);
return crypto.randomFillSync(rnds8);
}
14 changes: 8 additions & 6 deletions src/v4.js
Expand Up @@ -2,10 +2,8 @@ import rng from './rng.js';
import bytesToUuid from './bytesToUuid.js';

function v4(options, buf, offset) {
const i = (buf && offset) || 0;

if (typeof options === 'string') {
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to finally get rid of this legacy old API:

uuidv4('binary');

Out of scope for this PR, but I've taken note in #437.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ctavan ok, I will do it a bit later (deprecated)

buf = options === 'binary' ? new Uint32Array(16) : null;
buf = options === 'binary' ? new Uint8Array(16) : null;
options = null;
}

Expand All @@ -19,12 +17,16 @@ function v4(options, buf, offset) {

// Copy bytes to buffer, if provided
if (buf) {
for (let ii = 0; ii < 16; ++ii) {
buf[i + ii] = rnds[ii];
const start = offset || 0;

for (let i = 0; i < 16; ++i) {
buf[start + i] = rnds[i];
}

return buf;
}

return buf || bytesToUuid(rnds);
return bytesToUuid(rnds);
Copy link
Member

@broofa broofa May 5, 2020

Choose a reason for hiding this comment

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

Gotta return buf if it's passed in. (This should have been caught by unit tests. Can you add a test to check this?)

Suggested change
return bytesToUuid(rnds);
return buf || bytesToUuid(rnds);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@broofa sure, I can add test for this case.

But the code can be seen that the buffer is returned after it is filled:
https://github.com/uuidjs/uuid/pull/435/files#diff-8988f35cbc1e6b5ea7f9cb52a4bfa0f2R26

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

@awwit I just realized that we currently do two things: fill the passed buffer and return it. But we only test for filling, not for the returned buffer.

Since we don't know if anybody relies on the fact, that our methods fill the provided buffer and return it, it would be a good opportunity to add another test case that test for the return value when a buffer is passed.

}

export default v4;
3 changes: 2 additions & 1 deletion test/unit/v4.test.js
Expand Up @@ -45,13 +45,14 @@ describe('v4', () => {

test('fills one UUID into a buffer as expected', () => {
const buffer = [];
v4(
const result = v4(
{
random: randomBytesFixture,
},
buffer,
);
assert.deepEqual(buffer, expectedBytes);
assert.strictEqual(buffer, result);
});

test('fills two UUIDs into a buffer as expected', () => {
Expand Down