Skip to content

Commit

Permalink
feat(fs): improve readTextFile and readTextFileLines performance …
Browse files Browse the repository at this point in the history
…(#1962)

Committed via a GitHub action: https://github.com/tauri-apps/plugins-workspace/actions/runs/11954486261

Co-authored-by: amrbashir <[email protected]>
  • Loading branch information
2 people authored and tauri-bot committed Nov 21, 2024
1 parent c981b60 commit 4f7b852
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 75 deletions.
2 changes: 1 addition & 1 deletion api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 17 additions & 5 deletions dist-js/index.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,12 @@ async function readTextFile(path, options) {
if (path instanceof URL && path.protocol !== 'file:') {
throw new TypeError('Must be a file URL.');
}
return await core.invoke('plugin:fs|read_text_file', {
const arr = await core.invoke('plugin:fs|read_text_file', {
path: path instanceof URL ? path.toString() : path,
options
});
const bytes = arr instanceof ArrayBuffer ? arr : Uint8Array.from(arr);
return new TextDecoder().decode(bytes);
}
/**
* Returns an async {@linkcode AsyncIterableIterator} over the lines of a file as UTF-8 string.
Expand Down Expand Up @@ -479,12 +481,22 @@ async function readTextFileLines(path, options) {
options
});
}
const [line, done] = await core.invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
// an iteration is over, reset rid for next iteration
if (done)
const arr = await core.invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
const bytes = arr instanceof ArrayBuffer ? new Uint8Array(arr) : Uint8Array.from(arr);
// Rust side will never return an empty array for this command and
// ensure there is at least one elements there.
//
// This is an optimization to include whether we finished iteration or not (1 or 0)
// at the end of returned array to avoid serialization overhead of separate values.
const done = bytes[bytes.byteLength - 1] === 1;
if (done) {
// a full iteration is over, reset rid for next iteration
this.rid = null;
return { value: null, done };
}
const line = new TextDecoder().decode(bytes.slice(0, bytes.byteLength));
return {
value: done ? '' : line,
value: line,
done
};
},
Expand Down
22 changes: 17 additions & 5 deletions dist-js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,12 @@ async function readTextFile(path, options) {
if (path instanceof URL && path.protocol !== 'file:') {
throw new TypeError('Must be a file URL.');
}
return await invoke('plugin:fs|read_text_file', {
const arr = await invoke('plugin:fs|read_text_file', {
path: path instanceof URL ? path.toString() : path,
options
});
const bytes = arr instanceof ArrayBuffer ? arr : Uint8Array.from(arr);
return new TextDecoder().decode(bytes);
}
/**
* Returns an async {@linkcode AsyncIterableIterator} over the lines of a file as UTF-8 string.
Expand Down Expand Up @@ -477,12 +479,22 @@ async function readTextFileLines(path, options) {
options
});
}
const [line, done] = await invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
// an iteration is over, reset rid for next iteration
if (done)
const arr = await invoke('plugin:fs|read_text_file_lines_next', { rid: this.rid });
const bytes = arr instanceof ArrayBuffer ? new Uint8Array(arr) : Uint8Array.from(arr);
// Rust side will never return an empty array for this command and
// ensure there is at least one elements there.
//
// This is an optimization to include whether we finished iteration or not (1 or 0)
// at the end of returned array to avoid serialization overhead of separate values.
const done = bytes[bytes.byteLength - 1] === 1;
if (done) {
// a full iteration is over, reset rid for next iteration
this.rid = null;
return { value: null, done };
}
const line = new TextDecoder().decode(bytes.slice(0, bytes.byteLength));
return {
value: done ? '' : line,
value: line,
done
};
},
Expand Down
Loading

0 comments on commit 4f7b852

Please sign in to comment.