ソースを参照

Perf: use `readline` to `readFileByLine` (50% faster)

SukkaW 1 年間 前
コミット
3107e72787
2 ファイル変更20 行追加9 行削除
  1. 11 7
      Build/lib/fetch-text-by-line.bench.ts
  2. 9 2
      Build/lib/fetch-text-by-line.ts

+ 11 - 7
Build/lib/fetch-text-by-line.bench.ts

@@ -1,15 +1,19 @@
-import { bench, group, run } from 'mitata';
 import { processLine, processLineFromReadline } from './process-line';
-import { readFileByLine } from './fetch-text-by-line';
+import { readFileByLine, readFileByLineLegacy } from './fetch-text-by-line';
 import path from 'node:path';
 import fsp from 'node:fs/promises';
 import { SOURCE_DIR } from '../constants/dir';
 
 const file = path.join(SOURCE_DIR, 'domainset/cdn.conf');
 
-group(() => {
-  bench('readFileByLine', () => processLineFromReadline(readFileByLine(file)));
-  bench('fsp.readFile', () => fsp.readFile(file, 'utf-8').then((content) => content.split('\n').filter(processLine)));
-});
+(async () => {
+  const { bench, group, run } = await import('mitata');
 
-run();
+  group(() => {
+    bench('readFileByLine', () => processLineFromReadline(readFileByLine(file)));
+    bench('readFileByLineLegacy', () => processLineFromReadline(readFileByLineLegacy(file)));
+    bench('fsp.readFile', () => fsp.readFile(file, 'utf-8').then((content) => content.split('\n').filter(processLine)));
+  });
+
+  run();
+})();

+ 9 - 2
Build/lib/fetch-text-by-line.ts

@@ -1,6 +1,7 @@
 import fs from 'node:fs';
 import { Readable } from 'node:stream';
 import type { FileHandle } from 'node:fs/promises';
+import readline from 'node:readline';
 
 import { TextLineStream } from './text-line-transform-stream';
 import type { ReadableStream } from 'node:stream/web';
@@ -18,11 +19,17 @@ function getReadableStream(file: string | FileHandle): ReadableStream {
   }
   return file.readableWebStream();
 }
+
 // TODO: use FileHandle.readLine()
-export const readFileByLine: ((file: string | FileHandle) => AsyncIterable<string>) = (file: string | FileHandle) => getReadableStream(file)
+export const readFileByLineLegacy: ((file: string /* | FileHandle */) => AsyncIterable<string>) = (file: string | FileHandle) => getReadableStream(file)
   .pipeThrough(new TextDecoderStream())
   .pipeThrough(new TextLineStream());
 
+export const readFileByLine: ((file: string /* | FileHandle */) => AsyncIterable<string>) = (file: string) => readline.createInterface({
+  input: fs.createReadStream(file/* , { encoding: 'utf-8' } */),
+  crlfDelay: Infinity
+});
+
 function ensureResponseBody<T extends NodeFetchResponse | UndiciResponseData | UnidiciWebResponse>(resp: T): NonNullable<T['body']> {
   if (resp.body == null) {
     throw new Error('Failed to fetch remote text');
@@ -53,7 +60,7 @@ export function fetchRemoteTextByLine(url: string) {
   return $fetch(url).then(createReadlineInterfaceFromResponse);
 }
 
-export async function readFileIntoProcessedArray(file: string | FileHandle) {
+export async function readFileIntoProcessedArray(file: string /* | FileHandle */) {
   const results = [];
   for await (const line of readFileByLine(file)) {
     if (processLine(line)) {