Skip to content

Commit

Permalink
Minor edits to reduce memory use (#815)
Browse files Browse the repository at this point in the history
  • Loading branch information
Balearica committed Aug 28, 2023
1 parent 2c2e014 commit 79cacaa
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/worker-script/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ res) => {
if (!resp.ok) {
throw Error(`Network error while fetching ${fetchUrl}. Response code: ${resp.status}`);
}
data = await resp.arrayBuffer();
data = new Uint8Array(await resp.arrayBuffer());

// langPath is a local file, read .traineddata from local filesystem
// (adapter.readCache is a generic file read function in Node.js version)
Expand All @@ -132,8 +132,6 @@ res) => {
}
}

data = new Uint8Array(data);

// Check for gzip magic numbers (1F and 8B in hex)
const isGzip = (data[0] === 31 && data[1] === 139) || (data[1] === 31 && data[0] === 139);

Expand All @@ -160,8 +158,7 @@ res) => {
log(err.toString());
}
}

return Promise.resolve(data);
return Promise.resolve();
};

res.progress({ workerId, status: 'loading language traineddata', progress: 0 });
Expand Down Expand Up @@ -445,8 +442,14 @@ const terminate = async (_, res) => {
*/
exports.dispatchHandlers = (packet, send) => {
const res = (status, data) => {
// Return only the necessary info to avoid sending unnecessarily large messages
const packetRes = {
jobId: packet.jobId,
workerId: packet.workerId,
action: packet.action,
};
send({
...packet,
...packetRes,
status,
data,
});
Expand Down

0 comments on commit 79cacaa

Please sign in to comment.