Skip to content

Commit fd3bb32

Browse files
authored
Improve build performance (#83)
1 parent e5e0057 commit fd3bb32

File tree

2 files changed

+58
-36
lines changed

2 files changed

+58
-36
lines changed

scripts/generate-data.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
const resources = require('../data/resources.js');
44
const generateData = require('../index.js');
5-
const utils = require('../scripts/utils.js');
65

76
// -----------------------------------------------------------------------------
87

@@ -28,7 +27,9 @@ const complicatedWorkThatTakesTime = (resource, callback) => {
2827
console.log('[%s] Worker %d \u2192 Unicode v%s',
2928
getTime(), cluster.worker.id, version);
3029

30+
console.groupCollapsed();
3131
generateData(version);
32+
console.groupEnd();
3233

3334
complicatedWorkThatTakesTime(
3435
resource.slice(1),
@@ -40,10 +41,16 @@ const complicatedWorkThatTakesTime = (resource, callback) => {
4041
}
4142
};
4243

43-
if (cluster.isMaster) {
44+
if (cluster.isPrimary) {
4445

4546
for (let index = 0; index < numCPUs; index++) {
46-
cluster.fork();
47+
const worker = cluster.fork();
48+
worker.on('message', (error) => {
49+
for (const id in cluster.workers) {
50+
cluster.workers[id].kill();
51+
}
52+
throw new Error(`Worker ${worker.id} encountered an error: ${error}`);
53+
})
4754
}
4855

4956
cluster.on('online', (worker) => {
@@ -76,4 +83,14 @@ if (cluster.isMaster) {
7683
});
7784
});
7885

86+
process.on('uncaughtException', (error) => {
87+
console.error(error);
88+
process.send(error.message);
89+
});
90+
91+
process.on('unhandledRejection', (error) => {
92+
console.error(error);
93+
process.send(error.message || error);
94+
});
95+
7996
}

scripts/utils.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const gzipInline = function(data) {
1111
if (data instanceof Map) {
1212
return `new Map(${ gzipInline([...data]) })`;
1313
}
14-
const json = jsesc(data, { 'json': true });
14+
const json = JSON.stringify(data);
1515
const gzipBuffer = zlib.gzipSync(json);
1616
const str = gzipBuffer.toString('base64');
1717
return `JSON.parse(require('zlib').gunzipSync(Buffer.from('${ str }','base64')))`;
@@ -84,7 +84,13 @@ const writeFiles = function(options) {
8484
return;
8585
}
8686
const dirMap = {};
87-
const bidiMirroringGlyphMap = [];
87+
/**
88+
* A list of flatten (x, y) pairs,
89+
* where x is a codepoint, y := codepoint(z) - x,
90+
* where z is the BidiMirroringGlyph of character(x) and codepoint(z) > x
91+
* @type number[]
92+
*/
93+
const bidiMirroringGlyphFlatPairs = [];
8894
const auxMap = {};
8995
Object.keys(map).forEach(function(item) {
9096
const codePoints = map[item];
@@ -112,10 +118,11 @@ const writeFiles = function(options) {
112118
)
113119
) {
114120
if (type == 'Bidi_Mirroring_Glyph') {
115-
const shortName = item.codePointAt(0);
121+
const toCodepoint = item.codePointAt(0);
116122
codePoints.toArray().forEach(function(codePoint) {
117-
console.assert(!bidiMirroringGlyphMap[codePoint]);
118-
bidiMirroringGlyphMap[codePoint] = shortName;
123+
if (codePoint < toCodepoint) {
124+
bidiMirroringGlyphFlatPairs.push(codePoint, toCodepoint - codePoint);
125+
}
119126
});
120127
} else {
121128
if (!auxMap[type]) {
@@ -124,34 +131,7 @@ const writeFiles = function(options) {
124131
auxMap[type].push([item, codePoints]);
125132
}
126133
}
127-
if (isNamesCanon) {
128-
return;
129-
}
130-
131-
if (type == 'Bidi_Mirroring_Glyph') {
132-
const dir = path.resolve(
133-
__dirname, '..',
134-
'output', 'unicode-' + version, type
135-
);
136-
if (!hasKey(dirMap, type)) {
137-
dirMap[type] = [];
138-
}
139-
fs.mkdirSync(dir, { recursive: true });
140-
// `Bidi_Mirroring_Glyph/index.js`
141-
// Note: `Bidi_Mirroring_Glyph` doesn’t have repeated strings; don’t gzip.
142-
const flatPairs = bidiMirroringGlyphMap
143-
.flatMap((a, b) => a < b ? [a, b - a] : []);
144-
const output = [
145-
`const chr=String.fromCodePoint`,
146-
`const pair=(t,u,v)=>[t?u+v:v,chr(t?u:u+v)]`,
147-
`module.exports=new Map(${
148-
jsesc(flatPairs)
149-
}.map((v,i,a)=>pair(i&1,a[i^1],v)))`
150-
].join(';');
151-
fs.writeFileSync(
152-
path.resolve(dir, 'index.js'),
153-
output
154-
);
134+
if (isNamesCanon || type == 'Bidi_Mirroring_Glyph') {
155135
return;
156136
}
157137

@@ -211,6 +191,31 @@ const writeFiles = function(options) {
211191
`module.exports=${ symbolsExports }`
212192
);
213193
});
194+
if (options.type == 'Bidi_Mirroring_Glyph') {
195+
const type = options.type;
196+
const dir = path.resolve(
197+
__dirname, '..',
198+
'output', 'unicode-' + version, type
199+
);
200+
if (!hasKey(dirMap, type)) {
201+
dirMap[type] = [];
202+
}
203+
fs.mkdirSync(dir, { recursive: true });
204+
// `Bidi_Mirroring_Glyph/index.js`
205+
// Note: `Bidi_Mirroring_Glyph` doesn’t have repeated strings; don’t gzip.
206+
const output = [
207+
`const chr=String.fromCodePoint`,
208+
`const pair=(t,u,v)=>[t?u+v:v,chr(t?u:u+v)]`,
209+
`module.exports=new Map(${
210+
JSON.stringify(bidiMirroringGlyphFlatPairs)
211+
}.map((v,i,a)=>pair(i&1,a[i^1],v)))`
212+
].join(';');
213+
fs.writeFileSync(
214+
path.resolve(dir, 'index.js'),
215+
output
216+
);
217+
return;
218+
}
214219
Object.keys(auxMap).forEach(function(type) {
215220
const dir = path.resolve(
216221
__dirname, '..',

0 commit comments

Comments
 (0)