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

v8: refactor the v8 module #12681

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions benchmark/v8/get-stats.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
'use strict';

const common = require('../common.js');
const v8 = require('v8');

const bench = common.createBenchmark(main, {
method: [
'getHeapStatistics',
'getHeapSpaceStatistics'
],
n: [1e6],
flags: ['--ignition --turbo', '']
Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Apr 27, 2017

Choose a reason for hiding this comment

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

If I understand the guide right, this should be a property of the third argument (options). And flags do not vary, so empty flag seems to be useless. However, I may miss something.

Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Apr 27, 2017

Choose a reason for hiding this comment

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

Also I am not sure if the '--ignition --turbo' should be '--ignition', '--turbo'

Copy link
Member Author

Choose a reason for hiding this comment

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

Setting the flags like this runs the benchmark with two separate configurations, one with --ignition --turbo and the other without flags. I wasn't sure myself but this definitely worked when I ran it like this :-)

Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Apr 27, 2017

Choose a reason for hiding this comment

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

Are you sure the flags are really set, not only displayed in the config list? I've added a debug log:

'use strict';

const common = require('../common.js');
const v8 = require('v8');

const bench = common.createBenchmark(main, {
  method: [
    'getHeapStatistics',
    'getHeapSpaceStatistics'
  ],
  n: [1e6],
  flags: ['--ignition --turbo', '']
});

function main(conf) {
  console.log(process.execArgv);
  const n = +conf.n;
  const method = conf.method;
  var i = 0;
  bench.start();
  for (; i < n; i++)
    v8[method]();
  bench.end(n);
}
[]
v8\get-stats.js flags="--ignition --turbo" n=1000000 method="getHeapStatistics": 1,476,245.8999447592
[]
v8\get-stats.js flags="" n=1000000 method="getHeapStatistics": 1,455,785.4873297967
[]
v8\get-stats.js flags="--ignition --turbo" n=1000000 method="getHeapSpaceStatistics": 2,515,738.0104498775
[]
v8\get-stats.js flags="" n=1000000 method="getHeapSpaceStatistics": 2,505,423.9547382533

Copy link
Contributor

Choose a reason for hiding this comment

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

Compare:

'use strict';

const common = require('../common.js');
const v8 = require('v8');

const bench = common.createBenchmark(main, {
  method: [
    'getHeapStatistics',
    'getHeapSpaceStatistics'
  ],
  n: [1e6],
}, { flags: ['--ignition', '--turbo'] });

function main(conf) {
  console.log(process.execArgv);
  const n = +conf.n;
  const method = conf.method;
  var i = 0;
  bench.start();
  for (; i < n; i++)
    v8[method]();
  bench.end(n);
}
[ '--ignition', '--turbo' ]
v8\get-stats.js n=1000000 method="getHeapStatistics": 1,267,677.6893168623
[ '--ignition', '--turbo' ]
v8\get-stats.js n=1000000 method="getHeapSpaceStatistics": 1,821,113.6853929006

Copy link
Contributor

@vsemozhetbyt vsemozhetbyt Apr 27, 2017

Choose a reason for hiding this comment

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

It seems this is a design that can be improved. Currently, the key flags in configs has no automatic impact, while the same flag in options can not vary between runs.

cc @nodejs/performance, @nodejs/benchmarking, @mscdex

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll just back the flags bit out of this particular change to keep it from holding this up at all, but big +1 on refactoring this generally.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would actually prefer flags in the config object to not have any automagic behavior. It should retain its 'config' meaning (allowing it to be specified on the command line as a regular benchmark option).

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, I did not propose the automatic behavior for this key in config. I meant that may be an easier way to vary execArgv between runs can be supported.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, the flags in the options is applied to all runs...to compare the performance under different flags I would suggest something like

const bench = common.createBenchmark(main, {
  method: [
    'getHeapStatistics',
    'getHeapSpaceStatistics'
  ],
  n: [1e6],
  pipeline: ['new', 'old']
});

function main(conf) {
  if (conf.pipeline === 'new') {
    v8.setFlagsFromString('--ignition');
    v8.setFlagsFromString('--turbo')
  }
}

Unless we have to turn on the flag during bootstrapping..in that case we do need to add special support in the benchmark suite.

});

function main(conf) {
const n = +conf.n;
const method = conf.method;
var i = 0;
bench.start();
for (; i < n; i++)
v8[method]();
bench.end(n);
}
116 changes: 62 additions & 54 deletions lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,50 @@

'use strict';

const Buffer = require('buffer').Buffer;

const v8binding = process.binding('v8');
const serdesBinding = process.binding('serdes');
const bufferBinding = process.binding('buffer');

const { Buffer } = require('buffer');
const { Serializer, Deserializer } = process.binding('serdes');
const { copy } = process.binding('buffer');
const { objectToString } = require('internal/util');
const { FastBuffer } = require('internal/buffer');

// Properties for heap statistics buffer extraction.
const {
cachedDataVersionTag,
setFlagsFromString,
heapStatisticsArrayBuffer,
heapSpaceStatisticsArrayBuffer,
updateHeapStatisticsArrayBuffer,
updateHeapSpaceStatisticsArrayBuffer,

// Properties for heap and heap space statistics buffer extraction.
kTotalHeapSizeIndex,
kTotalHeapSizeExecutableIndex,
kTotalPhysicalSizeIndex,
kTotalAvailableSize,
kUsedHeapSizeIndex,
kHeapSizeLimitIndex,
kDoesZapGarbageIndex,
kMallocedMemoryIndex,
kPeakMallocedMemoryIndex,
kHeapSpaces,
kHeapSpaceStatisticsPropertiesCount,
kSpaceSizeIndex,
kSpaceUsedSizeIndex,
kSpaceAvailableSizeIndex,
kPhysicalSpaceSizeIndex
} = process.binding('v8');

const kNumberOfHeapSpaces = kHeapSpaces.length;

const heapStatisticsBuffer =
new Float64Array(v8binding.heapStatisticsArrayBuffer);
const kTotalHeapSizeIndex = v8binding.kTotalHeapSizeIndex;
const kTotalHeapSizeExecutableIndex = v8binding.kTotalHeapSizeExecutableIndex;
const kTotalPhysicalSizeIndex = v8binding.kTotalPhysicalSizeIndex;
const kTotalAvailableSize = v8binding.kTotalAvailableSize;
const kUsedHeapSizeIndex = v8binding.kUsedHeapSizeIndex;
const kHeapSizeLimitIndex = v8binding.kHeapSizeLimitIndex;
const kDoesZapGarbageIndex = v8binding.kDoesZapGarbageIndex;
const kMallocedMemoryIndex = v8binding.kMallocedMemoryIndex;
const kPeakMallocedMemoryIndex = v8binding.kPeakMallocedMemoryIndex;

// Properties for heap space statistics buffer extraction.
new Float64Array(heapStatisticsArrayBuffer);

const heapSpaceStatisticsBuffer =
new Float64Array(v8binding.heapSpaceStatisticsArrayBuffer);
const kHeapSpaces = v8binding.kHeapSpaces;
const kNumberOfHeapSpaces = kHeapSpaces.length;
const kHeapSpaceStatisticsPropertiesCount =
v8binding.kHeapSpaceStatisticsPropertiesCount;
const kSpaceSizeIndex = v8binding.kSpaceSizeIndex;
const kSpaceUsedSizeIndex = v8binding.kSpaceUsedSizeIndex;
const kSpaceAvailableSizeIndex = v8binding.kSpaceAvailableSizeIndex;
const kPhysicalSpaceSizeIndex = v8binding.kPhysicalSpaceSizeIndex;

exports.getHeapStatistics = function() {
new Float64Array(heapSpaceStatisticsArrayBuffer);

function getHeapStatistics() {
const buffer = heapStatisticsBuffer;

v8binding.updateHeapStatisticsArrayBuffer();
updateHeapStatisticsArrayBuffer();

return {
'total_heap_size': buffer[kTotalHeapSizeIndex],
Expand All @@ -64,15 +70,12 @@ exports.getHeapStatistics = function() {
'peak_malloced_memory': buffer[kPeakMallocedMemoryIndex],
'does_zap_garbage': buffer[kDoesZapGarbageIndex]
};
};

exports.cachedDataVersionTag = v8binding.cachedDataVersionTag;
exports.setFlagsFromString = v8binding.setFlagsFromString;
}

exports.getHeapSpaceStatistics = function() {
function getHeapSpaceStatistics() {
const heapSpaceStatistics = new Array(kNumberOfHeapSpaces);
const buffer = heapSpaceStatisticsBuffer;
v8binding.updateHeapSpaceStatisticsArrayBuffer();
updateHeapSpaceStatisticsArrayBuffer();

for (var i = 0; i < kNumberOfHeapSpaces; i++) {
const propertyOffset = i * kHeapSpaceStatisticsPropertiesCount;
Expand All @@ -86,17 +89,14 @@ exports.getHeapSpaceStatistics = function() {
}

return heapSpaceStatistics;
};
}

/* V8 serialization API */

const Serializer = exports.Serializer = serdesBinding.Serializer;
const Deserializer = exports.Deserializer = serdesBinding.Deserializer;

/* JS methods for the base objects */
Serializer.prototype._getDataCloneError = Error;

Deserializer.prototype.readRawBytes = function(length) {
Deserializer.prototype.readRawBytes = function readRawBytes(length) {
const offset = this._readRawBytes(length);
// `this.buffer` can be a Buffer or a plain Uint8Array, so just calling
// `.slice()` doesn't work.
Expand Down Expand Up @@ -155,8 +155,6 @@ class DefaultSerializer extends Serializer {
}
}

exports.DefaultSerializer = DefaultSerializer;

class DefaultDeserializer extends Deserializer {
constructor(buffer) {
super(buffer);
Expand All @@ -176,27 +174,37 @@ class DefaultDeserializer extends Deserializer {
byteLength / BYTES_PER_ELEMENT);
} else {
// Copy to an aligned buffer first.
const copy = Buffer.allocUnsafe(byteLength);
bufferBinding.copy(this.buffer, copy, 0,
byteOffset, byteOffset + byteLength);
return new ctor(copy.buffer,
copy.byteOffset,
const buffer_copy = Buffer.allocUnsafe(byteLength);
copy(this.buffer, buffer_copy, 0, byteOffset, byteOffset + byteLength);
return new ctor(buffer_copy.buffer,
buffer_copy.byteOffset,
byteLength / BYTES_PER_ELEMENT);
}
}
}

exports.DefaultDeserializer = DefaultDeserializer;

exports.serialize = function serialize(value) {
function serialize(value) {
const ser = new DefaultSerializer();
ser.writeHeader();
ser.writeValue(value);
return ser.releaseBuffer();
};
}

exports.deserialize = function deserialize(buffer) {
function deserialize(buffer) {
const der = new DefaultDeserializer(buffer);
der.readHeader();
return der.readValue();
}

module.exports = exports = {
cachedDataVersionTag,
getHeapStatistics,
getHeapSpaceStatistics,
setFlagsFromString,
Serializer,
Deserializer,
DefaultSerializer,
DefaultDeserializer,
deserialize,
serialize
};