Skip to content

Commit bafb06b

Browse files
mrousavychrispader
andauthored
fix: Avoid a bunch of heap allocations (shared_ptr/unique_ptr) (#130)
* fix: Avoid a bunch of heap allocations (shared_ptr/unique_ptr) * fix: Avoid shared_ptr allocation for vector * fix: Specs * fix: `HybridNitroSQLiteOnLoad` implementation in nitrogen generated directory * fix: remove old file * fix: std::nullopt not callable * fix: pass std::nullopt instead of NULL pointer to BatchQuery * fix: make insertId optional again * fix: pointer access * fix: missing HybridObject base constructor call * chore: update Podfile.lock --------- Co-authored-by: Christoph Pader <chris@margelo.com>
1 parent bde0dc0 commit bafb06b

File tree

9 files changed

+29
-40
lines changed

9 files changed

+29
-40
lines changed

example/ios/Podfile.lock

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,14 +1906,14 @@ EXTERNAL SOURCES:
19061906
:path: "../../node_modules/react-native/ReactCommon/yoga"
19071907

19081908
SPEC CHECKSUMS:
1909-
boost: 1dca942403ed9342f98334bf4c3621f011aa7946
1910-
DoubleConversion: f16ae600a246532c4020132d54af21d0ddb2a385
1909+
boost: 7e761d76ca2ce687f7cc98e698152abd03a18f90
1910+
DoubleConversion: cb417026b2400c8f53ae97020b2be961b59470cb
19111911
fast_float: 06eeec4fe712a76acc9376682e4808b05ce978b6
19121912
FBLazyVector: 6fe148afcef2e3213e484758e3459609d40d57f5
19131913
fmt: a40bb5bd0294ea969aaaba240a927bd33d878cdd
1914-
glog: 08b301085f15bcbb6ff8632a8ebaf239aae04e6a
1914+
glog: eb93e2f488219332457c3c4eafd2738ddc7e80b8
19151915
hermes-engine: b417d2b2aee3b89b58e63e23a51e02be91dc876d
1916-
NitroModules: 30b9dd66a1fffca12828563c9a3648c62758da65
1916+
NitroModules: 32f6f1111b1c77b51e7907aa92f4de3b1a1764bc
19171917
RCT-Folly: 36fe2295e44b10d831836cc0d1daec5f8abcf809
19181918
RCTDeprecation: b2eecf2d60216df56bc5e6be5f063826d3c1ee35
19191919
RCTRequired: 78522de7dc73b81f3ed7890d145fa341f5bb32ea
@@ -1936,7 +1936,7 @@ SPEC CHECKSUMS:
19361936
React-idlecallbacksnativemodule: dd2af19cdd3bc55149d17a2409ed72b694dfbe9c
19371937
React-ImageManager: a77dde8d5aa6a2b6962c702bf3a47695ef0aa32b
19381938
React-jserrorhandler: 9c14e89f12d5904257a79aaf84a70cd2e5ac07ba
1939-
React-jsi: f25e8fac296fc696d7f2a5275ef35e43c84e38c0
1939+
React-jsi: 0775a66820496769ad83e629f0f5cce621a57fc7
19401940
React-jsiexecutor: 2cf5ba481386803f3c88b85c63fa102cba5d769e
19411941
React-jsinspector: 8052d532bb7a98b6e021755674659802fb140cc5
19421942
React-jsinspectortracing: bdd8fd0adcb4813663562e7874c5842449df6d8a

package/cpp/operations.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cmath>
66
#include <ctime>
77
#include <iostream>
8+
#include <optional>
89
#include <map>
910
#include <sqlite3.h>
1011
#include <sstream>
@@ -143,8 +144,8 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str
143144
std::string column_name;
144145
ColumnType column_declared_type;
145146
SQLiteQueryResultRow row;
146-
auto results = std::make_unique<SQLiteQueryResults>();
147-
auto metadata = new SQLiteQueryTableMetadata();
147+
SQLiteQueryResults results;
148+
std::optional<SQLiteQueryTableMetadata> metadata = std::nullopt;
148149

149150
while (isConsuming) {
150151
result = sqlite3_step(statement);
@@ -192,7 +193,7 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str
192193
}
193194
i++;
194195
}
195-
results->push_back(std::move(row));
196+
results.push_back(std::move(row));
196197
break;
197198
case SQLITE_DONE:
198199
i = 0;
@@ -202,6 +203,10 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str
202203
const char* tp = sqlite3_column_decltype(statement, i);
203204
column_declared_type = mapSQLiteTypeToColumnType(tp);
204205
auto columnMeta = SQLiteQueryColumnMetadata(std::move(column_name), std::move(column_declared_type), i);
206+
207+
if (!metadata) {
208+
metadata = std::make_optional<SQLiteQueryTableMetadata>();
209+
}
205210
metadata->insert({column_name, columnMeta});
206211
i++;
207212
}
@@ -221,16 +226,11 @@ SQLiteExecuteQueryResult sqliteExecute(const std::string& dbName, const std::str
221226

222227
int rowsAffected = sqlite3_changes(db);
223228
long long latestInsertRowId = sqlite3_last_insert_rowid(db);
224-
auto meta = std::make_unique<std::optional<SQLiteQueryTableMetadata>>(
225-
metadata && metadata->size() > 0
226-
? std::make_optional(std::move(*metadata))
227-
: std::nullopt
228-
);
229229
return {
230230
.rowsAffected = rowsAffected,
231231
.insertId = static_cast<double>(latestInsertRowId),
232232
.results = std::move(results),
233-
.metadata = std::move(meta)
233+
.metadata = std::move(metadata)
234234
};
235235
}
236236

package/cpp/specs/HybridNativeQueryResult.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
namespace margelo::nitro::rnnitrosqlite {
44

55
std::optional<double> HybridNativeQueryResult::getInsertId() {
6-
return this->_insertId;
6+
return _result.insertId;
77
}
88

99
double HybridNativeQueryResult::getRowsAffected() {
10-
return this->_rowsAffected;
10+
return _result.rowsAffected;
1111
}
1212

1313
SQLiteQueryResults HybridNativeQueryResult::getResults() {
14-
return this->_results;
14+
return _result.results;
1515
};
1616

1717
std::optional<SQLiteQueryTableMetadata> HybridNativeQueryResult::getMetadata() {
18-
return this->_metadata;
18+
return _result.metadata;
1919
}
2020

2121
} // namespace margelo::nitro::rnnitrosqlite

package/cpp/specs/HybridNativeQueryResult.hpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,16 @@ namespace margelo::nitro::rnnitrosqlite {
1111
class HybridNativeQueryResult : public HybridNativeQueryResultSpec {
1212
public:
1313
HybridNativeQueryResult() : HybridObject(TAG) {}
14-
HybridNativeQueryResult(std::optional<double> insertId, int rowsAffected, SQLiteQueryResults& results, std::optional<SQLiteQueryTableMetadata>& metadata)
15-
: HybridObject(TAG),
16-
_insertId(insertId),
17-
_rowsAffected(rowsAffected),
18-
_results(std::move(results)),
19-
_metadata(std::move(metadata)) {}
14+
HybridNativeQueryResult(SQLiteExecuteQueryResult&& result): HybridObject(TAG), _result(std::move(result)) {}
2015

2116
private:
22-
std::optional<double> _insertId;
23-
int _rowsAffected;
24-
SQLiteQueryResults _results;
25-
std::optional<SQLiteQueryTableMetadata> _metadata;
17+
SQLiteExecuteQueryResult _result;
2618

2719
public:
2820
// Properties
2921
std::optional<double> getInsertId() override;
30-
3122
double getRowsAffected() override;
32-
3323
SQLiteQueryResults getResults() override;
34-
3524
std::optional<SQLiteQueryTableMetadata> getMetadata() override;
3625
};
3726

package/cpp/specs/HybridNitroSQLite.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ using ExecuteQueryResult = std::shared_ptr<HybridNativeQueryResultSpec>;
5454

5555
ExecuteQueryResult HybridNitroSQLite::execute(const std::string& dbName, const std::string& query,
5656
const std::optional<SQLiteQueryParams>& params) {
57-
auto result = sqliteExecute(dbName, query, params);
58-
return std::make_shared<HybridNativeQueryResult>(result.insertId, result.rowsAffected, *result.results, *result.metadata);
57+
SQLiteExecuteQueryResult result = sqliteExecute(dbName, query, params);
58+
return std::make_shared<HybridNativeQueryResult>(std::move(result));
5959
};
6060

6161
std::shared_ptr<Promise<std::shared_ptr<HybridNativeQueryResultSpec>>> HybridNitroSQLite::executeAsync(const std::string& dbName, const std::string& query,

package/cpp/sqliteExecuteBatch.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ std::vector<BatchQuery> batchParamsToCommands(const std::vector<BatchQueryComman
1919
if (std::holds_alternative<NestedParamsVec>(*command.params)) {
2020
// This arguments is an array of arrays, like a batch update of a single sql command.
2121
for (const auto& params : std::get<NestedParamsVec>(*command.params)) {
22-
commands.push_back(BatchQuery{command.query, std::make_shared<ParamsVec>(params)});
22+
commands.push_back(BatchQuery{command.query, ParamsVec(params)});
2323
}
2424
} else {
25-
commands.push_back(BatchQuery{command.query, std::make_shared<ParamsVec>(std::move(std::get<ParamsVec>(*command.params)))});
25+
commands.push_back(BatchQuery{command.query, std::move(std::get<ParamsVec>(*command.params))});
2626
}
2727
} else {
28-
commands.push_back(BatchQuery{command.query, NULL});
28+
commands.push_back(BatchQuery{command.query, std::nullopt});
2929
}
3030
}
3131

@@ -48,7 +48,7 @@ SQLiteOperationResult sqliteExecuteBatch(const std::string& dbName, const std::v
4848
auto results = SQLiteQueryResults();
4949
auto metadata = std::optional<SQLiteQueryTableMetadata>(std::nullopt);
5050
try {
51-
auto result = sqliteExecute(dbName, command.sql, *command.params.get());
51+
auto result = sqliteExecute(dbName, command.sql, command.params);
5252
rowsAffected += result.rowsAffected;
5353
} catch (NitroSQLiteException& e) {
5454
sqliteExecuteLiteral(dbName, "ROLLBACK");

package/cpp/sqliteExecuteBatch.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace margelo::rnnitrosqlite {
1313

1414
struct BatchQuery {
1515
std::string sql;
16-
std::shared_ptr<SQLiteQueryParams> params;
16+
std::optional<SQLiteQueryParams> params;
1717
};
1818

1919
/**

package/cpp/types.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ struct SQLiteExecuteQueryResult {
2727
int rowsAffected;
2828
double insertId;
2929

30-
std::unique_ptr<SQLiteQueryResults> results;
31-
std::unique_ptr<std::optional<SQLiteQueryTableMetadata>> metadata;
30+
SQLiteQueryResults results;
31+
std::optional<SQLiteQueryTableMetadata> metadata;
3232
};
3333

3434
// constexpr function that maps SQLiteColumnType to string literals

0 commit comments

Comments
 (0)