Skip to content
This repository was archived by the owner on Feb 22, 2026. It is now read-only.

Commit 80d3a1d

Browse files
authored
chore: Extract make owning buffer (software-mansion#604)
## Description Adding constructors to `OwningArrayBuffer` that accept data via pointer or vector. ### Introduces a breaking change? - [ ] Yes - [x] No ### Type of change - [ ] Bug fix (change which fixes an issue) - [ ] New feature (change which adds functionality) - [ ] Documentation update (improves or adds clarity to existing documentation) - [x] Other (chores, tests, code style improvements etc.) ### Tested on - [ ] iOS - [x] Android ### Testing instructions <!-- Provide step-by-step instructions on how to test your changes. Include setup details if necessary. --> ### Screenshots <!-- Add screenshots here, if applicable --> ### Related issues Closes software-mansion#584 ### Checklist - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [ ] I have updated the documentation accordingly - [x] My changes generate no new warnings ### Additional notes <!-- Include any additional information, assumptions, or context that reviewers might need to understand this PR. -->
1 parent 45b029a commit 80d3a1d

6 files changed

Lines changed: 32 additions & 41 deletions

File tree

packages/react-native-executorch/common/rnexecutorch/jsi/OwningArrayBuffer.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <jsi/jsi.h>
4+
#include <vector>
45

56
namespace rnexecutorch {
67

@@ -19,8 +20,24 @@ using namespace facebook;
1920
*/
2021
class OwningArrayBuffer : public jsi::MutableBuffer {
2122
public:
22-
OwningArrayBuffer(const size_t size) : size_(size) {
23-
data_ = new uint8_t[size];
23+
/**
24+
* @param size Size of the buffer in bytes.
25+
*/
26+
OwningArrayBuffer(size_t size) : size_(size) {
27+
data_ = new uint8_t[size_];
28+
}
29+
/**
30+
* @param data Pointer to the source data.
31+
* @param size Size of the data in bytes.
32+
*/
33+
OwningArrayBuffer(const auto &data, size_t size) : size_(size) {
34+
data_ = new uint8_t[size_];
35+
std::memcpy(data_, data, size_);
36+
}
37+
template <typename T>
38+
OwningArrayBuffer(const std::vector<T> &vec) : size_(vec.size() * sizeof(T)) {
39+
data_ = new uint8_t[size_];
40+
std::memcpy(data_, vec.data(), size_);
2441
}
2542
~OwningArrayBuffer() override { delete[] data_; }
2643

packages/react-native-executorch/common/rnexecutorch/models/BaseModel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ BaseModel::forwardJS(std::vector<JSTensorViewIn> tensorViewVec) {
126126
auto &outputTensor = outputs[i].toTensor();
127127
std::vector<int32_t> sizes = getTensorShape(outputTensor);
128128
size_t bufferSize = outputTensor.numel() * outputTensor.element_size();
129-
auto buffer = std::make_shared<OwningArrayBuffer>(bufferSize);
130-
std::memcpy(buffer->data(), outputTensor.const_data_ptr(), bufferSize);
129+
auto buffer = std::make_shared<OwningArrayBuffer>(
130+
outputTensor.const_data_ptr(), bufferSize);
131131
auto jsTensor = JSTensorViewOut(sizes, outputTensor.scalar_type(), buffer);
132132
output.emplace_back(jsTensor);
133133
}

packages/react-native-executorch/common/rnexecutorch/models/embeddings/BaseEmbeddings.cpp

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,9 @@ BaseEmbeddings::BaseEmbeddings(const std::string &modelSource,
1111
std::shared_ptr<OwningArrayBuffer>
1212
BaseEmbeddings::postprocess(const Result<std::vector<EValue>> &forwardResult) {
1313
auto forwardResultTensor = forwardResult->at(0).toTensor();
14-
auto dataPtr = forwardResultTensor.mutable_data_ptr();
15-
auto outputNumel = forwardResultTensor.numel();
16-
17-
std::span<float> modelOutput(static_cast<float *>(dataPtr), outputNumel);
18-
19-
auto createBuffer = [](const auto &data, size_t size) {
20-
auto buffer = std::make_shared<OwningArrayBuffer>(size);
21-
std::memcpy(buffer->data(), data, size);
22-
return buffer;
23-
};
24-
return createBuffer(modelOutput.data(), modelOutput.size_bytes());
14+
auto buffer = std::make_shared<OwningArrayBuffer>(
15+
forwardResultTensor.const_data_ptr(), forwardResultTensor.nbytes());
16+
return buffer;
2517
}
2618

2719
} // namespace rnexecutorch::models::embeddings

packages/react-native-executorch/common/rnexecutorch/models/image_segmentation/ImageSegmentation.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,9 @@ std::shared_ptr<jsi::Object> ImageSegmentation::postprocess(
6262
std::vector<std::shared_ptr<OwningArrayBuffer>> resultClasses;
6363
resultClasses.reserve(numClasses);
6464
for (std::size_t cl = 0; cl < numClasses; ++cl) {
65-
auto classBuffer =
66-
std::make_shared<OwningArrayBuffer>(numModelPixels * sizeof(float));
65+
auto classBuffer = std::make_shared<OwningArrayBuffer>(
66+
&resultData[cl * numModelPixels], numModelPixels * sizeof(float));
6767
resultClasses.push_back(classBuffer);
68-
std::memcpy(classBuffer->data(), &resultData[cl * numModelPixels],
69-
numModelPixels * sizeof(float));
7068
}
7169

7270
// Apply softmax per each pixel across all classes
@@ -112,18 +110,14 @@ std::shared_ptr<jsi::Object> ImageSegmentation::postprocess(
112110
cv::Mat argmaxMat(modelImageSize, CV_32SC1, argmax->data());
113111
cv::resize(argmaxMat, argmaxMat, originalSize, 0, 0,
114112
cv::InterpolationFlags::INTER_NEAREST);
115-
argmax = std::make_shared<OwningArrayBuffer>(originalSize.area() *
116-
sizeof(int32_t));
117-
std::memcpy(argmax->data(), argmaxMat.data,
118-
originalSize.area() * sizeof(int32_t));
113+
argmax = std::make_shared<OwningArrayBuffer>(
114+
argmaxMat.data, originalSize.area() * sizeof(int32_t));
119115

120116
for (auto &[label, arrayBuffer] : *buffersToReturn) {
121117
cv::Mat classMat(modelImageSize, CV_32FC1, arrayBuffer->data());
122118
cv::resize(classMat, classMat, originalSize);
123-
arrayBuffer = std::make_shared<OwningArrayBuffer>(originalSize.area() *
124-
sizeof(float));
125-
std::memcpy(arrayBuffer->data(), classMat.data,
126-
originalSize.area() * sizeof(float));
119+
arrayBuffer = std::make_shared<OwningArrayBuffer>(
120+
classMat.data, originalSize.area() * sizeof(float));
127121
}
128122
}
129123
return populateDictionary(argmax, buffersToReturn);

packages/react-native-executorch/common/rnexecutorch/models/speech_to_text/SpeechToText.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ void SpeechToText::unload() noexcept {
3131
std::shared_ptr<OwningArrayBuffer>
3232
SpeechToText::encode(std::span<float> waveform) const {
3333
std::vector<float> encoderOutput = this->asr->encode(waveform);
34-
return this->makeOwningBuffer(encoderOutput);
34+
return std::make_shared<OwningArrayBuffer>(encoderOutput);
3535
}
3636

3737
std::shared_ptr<OwningArrayBuffer>
3838
SpeechToText::decode(std::span<int32_t> tokens,
3939
std::span<float> encoderOutput) const {
4040
std::vector<float> decoderOutput = this->asr->decode(tokens, encoderOutput);
41-
return this->makeOwningBuffer(decoderOutput);
41+
return std::make_shared<OwningArrayBuffer>(decoderOutput);
4242
}
4343

4444
std::string SpeechToText::transcribe(std::span<float> waveform,
@@ -68,15 +68,6 @@ size_t SpeechToText::getMemoryLowerBound() const noexcept {
6868
this->decoder->getMemoryLowerBound();
6969
}
7070

71-
std::shared_ptr<OwningArrayBuffer>
72-
SpeechToText::makeOwningBuffer(std::span<const float> vectorView) const {
73-
auto owningArrayBuffer =
74-
std::make_shared<OwningArrayBuffer>(vectorView.size_bytes());
75-
std::memcpy(owningArrayBuffer->data(), vectorView.data(),
76-
vectorView.size_bytes());
77-
return owningArrayBuffer;
78-
}
79-
8071
void SpeechToText::stream(std::shared_ptr<jsi::Function> callback,
8172
std::string languageOption) {
8273
if (this->isStreaming) {

packages/react-native-executorch/common/rnexecutorch/models/speech_to_text/SpeechToText.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ class SpeechToText {
3434
std::unique_ptr<TokenizerModule> tokenizer;
3535
std::unique_ptr<asr::ASR> asr;
3636

37-
std::shared_ptr<OwningArrayBuffer>
38-
makeOwningBuffer(std::span<const float> vectorView) const;
39-
4037
// Stream
4138
std::shared_ptr<react::CallInvoker> callInvoker;
4239
std::unique_ptr<stream::OnlineASRProcessor> processor;

0 commit comments

Comments
 (0)