@naturalcycles/nodejs-lib 15.125.0 → 15.125.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/stream/ndjson/createReadStreamAsNDJson.js +3 -3
- package/dist/stream/pipeline.js +3 -3
- package/dist/stream/transform/transformSplit.js +36 -24
- package/package.json +1 -1
- package/src/stream/ndjson/createReadStreamAsNDJson.ts +3 -3
- package/src/stream/pipeline.ts +3 -3
- package/src/stream/transform/transformSplit.ts +37 -25
|
@@ -19,17 +19,17 @@ export function createReadStreamAsNDJson(inputPath) {
|
|
|
19
19
|
fs2.requireFileToExist(inputPath);
|
|
20
20
|
let stream = fs2
|
|
21
21
|
.createReadStream(inputPath, {
|
|
22
|
-
highWaterMark:
|
|
22
|
+
highWaterMark: 256 * 1024, // 64KB had no observed speedup over the default; 256KB is +6-12% end-to-end on ~9KB rows, neutral on ~180B rows, 1MB brings nothing more
|
|
23
23
|
})
|
|
24
24
|
.on('error', err => stream.destroy(err));
|
|
25
25
|
if (inputPath.endsWith('.gz')) {
|
|
26
26
|
stream = stream.pipe(createUnzip({
|
|
27
|
-
chunkSize:
|
|
27
|
+
chunkSize: 256 * 1024, // 64KB: speedup from ~3200 to 3800 rps! 256KB: +6-8% more on ~9KB rows, neutral on ~180B rows
|
|
28
28
|
}));
|
|
29
29
|
}
|
|
30
30
|
else if (inputPath.endsWith('.zst')) {
|
|
31
31
|
stream = stream.pipe(createZstdDecompress({
|
|
32
|
-
chunkSize:
|
|
32
|
+
chunkSize: 256 * 1024, // tested: 256KB is +10-12% over 64KB on ~9KB rows, neutral on ~180B rows
|
|
33
33
|
}));
|
|
34
34
|
}
|
|
35
35
|
return stream.pipe(transformSplitOnNewline()).map(line => JSON.parse(line));
|
package/dist/stream/pipeline.js
CHANGED
|
@@ -73,7 +73,7 @@ export class Pipeline {
|
|
|
73
73
|
}
|
|
74
74
|
static fromFile(sourceFilePath) {
|
|
75
75
|
return new Pipeline(fs2.createReadStream(sourceFilePath, {
|
|
76
|
-
highWaterMark:
|
|
76
|
+
highWaterMark: 256 * 1024, // 64KB had no observed speedup over the default; 256KB is +6-12% end-to-end on ~9KB rows, neutral on ~180B rows, 1MB brings nothing more
|
|
77
77
|
}), false);
|
|
78
78
|
}
|
|
79
79
|
/**
|
|
@@ -254,7 +254,7 @@ export class Pipeline {
|
|
|
254
254
|
}
|
|
255
255
|
gunzip(opt) {
|
|
256
256
|
this.transforms.push(createUnzip({
|
|
257
|
-
chunkSize:
|
|
257
|
+
chunkSize: 256 * 1024, // 64KB: speedup from ~3200 to 3800 rps! 256KB: +6-8% more on ~9KB rows, neutral on ~180B rows
|
|
258
258
|
...opt,
|
|
259
259
|
}));
|
|
260
260
|
this.objectMode = false;
|
|
@@ -268,7 +268,7 @@ export class Pipeline {
|
|
|
268
268
|
}
|
|
269
269
|
zstdDecompress(opt) {
|
|
270
270
|
this.transforms.push(createZstdDecompress({
|
|
271
|
-
chunkSize:
|
|
271
|
+
chunkSize: 256 * 1024, // tested: 256KB is +10-12% over 64KB on ~9KB rows, neutral on ~180B rows
|
|
272
272
|
...opt,
|
|
273
273
|
}));
|
|
274
274
|
this.objectMode = false;
|
|
@@ -30,8 +30,9 @@ export function transformSplit(separator = '\n') {
|
|
|
30
30
|
const needle = matcherLength === 1 ? matcher[0] : matcher;
|
|
31
31
|
// a multi-byte separator may straddle a chunk boundary, so we keep that many trailing bytes around
|
|
32
32
|
const overlap = matcherLength - 1;
|
|
33
|
-
// Unterminated data is collected as a list of chunks and concatenated only once
|
|
34
|
-
// so a
|
|
33
|
+
// Unterminated data is collected as a list of chunks and concatenated only once its separator arrives,
|
|
34
|
+
// so a line spanning many chunks is copied once rather than on every chunk (linear, not quadratic).
|
|
35
|
+
// Only that one line is copied: the rest of the chunk is scanned in place and emitted as views into it.
|
|
35
36
|
let pending = [];
|
|
36
37
|
let pendingLength = 0;
|
|
37
38
|
let edge; // last `overlap` bytes of pending data, only tracked when overlap > 0
|
|
@@ -40,39 +41,50 @@ export function transformSplit(separator = '\n') {
|
|
|
40
41
|
writableHighWaterMark: 64 * 1024,
|
|
41
42
|
readableObjectMode: true,
|
|
42
43
|
transform(chunk, _enc, cb) {
|
|
43
|
-
let
|
|
44
|
-
let offset = 0;
|
|
44
|
+
let start = 0;
|
|
45
45
|
if (pendingLength > 0) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
46
|
+
// A multi-byte separator may straddle the chunk boundary: look for it in the last `overlap` bytes
|
|
47
|
+
// of pending data followed by the first `overlap` bytes of this chunk
|
|
48
|
+
const straddleIdx = overlap > 0 ? Buffer.concat([edge, chunk.subarray(0, overlap)]).indexOf(matcher) : -1;
|
|
49
|
+
// Position of the first separator in chunk coordinates, negative when it started inside the pending data
|
|
50
|
+
let idx;
|
|
51
|
+
if (straddleIdx === -1) {
|
|
52
|
+
idx = chunk.indexOf(needle);
|
|
53
|
+
if (idx === -1) {
|
|
54
|
+
// No separator yet: keep accumulating without copying
|
|
55
|
+
pending.push(chunk);
|
|
56
|
+
pendingLength += chunk.length;
|
|
57
|
+
if (overlap > 0) {
|
|
58
|
+
edge = Buffer.concat([edge, chunk.subarray(-overlap)]).subarray(-overlap);
|
|
59
|
+
}
|
|
60
|
+
cb();
|
|
61
|
+
return;
|
|
54
62
|
}
|
|
55
|
-
cb();
|
|
56
|
-
return;
|
|
57
63
|
}
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
64
|
+
else {
|
|
65
|
+
idx = straddleIdx - edge.length;
|
|
66
|
+
}
|
|
67
|
+
// Complete the pending line with the head of this chunk
|
|
68
|
+
if (idx > 0)
|
|
69
|
+
pending.push(chunk.subarray(0, idx));
|
|
70
|
+
const lineLength = pendingLength + idx;
|
|
71
|
+
if (lineLength > 0) {
|
|
72
|
+
this.push(Buffer.concat(pending, lineLength));
|
|
73
|
+
}
|
|
62
74
|
pending = [];
|
|
63
75
|
pendingLength = 0;
|
|
76
|
+
start = idx + matcherLength;
|
|
64
77
|
}
|
|
65
|
-
let
|
|
66
|
-
let idx = buf.indexOf(needle, offset);
|
|
78
|
+
let idx = chunk.indexOf(needle, start);
|
|
67
79
|
while (idx !== -1) {
|
|
68
80
|
if (idx > start) {
|
|
69
|
-
this.push(
|
|
81
|
+
this.push(chunk.subarray(start, idx));
|
|
70
82
|
}
|
|
71
83
|
start = idx + matcherLength;
|
|
72
|
-
idx =
|
|
84
|
+
idx = chunk.indexOf(needle, start);
|
|
73
85
|
}
|
|
74
|
-
if (start <
|
|
75
|
-
const rest =
|
|
86
|
+
if (start < chunk.length) {
|
|
87
|
+
const rest = chunk.subarray(start);
|
|
76
88
|
pending.push(rest);
|
|
77
89
|
pendingLength = rest.length;
|
|
78
90
|
if (overlap > 0)
|
package/package.json
CHANGED
|
@@ -23,20 +23,20 @@ export function createReadStreamAsNDJson<ROW = any>(inputPath: string): Readable
|
|
|
23
23
|
|
|
24
24
|
let stream: ReadableTyped<ROW> = fs2
|
|
25
25
|
.createReadStream(inputPath, {
|
|
26
|
-
highWaterMark:
|
|
26
|
+
highWaterMark: 256 * 1024, // 64KB had no observed speedup over the default; 256KB is +6-12% end-to-end on ~9KB rows, neutral on ~180B rows, 1MB brings nothing more
|
|
27
27
|
})
|
|
28
28
|
.on('error', err => stream.destroy(err))
|
|
29
29
|
|
|
30
30
|
if (inputPath.endsWith('.gz')) {
|
|
31
31
|
stream = stream.pipe(
|
|
32
32
|
createUnzip({
|
|
33
|
-
chunkSize:
|
|
33
|
+
chunkSize: 256 * 1024, // 64KB: speedup from ~3200 to 3800 rps! 256KB: +6-8% more on ~9KB rows, neutral on ~180B rows
|
|
34
34
|
}),
|
|
35
35
|
)
|
|
36
36
|
} else if (inputPath.endsWith('.zst')) {
|
|
37
37
|
stream = stream.pipe(
|
|
38
38
|
createZstdDecompress({
|
|
39
|
-
chunkSize:
|
|
39
|
+
chunkSize: 256 * 1024, // tested: 256KB is +10-12% over 64KB on ~9KB rows, neutral on ~180B rows
|
|
40
40
|
}),
|
|
41
41
|
)
|
|
42
42
|
}
|
package/src/stream/pipeline.ts
CHANGED
|
@@ -114,7 +114,7 @@ export class Pipeline<T = unknown> {
|
|
|
114
114
|
static fromFile(sourceFilePath: string): Pipeline<Uint8Array> {
|
|
115
115
|
return new Pipeline(
|
|
116
116
|
fs2.createReadStream(sourceFilePath, {
|
|
117
|
-
highWaterMark:
|
|
117
|
+
highWaterMark: 256 * 1024, // 64KB had no observed speedup over the default; 256KB is +6-12% end-to-end on ~9KB rows, neutral on ~180B rows, 1MB brings nothing more
|
|
118
118
|
}),
|
|
119
119
|
false,
|
|
120
120
|
)
|
|
@@ -347,7 +347,7 @@ export class Pipeline<T = unknown> {
|
|
|
347
347
|
gunzip(this: Pipeline<Uint8Array>, opt?: ZlibOptions): Pipeline<Uint8Array> {
|
|
348
348
|
this.transforms.push(
|
|
349
349
|
createUnzip({
|
|
350
|
-
chunkSize:
|
|
350
|
+
chunkSize: 256 * 1024, // 64KB: speedup from ~3200 to 3800 rps! 256KB: +6-8% more on ~9KB rows, neutral on ~180B rows
|
|
351
351
|
...opt,
|
|
352
352
|
}),
|
|
353
353
|
)
|
|
@@ -368,7 +368,7 @@ export class Pipeline<T = unknown> {
|
|
|
368
368
|
zstdDecompress(this: Pipeline<Uint8Array>, opt?: ZstdOptions): Pipeline<Uint8Array> {
|
|
369
369
|
this.transforms.push(
|
|
370
370
|
createZstdDecompress({
|
|
371
|
-
chunkSize:
|
|
371
|
+
chunkSize: 256 * 1024, // tested: 256KB is +10-12% over 64KB on ~9KB rows, neutral on ~180B rows
|
|
372
372
|
...opt,
|
|
373
373
|
}),
|
|
374
374
|
)
|
|
@@ -35,8 +35,9 @@ export function transformSplit(separator = '\n'): TransformTyped<Buffer, Buffer>
|
|
|
35
35
|
// a multi-byte separator may straddle a chunk boundary, so we keep that many trailing bytes around
|
|
36
36
|
const overlap = matcherLength - 1
|
|
37
37
|
|
|
38
|
-
// Unterminated data is collected as a list of chunks and concatenated only once
|
|
39
|
-
// so a
|
|
38
|
+
// Unterminated data is collected as a list of chunks and concatenated only once its separator arrives,
|
|
39
|
+
// so a line spanning many chunks is copied once rather than on every chunk (linear, not quadratic).
|
|
40
|
+
// Only that one line is copied: the rest of the chunk is scanned in place and emitted as views into it.
|
|
40
41
|
let pending: Buffer[] = []
|
|
41
42
|
let pendingLength = 0
|
|
42
43
|
let edge: Buffer | undefined // last `overlap` bytes of pending data, only tracked when overlap > 0
|
|
@@ -47,43 +48,54 @@ export function transformSplit(separator = '\n'): TransformTyped<Buffer, Buffer>
|
|
|
47
48
|
readableObjectMode: true,
|
|
48
49
|
|
|
49
50
|
transform(chunk: Buffer, _enc, cb) {
|
|
50
|
-
let
|
|
51
|
-
let offset = 0
|
|
51
|
+
let start = 0
|
|
52
52
|
|
|
53
53
|
if (pendingLength > 0) {
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
const
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
54
|
+
// A multi-byte separator may straddle the chunk boundary: look for it in the last `overlap` bytes
|
|
55
|
+
// of pending data followed by the first `overlap` bytes of this chunk
|
|
56
|
+
const straddleIdx =
|
|
57
|
+
overlap > 0 ? Buffer.concat([edge!, chunk.subarray(0, overlap)]).indexOf(matcher) : -1
|
|
58
|
+
|
|
59
|
+
// Position of the first separator in chunk coordinates, negative when it started inside the pending data
|
|
60
|
+
let idx: number
|
|
61
|
+
if (straddleIdx === -1) {
|
|
62
|
+
idx = chunk.indexOf(needle)
|
|
63
|
+
if (idx === -1) {
|
|
64
|
+
// No separator yet: keep accumulating without copying
|
|
65
|
+
pending.push(chunk)
|
|
66
|
+
pendingLength += chunk.length
|
|
67
|
+
if (overlap > 0) {
|
|
68
|
+
edge = Buffer.concat([edge!, chunk.subarray(-overlap)]).subarray(-overlap)
|
|
69
|
+
}
|
|
70
|
+
cb()
|
|
71
|
+
return
|
|
63
72
|
}
|
|
64
|
-
|
|
65
|
-
|
|
73
|
+
} else {
|
|
74
|
+
idx = straddleIdx - edge!.length
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Complete the pending line with the head of this chunk
|
|
78
|
+
if (idx > 0) pending.push(chunk.subarray(0, idx))
|
|
79
|
+
const lineLength = pendingLength + idx
|
|
80
|
+
if (lineLength > 0) {
|
|
81
|
+
this.push(Buffer.concat(pending, lineLength))
|
|
66
82
|
}
|
|
67
|
-
pending.push(chunk)
|
|
68
|
-
buf = Buffer.concat(pending, pendingLength + chunk.length)
|
|
69
|
-
// Start scanning where the first separator can possibly be
|
|
70
|
-
offset = straddles ? Math.max(0, pendingLength - overlap) : pendingLength + idxInChunk
|
|
71
83
|
pending = []
|
|
72
84
|
pendingLength = 0
|
|
85
|
+
start = idx + matcherLength
|
|
73
86
|
}
|
|
74
87
|
|
|
75
|
-
let
|
|
76
|
-
let idx = buf.indexOf(needle, offset)
|
|
88
|
+
let idx = chunk.indexOf(needle, start)
|
|
77
89
|
while (idx !== -1) {
|
|
78
90
|
if (idx > start) {
|
|
79
|
-
this.push(
|
|
91
|
+
this.push(chunk.subarray(start, idx))
|
|
80
92
|
}
|
|
81
93
|
start = idx + matcherLength
|
|
82
|
-
idx =
|
|
94
|
+
idx = chunk.indexOf(needle, start)
|
|
83
95
|
}
|
|
84
96
|
|
|
85
|
-
if (start <
|
|
86
|
-
const rest =
|
|
97
|
+
if (start < chunk.length) {
|
|
98
|
+
const rest = chunk.subarray(start)
|
|
87
99
|
pending.push(rest)
|
|
88
100
|
pendingLength = rest.length
|
|
89
101
|
if (overlap > 0) edge = rest.subarray(-overlap)
|