Skip to content

Commit

Permalink
perf(Python): Re-use WASI pipelines
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed May 26, 2023
1 parent 1abfd42 commit 8ae95dc
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from importlib_resources import files as file_resources

_pipeline = None

from itkwasm import (
InterfaceTypes,
PipelineOutput,
Expand Down Expand Up @@ -37,7 +39,9 @@ def compress_stringify(
:return: Output compressed binary
:rtype: bytes
"""
pipeline = Pipeline(file_resources('itkwasm_compress_stringify_wasi').joinpath(Path('wasm_modules') / Path('compress-stringify.wasi.wasm')))
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('itkwasm_compress_stringify_wasi').joinpath(Path('wasm_modules') / Path('compress-stringify.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.BinaryStream),
Expand Down Expand Up @@ -65,9 +69,7 @@ def compress_stringify(
args.append(str(data_url_prefix))


outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs)

del pipeline
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = outputs[0].data.data
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from importlib_resources import files as file_resources

_pipeline = None

from itkwasm import (
InterfaceTypes,
PipelineOutput,
Expand All @@ -29,7 +31,9 @@ def parse_string_decompress(
:return: Output decompressed binary
:rtype: bytes
"""
pipeline = Pipeline(file_resources('itkwasm_compress_stringify_wasi').joinpath(Path('wasm_modules') / Path('parse-string-decompress.wasi.wasm')))
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('itkwasm_compress_stringify_wasi').joinpath(Path('wasm_modules') / Path('parse-string-decompress.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.BinaryStream),
Expand All @@ -49,9 +53,7 @@ def parse_string_decompress(
args.append('--parse-string')


outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs)

del pipeline
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = outputs[0].data.data
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from importlib_resources import files as file_resources

_pipeline = None

from itkwasm import (
InterfaceTypes,
PipelineOutput,
Expand Down Expand Up @@ -49,7 +51,9 @@ def apply_presentation_state_to_image(
:return: Output image
:rtype: Image
"""
pipeline = Pipeline(file_resources('itkwasm_dicom_wasi').joinpath(Path('wasm_modules') / Path('apply-presentation-state-to-image.wasi.wasm')))
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('itkwasm_dicom_wasi').joinpath(Path('wasm_modules') / Path('apply-presentation-state-to-image.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.JsonObject),
Expand Down Expand Up @@ -84,9 +88,7 @@ def apply_presentation_state_to_image(
args.append('--no-bitmap-output')


outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs)

del pipeline
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = (
outputs[0].data.data,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from importlib_resources import files as file_resources

_pipeline = None

from itkwasm import (
InterfaceTypes,
PipelineOutput,
Expand Down Expand Up @@ -86,7 +88,9 @@ def read_dicom_encapsulated_pdf(
:return: Output pdf file
:rtype: bytes
"""
pipeline = Pipeline(file_resources('itkwasm_dicom_wasi').joinpath(Path('wasm_modules') / Path('read-dicom-encapsulated-pdf.wasi.wasm')))
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('itkwasm_dicom_wasi').joinpath(Path('wasm_modules') / Path('read-dicom-encapsulated-pdf.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.BinaryStream),
Expand Down Expand Up @@ -148,9 +152,7 @@ def read_dicom_encapsulated_pdf(
args.append('--disable-correction')


outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs)

del pipeline
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = outputs[0].data.data
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from importlib_resources import files as file_resources

_pipeline = None

from itkwasm import (
InterfaceTypes,
PipelineOutput,
Expand Down Expand Up @@ -187,7 +189,9 @@ def structured_report_to_html(
:return: Output text file
:rtype: str
"""
pipeline = Pipeline(file_resources('itkwasm_dicom_wasi').joinpath(Path('wasm_modules') / Path('structured-report-to-html.wasi.wasm')))
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('itkwasm_dicom_wasi').joinpath(Path('wasm_modules') / Path('structured-report-to-html.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.TextStream),
Expand Down Expand Up @@ -332,9 +336,7 @@ def structured_report_to_html(
args.append('--code-details-tooltip')


outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs)

del pipeline
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = outputs[0].data.data
return result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from importlib_resources import files as file_resources

_pipeline = None

from itkwasm import (
InterfaceTypes,
PipelineOutput,
Expand Down Expand Up @@ -94,7 +96,9 @@ def structured_report_to_text(
:return: Output text file
:rtype: str
"""
pipeline = Pipeline(file_resources('itkwasm_dicom_wasi').joinpath(Path('wasm_modules') / Path('structured-report-to-text.wasi.wasm')))
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('itkwasm_dicom_wasi').joinpath(Path('wasm_modules') / Path('structured-report-to-text.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.TextStream),
Expand Down Expand Up @@ -162,9 +166,7 @@ def structured_report_to_text(
args.append('--print-color')


outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs)

del pipeline
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = outputs[0].data.data
return result
Expand Down
10 changes: 6 additions & 4 deletions src/bindgen/python.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ from typing import Dict, Tuple, Optional, List
from importlib_resources import files as file_resources
_pipeline = None
from itkwasm import (
InterfaceTypes,
PipelineOutput,
Expand Down Expand Up @@ -465,7 +467,9 @@ from itkwasm import (
moduleContent += `def ${functionName}(
${functionArgs}) -> ${returnType}:
${docstring}
pipeline = Pipeline(file_resources('${pypackage}').joinpath(Path('wasm_modules') / Path('${interfaceJson.name}.wasi.wasm')))
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('${pypackage}').joinpath(Path('wasm_modules') / Path('${interfaceJson.name}.wasi.wasm')))
pipeline_outputs: List[PipelineOutput] = [
${pipelineOutputs} ]
Expand All @@ -474,9 +478,7 @@ ${pipelineOutputs} ]
${pipelineInputs} ]
${args}
outputs = pipeline.run(args, pipeline_outputs, pipeline_inputs)
del pipeline
outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)
${postOutput}
`
Expand Down

0 comments on commit 8ae95dc

Please sign in to comment.