Skip to content

packed hexSliceLookupTable instead holey #246

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

Closed
wants to merge 1 commit into from

Conversation

fanatid
Copy link
Contributor

@fanatid fanatid commented Sep 17, 2019

Lookup table was added in #245
I made wrong assumption about packed/holey array. In #245 was holey, packed by benchmark give up to 10%.

Code for bench:

const { randomBytes } = require('crypto')

var hexSliceLookupTable1 = (function () {
  var alphabet = '0123456789abcdef'
  var table = new Array(256)
  for (var i = 0; i < 16; ++i) {
    var i16 = i * 16
    for (var j = 0; j < 16; ++j) {
      table[i16 + j] = alphabet[i] + alphabet[j]
    }
  }
  return table
})()

var hexSliceLookupTable2 = (function () {
  var alphabet = '0123456789abcdef'
  var table = []
  for (var i = 0; i < 16; ++i) {
    var i16 = i * 16
    for (var j = 0; j < 16; ++j) {
      table.push(alphabet[i] + alphabet[j])
    }
  }
  return table
})()

console.log(`%HasPackedElements(hexSliceLookupTable1) = ${%HasPackedElements(hexSliceLookupTable1)}`)
console.log(`%HasHoleyElements(hexSliceLookupTable1) = ${%HasHoleyElements(hexSliceLookupTable1)}`)

console.log(`%HasPackedElements(hexSliceLookupTable2) = ${%HasPackedElements(hexSliceLookupTable2)}`)
console.log(`%HasHoleyElements(hexSliceLookupTable2) = ${%HasHoleyElements(hexSliceLookupTable2)}`)


function hexSlice (buf, table) {
  var out = ''
  for (var i = 0; i < buf.length; ++i) {
    out += table[buf[i]]
  }
  return out
}

for (let i = 0; i < 1e3; ++i) {
  hexSlice(randomBytes(32), hexSliceLookupTable1)
  hexSlice(randomBytes(32), hexSliceLookupTable2)
}
console.log(`%GetOptimizationStatus(hexSlice) = ${%GetOptimizationStatus(hexSlice)}, is optimized: ${!!(%GetOptimizationStatus(hexSlice) & (1 << 4))}`)


const buffer = randomBytes(100 * 1024)

console.time('hexSliceLookupTable1')
for (let i = 0; i < 1e3; ++i) hexSlice(buffer, hexSliceLookupTable1)
console.timeEnd('hexSliceLookupTable1')

console.time('hexSliceLookupTable2')
for (let i = 0; i < 1e3; ++i) hexSlice(buffer, hexSliceLookupTable2)
console.timeEnd('hexSliceLookupTable2')
$ node --allow-natives-syntax x.js 
%HasPackedElements(hexSliceLookupTable1) = false
%HasHoleyElements(hexSliceLookupTable1) = true
%HasPackedElements(hexSliceLookupTable2) = true
%HasHoleyElements(hexSliceLookupTable2) = false
%GetOptimizationStatus(hexSlice) = 49, is optimized: true
hexSliceLookupTable1: 788.261ms
hexSliceLookupTable2: 761.303ms

@fanatid
Copy link
Contributor Author

fanatid commented Sep 17, 2019

Swap two calls and result is different, haha.

hexSliceLookupTable1: 840.760ms
hexSliceLookupTable2: 788.295ms
hexSliceLookupTable1: 819.380ms
hexSliceLookupTable2: 784.390ms
hexSliceLookupTable1: 763.472ms
hexSliceLookupTable2: 769.063ms
hexSliceLookupTable1: 787.785ms
hexSliceLookupTable2: 800.740ms
hexSliceLookupTable1: 773.156ms
hexSliceLookupTable2: 748.748ms

Looks like not big difference between packed/holey (or need check more deeply).

@fanatid fanatid closed this Sep 17, 2019
@fanatid fanatid deleted the hex-slice-packed branch September 17, 2019 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant