Skip to content

Commit b31c32f

Browse files
Remove std::format from c++ code to support _LIBCPP_STD_VER < 20 (#571)
## Description the c++ 20 `std::format` relies on c++ 17 `std::to_char` which was introduced later in libc++ effectively meaning that no build with target < iOS 16.3 can use `std::format`, (see [this](https://developer.apple.com/xcode/cpp/#c++17)) The simplest solution is to basically remove all `std::format` for now. The more future-realistic solutions could be considered. To see the more verbose discussion go to #570. ### Introduces a breaking change? - [ ] Yes - [x] No ### Type of change - [x] Bug fix (change which fixes an issue) - [ ] New feature (change which adds functionality) - [ ] Documentation update (improves or adds clarity to existing documentation) - [ ] Other (chores, tests, code style improvements etc.) ### Tested on - [x] iOS - [ ] 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 <!-- Link related issues here using #issue-number --> ### Checklist - [x] I have performed a self-review of my code - [ ] 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 1cf016a commit b31c32f

3 files changed

Lines changed: 19 additions & 20 deletions

File tree

packages/react-native-executorch/common/rnexecutorch/RnExecutorchInstaller.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#if defined(__ANDROID__) && defined(__aarch64__)
1717
#include <executorch/extension/threadpool/cpuinfo_utils.h>
1818
#include <executorch/extension/threadpool/threadpool.h>
19-
#include <format>
2019
#include <rnexecutorch/Log.h>
2120
#endif
2221

@@ -96,8 +95,7 @@ void RnExecutorchInstaller::injectJSIBindings(
9695
#if defined(__ANDROID__) && defined(__aarch64__)
9796
auto num_of_perf_cores =
9897
::executorch::extension::cpuinfo::get_num_performant_cores();
99-
log(LOG_LEVEL::Info,
100-
std::format("Detected {} performant cores", num_of_perf_cores));
98+
log(LOG_LEVEL::Info, "Detected ", num_of_perf_cores, " performant cores");
10199
// setting num_of_cores to floor(num_of_perf_cores / 2) + 1) because depending
102100
// on cpu arch as when possible we want to leave at least 2 performant cores
103101
// for other tasks (setting more actually results in drop of performance). For
@@ -107,8 +105,7 @@ void RnExecutorchInstaller::injectJSIBindings(
107105
auto num_of_cores = static_cast<uint32_t>(num_of_perf_cores / 2) + 1;
108106
::executorch::extension::threadpool::get_threadpool()
109107
->_unsafe_reset_threadpool(num_of_cores);
110-
log(LOG_LEVEL::Info,
111-
std::format("Configuring xnnpack for {} threads", num_of_cores));
108+
log(LOG_LEVEL::Info, "Configuring xnnpack for ", num_of_cores, " threads", );
112109
#endif
113110
}
114111

packages/react-native-executorch/common/rnexecutorch/data_processing/Numerical.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
#include <algorithm>
44
#include <cmath>
5-
#include <format>
65
#include <limits>
76
#include <numeric>
7+
#include <sstream>
88
#include <string>
99

1010
namespace rnexecutorch::numerical {
@@ -44,12 +44,13 @@ void normalize(std::span<float> input) {
4444
std::vector<float> meanPooling(std::span<const float> modelOutput,
4545
std::span<const int64_t> attnMask) {
4646
if (attnMask.empty() || modelOutput.size() % attnMask.size() != 0) {
47-
throw std::invalid_argument(
48-
std::format("Invalid dimensions for mean pooling, expected model "
49-
"output size to be divisible "
50-
"by the size of attention mask but got size: {} for model "
51-
"output and size: {} for attention mask",
52-
modelOutput.size(), attnMask.size()));
47+
std::stringstream ss;
48+
ss << "Invalid dimensions for mean pooling, expected model output size to "
49+
"be divisible "
50+
<< "by the size of attention mask but got size: " << modelOutput.size()
51+
<< " for model output and size: " << attnMask.size()
52+
<< " for attention mask";
53+
throw std::invalid_argument(ss.str());
5354
}
5455

5556
auto attnMaskLength = attnMask.size();

packages/react-native-executorch/common/rnexecutorch/host_objects/ModelHostObject.h

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

3-
#include <format>
43
#include <ReactCommon/CallInvoker.h>
4+
#include <sstream>
55
#include <string>
66
#include <thread>
77
#include <tuple>
@@ -110,10 +110,10 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
110110
template <auto FnPtr> JSI_HOST_FUNCTION(synchronousHostFunction) {
111111
constexpr std::size_t functionArgCount = meta::getArgumentCount(FnPtr);
112112
if (functionArgCount != count) {
113-
const auto errorMessage = std::format(
114-
"Argument count mismatch, was expecting: {} but got: {}",
115-
functionArgCount, count
116-
);
113+
std::stringstream ss;
114+
ss << "Argument count mismatch, was expecting: " << functionArgCount
115+
<< " but got: " << count;
116+
const auto errorMessage = ss.str();
117117
throw jsi::JSError(runtime, errorMessage);
118118
}
119119

@@ -156,9 +156,10 @@ template <typename Model> class ModelHostObject : public JsiHostObject {
156156
constexpr std::size_t functionArgCount =
157157
meta::getArgumentCount(FnPtr);
158158
if (functionArgCount != count) {
159-
const auto errorMessage = std::format(
160-
"Argument count mismatch, was expecting: {} but got: {}",
161-
functionArgCount, count);
159+
std::stringstream ss;
160+
ss << "Argument count mismatch, was expecting: " << functionArgCount
161+
<< " but got: " << count;
162+
const auto errorMessage = ss.str();
162163
promise->reject(errorMessage);
163164
return;
164165
}

0 commit comments

Comments
 (0)