KnicKnic / WASM-ImageMagick

Webassembly compilation of https://github.com/ImageMagick/ImageMagick & samples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use Transferable objects to pass data between worker and page

cancerberoSgx opened this issue · comments

It seens that webworkers support transference of data betwee the worker and the page with a zero-copy operation, which results in a vast performance improvement when sending large data sets. We should use that to pass input and output files from and to the web-worker:

see call boundries

export function call(inputFiles: MagickInputFile[], command: string[]): Promise<CallResult> {
const request = {
files: inputFiles,
args: command,
requestNumber: magickWorkerPromisesKey,
}
const promise = CreatePromiseEvent()
magickWorkerPromises[magickWorkerPromisesKey] = promise
magickWorker.postMessage(request)
magickWorkerPromisesKey++
return promise as Promise<CallResult>
}

message.outputFiles = responseFiles
message.stdout = stdout.map(s => s)
message.stderr = stderr.map(s => s)
message.exitCode = exitCode
postMessage(message)

onmessage = function (magickRequest) {
Module.messagesToProcess.push(magickRequest.data)
processFiles()
}

magickWorker.onmessage = e => {
const response = e.data
const promise = magickWorkerPromises[response.requestNumber]
delete magickWorkerPromises[response.requestNumber]
const result = {
outputFiles: response.outputFiles,
stdout: response.stdout,
stderr: response.stderr,
exitCode: response.exitCode || 0,
}
promise.resolve(result)
}

Hey @KnicKnic - did it improve performance ? (sorry to be just a spectator lately - promise will resume my work in the short term here since I have a couple of features in my branch and will need to use the library). Thanks!

@cancerberoSgx perf not really(copying a gig of ram doesn't use that much cpu(theoretically the max it could have saved in copying before would have only been .33GB saved twice(coming and going) for a total of .66GB) ). If I remember correctly there were scenarios with many files that it helped reduce the ram usage to such a point where it wouldn't exhaust the 1GB limit in chrome.