Skip to content

Commit 87e0c0f

Browse files
authored
fix: Improve memory management (#290)
* refactor: move constexpr to upper scope * fix: metadata size calculation * fix: std::move metadata object * style: format
1 parent 1945cf5 commit 87e0c0f

3 files changed

Lines changed: 6 additions & 7 deletions

File tree

packages/react-native-nitro-sqlite/cpp/hybridObjects/HybridNitroSQLiteQueryResult.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ namespace margelo::nitro::rnnitrosqlite {
1010

1111
namespace {
1212

13+
constexpr size_t nodePadding = 24;
14+
1315
/**
1416
* Compute the approximate external memory size of a single result row.
1517
* This includes:
@@ -18,7 +20,6 @@ namespace {
1820
*/
1921
size_t getRowExternalMemorySize(const SQLiteQueryResultRow& row) {
2022
size_t bucketMemory = row.bucket_count() * sizeof(void*);
21-
constexpr size_t nodePadding = 24;
2223
size_t nodesMemory = row.size() * (sizeof(std::pair<std::string, SQLiteValue>) + nodePadding);
2324
return bucketMemory + nodesMemory;
2425
}
@@ -49,10 +50,10 @@ namespace {
4950
* - Metadata contents, especially the `name` string on each metadata entry.
5051
*/
5152
size_t getMetadataExternalMemorySize(const SQLiteQueryTableMetadata& metadata) {
52-
size_t size = 0;
53+
size_t size = metadata.bucket_count() * sizeof(void*);
54+
size += metadata.size() * (sizeof(SQLiteQueryTableMetadata::value_type) + nodePadding);
5355

5456
for (const auto& [columnName, columnMeta] : metadata) {
55-
5657
size += columnName.capacity();
5758
size += columnMeta.name.capacity();
5859
}

packages/react-native-nitro-sqlite/cpp/hybridObjects/HybridNitroSQLiteQueryResult.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class HybridNitroSQLiteQueryResult : public HybridNitroSQLiteQueryResultSpec {
1313
HybridNitroSQLiteQueryResult() : HybridObject(TAG) {}
1414
HybridNitroSQLiteQueryResult(SQLiteQueryResults results, std::optional<double> insertId, double rowsAffected,
1515
std::optional<SQLiteQueryTableMetadata> metadata)
16-
: HybridObject(TAG), _insertId(insertId), _rowsAffected(rowsAffected), _results(std::move(results)), _metadata(metadata) {}
16+
: HybridObject(TAG), _insertId(insertId), _rowsAffected(rowsAffected), _results(std::move(results)), _metadata(std::move(metadata)) {}
1717

1818
private:
1919
std::optional<double> _insertId;

packages/react-native-nitro-sqlite/cpp/operations.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ using namespace margelo::nitro::rnnitrosqlite;
2525

2626
namespace margelo::rnnitrosqlite {
2727

28-
2928
static constexpr double kInt64MinAsDouble = static_cast<double>(std::numeric_limits<int64_t>::min());
3029
static constexpr double kInt64UpperBoundAsDouble = -kInt64MinAsDouble;
3130

@@ -128,8 +127,7 @@ void bindStatement(sqlite3_stmt* statement, const SQLiteQueryParams& values) {
128127
} else if (std::holds_alternative<double>(value)) {
129128
// Bind whole numbers as INTEGER so vec0 rowid/pk/partition (which reject REAL) work; SQLite still coerces to REAL for REAL columns.
130129
double doubleValue = std::get<double>(value);
131-
if (std::trunc(doubleValue) == doubleValue && doubleValue >= kInt64MinAsDouble &&
132-
doubleValue < kInt64UpperBoundAsDouble) {
130+
if (std::trunc(doubleValue) == doubleValue && doubleValue >= kInt64MinAsDouble && doubleValue < kInt64UpperBoundAsDouble) {
133131
sqlite3_bind_int64(statement, sqliteIndex, static_cast<sqlite3_int64>(doubleValue));
134132
} else {
135133
sqlite3_bind_double(statement, sqliteIndex, doubleValue);

0 commit comments

Comments
 (0)