-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from SalvatorePreviti/refactoring
Refactoring
- Loading branch information
Showing
18 changed files
with
221 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,2 @@ | ||
const cpuInfo = require('./lib/cpuinfo') | ||
|
||
let roaringNodePath | ||
if (cpuInfo.AVX2) { | ||
roaringNodePath = `./build/Release/roaring-avx2.node` | ||
} else if (cpuInfo.SSE42) { | ||
roaringNodePath = './build/Release/roaring-sse42.node' | ||
} else { | ||
roaringNodePath = './build/Release/roaring.node' | ||
} | ||
|
||
const roaringNode = require(roaringNodePath) | ||
|
||
roaringNode._initTypes({ | ||
Array, | ||
Buffer, | ||
Uint32Array | ||
}) | ||
|
||
if (!roaringNode.hasOwnProperty('PackageVersion')) { | ||
Object.defineProperty(roaringNode, 'PackageVersion', { | ||
get() { | ||
return require('./package.json').version | ||
}, | ||
enumerable: true, | ||
configurable: false | ||
}) | ||
} | ||
|
||
module.exports = roaringNode | ||
const getRoaring = require('./lib/getRoaring') | ||
module.exports = getRoaring() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const instructionSet = require('./instructionSet') | ||
const moduleExists = require('./moduleExists') | ||
|
||
const roaringNodePaths = { | ||
AVX2: '../build/Release/roaring-avx2.node', | ||
SSE42: '../build/Release/roaring-sse42.node', | ||
PLAIN: '../build/Release/roaring.node' | ||
} | ||
|
||
const cache = { | ||
AVX2: undefined, | ||
SSE42: undefined, | ||
PLAIN: undefined | ||
} | ||
|
||
let packageVersion | ||
|
||
function getPackageVersion() { | ||
if (packageVersion === undefined) { | ||
packageVersion = require('../package.json').version | ||
} | ||
return packageVersion | ||
} | ||
|
||
function loadRoaring(roaringNodePath) { | ||
const roaringNode = require(roaringNodePath) | ||
|
||
roaringNode._initTypes({ | ||
Set, | ||
Array, | ||
Buffer, | ||
Uint32Array | ||
}) | ||
|
||
if (!roaringNode.hasOwnProperty('PackageVersion')) { | ||
Object.defineProperty(roaringNode, 'PackageVersion', { | ||
get: getPackageVersion, | ||
enumerable: true, | ||
configurable: false | ||
}) | ||
} | ||
return roaringNode | ||
} | ||
|
||
function getByInstructionSet(set) { | ||
const cached = cache[set] | ||
if (typeof cached === 'object') { | ||
return cached === null ? undefined : cached | ||
} | ||
|
||
const roaringNodePath = roaringNodePaths[set] | ||
if (typeof roaringNodePath !== 'string') { | ||
throw new TypeError(`Unknown instruction set: ${set}`) | ||
} | ||
|
||
if ( | ||
(set === 'AVX2' && instructionSet !== 'AVX2') || | ||
(set === 'SSE42' && instructionSet === 'PLAIN') || | ||
(instructionSet !== 'PLAIN' && !moduleExists(roaringNodePath)) | ||
) { | ||
cache[set] = null | ||
return undefined | ||
} | ||
|
||
const roaring = loadRoaring(roaringNodePath) | ||
cache[set] = roaring | ||
return roaring | ||
} | ||
|
||
function getRoaring(instructionSetToUse) { | ||
if (instructionSetToUse !== undefined) { | ||
return getByInstructionSet(instructionSetToUse) | ||
} | ||
const cached = cache[instructionSet] | ||
if (typeof cached === 'object') { | ||
return cached === null ? undefined : cached | ||
} | ||
if (instructionSet === 'AVX2') { | ||
return getByInstructionSet('AVX2') || getByInstructionSet('SSE42') || getByInstructionSet('PLAIN') | ||
} | ||
if (instructionSet === 'SSE42') { | ||
return getByInstructionSet('SSE42') || getByInstructionSet('PLAIN') | ||
} | ||
return getByInstructionSet('PLAIN') | ||
} | ||
|
||
module.exports = getRoaring |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
function moduleExists(modulePath) { | ||
try { | ||
return !!require.resolve(modulePath) | ||
} catch (e) { | ||
return false | ||
} | ||
} | ||
|
||
module.exports = moduleExists |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.