Skip to content

Commit

Permalink
perf: Mark error paths as [[unlikely]] (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy authored Jan 19, 2024
1 parent 0313eec commit bbbdff2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 6 additions & 3 deletions cpp/TensorHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ size_t TensorHelpers::getTFLTensorDataTypeSize(TfLiteType dataType) {
case kTfLiteUInt16:
return sizeof(uint16_t);
default:
[[unlikely]];
throw std::runtime_error("Unsupported output data type! " + dataTypeToString(dataType));
}
}
Expand Down Expand Up @@ -125,8 +126,8 @@ TypedArrayBase TensorHelpers::createJSBufferForTensor(jsi::Runtime& runtime,
return TypedArray<TypedArrayKind::Uint16Array>(runtime, size);
case kTfLiteUInt32:
return TypedArray<TypedArrayKind::Uint32Array>(runtime, size);

default:
[[unlikely]];
throw std::runtime_error("Unsupported tensor data type! " + dataTypeToString(dataType));
}
}
Expand All @@ -138,6 +139,7 @@ void TensorHelpers::updateJSBufferFromTensor(jsi::Runtime& runtime, TypedArrayBa

void* data = TfLiteTensorData(tensor);
if (data == nullptr) {
[[unlikely]];
throw std::runtime_error("Failed to get data from tensor \"" + name + "\"!");
}

Expand Down Expand Up @@ -185,8 +187,8 @@ void TensorHelpers::updateJSBufferFromTensor(jsi::Runtime& runtime, TypedArrayBa
.as<TypedArrayKind::Uint32Array>(runtime)
.updateUnsafe(runtime, (uint32_t*)data, size);
break;

default:
[[unlikely]];
throw jsi::JSError(runtime, "Unsupported output data type! " + dataTypeToString(dataType));
}
}
Expand All @@ -195,11 +197,12 @@ void TensorHelpers::updateTensorFromJSBuffer(jsi::Runtime& runtime, TfLiteTensor
TypedArrayBase& jsBuffer) {
auto name = std::string(TfLiteTensorName(tensor));
auto buffer = jsBuffer.getBuffer(runtime);

#if DEBUG
int inputBufferSize = buffer.size(runtime);
int tensorSize = getTensorTotalLength(tensor) * getTFLTensorDataTypeSize(tensor->type);
if (tensorSize != inputBufferSize) {
[[unlikely]];
throw std::runtime_error("Input Buffer size (" + std::to_string(inputBufferSize) + ") does not "
"match the Input Tensor's expected size (" + std::to_string(tensorSize) + ")! "
"Make sure to resize the input values accordingly.");
Expand Down
5 changes: 5 additions & 0 deletions cpp/TensorflowPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ TensorflowPlugin::TensorflowPlugin(TfLiteInterpreter* interpreter, Buffer model,
// Allocate memory for the model's input/output `TFLTensor`s.
TfLiteStatus status = TfLiteInterpreterAllocateTensors(_interpreter);
if (status != kTfLiteOk) {
[[unlikely]];
throw std::runtime_error("Failed to allocate memory for input/output tensors! Status: " +
std::to_string(status));
}
Expand Down Expand Up @@ -174,6 +175,7 @@ void TensorflowPlugin::copyInputBuffers(jsi::Runtime& runtime, jsi::Object input
auto array = inputValues.asArray(runtime);
size_t count = array.size(runtime);
if (count != TfLiteInterpreterGetInputTensorCount(_interpreter)) {
[[unlikely]];
throw std::runtime_error(
"TFLite: Input Values have different size than there are input tensors!");
}
Expand Down Expand Up @@ -203,6 +205,7 @@ void TensorflowPlugin::run() {
// Run Model
TfLiteStatus status = TfLiteInterpreterInvoke(_interpreter);
if (status != kTfLiteOk) {
[[unlikely]];
throw std::runtime_error("Failed to run TFLite Model! Status: " + std::to_string(status));
}
}
Expand Down Expand Up @@ -254,6 +257,7 @@ jsi::Value TensorflowPlugin::get(jsi::Runtime& runtime, const jsi::PropNameID& p
for (size_t i = 0; i < size; i++) {
TfLiteTensor* tensor = TfLiteInterpreterGetInputTensor(_interpreter, i);
if (tensor == nullptr) {
[[unlikely]];
throw jsi::JSError(runtime, "Failed to get input tensor " + std::to_string(i) + "!");
}

Expand All @@ -267,6 +271,7 @@ jsi::Value TensorflowPlugin::get(jsi::Runtime& runtime, const jsi::PropNameID& p
for (size_t i = 0; i < size; i++) {
const TfLiteTensor* tensor = TfLiteInterpreterGetOutputTensor(_interpreter, i);
if (tensor == nullptr) {
[[unlikely]];
throw jsi::JSError(runtime, "Failed to get output tensor " + std::to_string(i) + "!");
}

Expand Down

0 comments on commit bbbdff2

Please sign in to comment.