diff --git a/wasm/bindings/TranslationModelBindings.cpp b/wasm/bindings/TranslationModelBindings.cpp index 3fd2c8b..4ee9265 100644 --- a/wasm/bindings/TranslationModelBindings.cpp +++ b/wasm/bindings/TranslationModelBindings.cpp @@ -13,31 +13,33 @@ using namespace emscripten; typedef marian::bergamot::Service TranslationModel; typedef marian::bergamot::Response TranslationResult; +typedef marian::bergamot::AlignedMemory AlignedMemory; -val getByteArrayView(marian::bergamot::AlignedMemory& alignedMemory) { +val getByteArrayView(AlignedMemory& alignedMemory) { return val(typed_memory_view(alignedMemory.size(), alignedMemory.as())); } EMSCRIPTEN_BINDINGS(aligned_memory) { - class_("AlignedMemory") + class_("AlignedMemory") .constructor() - .function("size", &marian::bergamot::AlignedMemory::size) + .function("size", &AlignedMemory::size) .function("getByteArrayView", &getByteArrayView) ; - register_vector("AlignedMemoryList"); + register_vector("AlignedMemoryList"); } -std::vector> -prepareVocabsSmartMemories(std::vector& vocabsMemories) { - auto sourceVocabMemory = std::make_shared(std::move(*(vocabsMemories[0]))); - std::vector> vocabsSmartMemories; +// When source and target vocab files are same, only one memory object is passed from JS to +// avoid allocating memory twice for the same file. However, the constructor of the TranslationModel +// class still expects 2 entries in this case, where each entry has the shared ownership of the +// same AlignedMemory object. This function prepares these smart pointer based AlignedMemory objects +// for unique AlignedMemory objects passed from JS. +std::vector> prepareVocabsSmartMemories(std::vector& vocabsMemories) { + auto sourceVocabMemory = std::make_shared(std::move(*(vocabsMemories[0]))); + std::vector> vocabsSmartMemories; vocabsSmartMemories.push_back(sourceVocabMemory); - // When source and target vocab files are same, only one memory object is passed in vocabsMemories - // to avoid double memory allocation for the same file. However, the constructor of the TranslationModel - // class still expects 2 entries where each entry has the shared ownership of a single AlignedMemory object. if (vocabsMemories.size() == 2) { - auto targetVocabMemory = std::make_shared(std::move(*(vocabsMemories[1]))); + auto targetVocabMemory = std::make_shared(std::move(*(vocabsMemories[1]))); vocabsSmartMemories.push_back(std::move(targetVocabMemory)); } else { @@ -47,9 +49,9 @@ prepareVocabsSmartMemories(std::vector& vocabs } TranslationModel* TranslationModelFactory(const std::string &config, - marian::bergamot::AlignedMemory* modelMemory, - marian::bergamot::AlignedMemory* shortlistMemory, - std::vector uniqueVocabsMemories) { + AlignedMemory* modelMemory, + AlignedMemory* shortlistMemory, + std::vector uniqueVocabsMemories) { return new TranslationModel(config, std::move(*modelMemory), std::move(*shortlistMemory),