Skip to content

Commit e5ab4cc

Browse files
sqlite: remove usage of std::pmr
Given that the current changeset is already quite large, and that the use of `std::pmr` is not critical to the implementation, I have decided to remove it for now. This will allow us to focus on the core functionality of the batched async operations without getting bogged down with the optimization of our allocaction strategy.
1 parent c57866e commit e5ab4cc

File tree

1 file changed

+44
-55
lines changed

1 file changed

+44
-55
lines changed

src/node_sqlite.cc

Lines changed: 44 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <cmath>
2424
#include <concepts>
2525
#include <limits>
26-
#include <memory_resource>
2726
#include <variant>
2827

2928
namespace node {
@@ -3861,25 +3860,25 @@ struct real {
38613860
double value;
38623861
};
38633862
struct text {
3864-
std::pmr::string value;
3863+
std::string value;
38653864

38663865
text() = default;
3867-
explicit text(std::pmr::string&& str) : value(std::move(str)) {}
3866+
explicit text(std::string&& str) : value(std::move(str)) {}
38683867
explicit text(std::string_view str) : value(str.data(), str.size()) {}
38693868
text(const char* str, size_t len) : value(str, len) {}
38703869
};
38713870
struct blob {
3872-
std::pmr::vector<uint8_t> value;
3871+
std::vector<uint8_t> value;
38733872

38743873
blob() = default;
3875-
explicit blob(std::pmr::vector<uint8_t>&& vec) : value(std::move(vec)) {}
3874+
explicit blob(std::vector<uint8_t>&& vec) : value(std::move(vec)) {}
38763875
explicit blob(std::span<const uint8_t> span)
38773876
: value(span.begin(), span.end()) {}
38783877
blob(const uint8_t* data, size_t len) : value(data, data + len) {}
38793878
};
38803879
using literal = std::variant<null, boolean, integer, real, text, blob>;
3881-
using value = std::variant<std::pmr::vector<literal>,
3882-
std::pmr::unordered_map<std::pmr::string, literal>,
3880+
using value = std::variant<std::vector<literal>,
3881+
std::unordered_map<std::string, literal>,
38833882
literal>;
38843883

38853884
struct bind_literal {
@@ -3923,7 +3922,7 @@ struct bind_value {
39233922
// bind_value should only be called with vector or map
39243923
return SQLITE_MISUSE;
39253924
}
3926-
int operator()(const std::pmr::vector<transfer::literal>& value) const {
3925+
int operator()(const std::vector<transfer::literal>& value) const {
39273926
if (!std::in_range<int>(value.size())) [[unlikely]] {
39283927
return SQLITE_RANGE;
39293928
}
@@ -3936,8 +3935,7 @@ struct bind_value {
39363935
return SQLITE_OK;
39373936
}
39383937
int operator()(
3939-
const std::pmr::unordered_map<std::pmr::string, transfer::literal>& value)
3940-
const {
3938+
const std::unordered_map<std::string, transfer::literal>& value) const {
39413939
bind_literal binder{stmt, 0};
39423940
for (const auto& [name, value] : value) {
39433941
binder.index = sqlite3_bind_parameter_index(stmt, name.c_str());
@@ -4000,7 +3998,7 @@ value FromRow(sqlite3* connection,
40003998
const int num_cols,
40013999
const statement_options options) {
40024000
if (options.return_arrays) {
4003-
std::pmr::vector<transfer::literal> row;
4001+
std::vector<transfer::literal> row;
40044002
row.reserve(num_cols);
40054003
for (int i = 0; i < num_cols; ++i) {
40064004
row.push_back(
@@ -4011,7 +4009,7 @@ value FromRow(sqlite3* connection,
40114009
// TODO(BurningEnlightenment): share column names between rows
40124010
// => return type should always be a vector of literals, and the caller
40134011
// should add an additional vector of column names if needed
4014-
std::pmr::unordered_map<std::pmr::string, transfer::literal> row;
4012+
std::unordered_map<std::string, transfer::literal> row;
40154013
for (int i = 0; i < num_cols; ++i) {
40164014
const char* col_name = sqlite3_column_name(stmt, i);
40174015
CHECK_NOT_NULL(col_name); // Catch OOM condition.
@@ -4059,8 +4057,7 @@ struct to_v8_value {
40594057
Local<Value> operator()(const transfer::literal& literal) const {
40604058
return std::visit(*this, literal);
40614059
}
4062-
Local<Value> operator()(
4063-
const std::pmr::vector<transfer::literal>& vec) const {
4060+
Local<Value> operator()(const std::vector<transfer::literal>& vec) const {
40644061
Local<Context> context = isolate->GetCurrentContext();
40654062
Local<Array> array = Array::New(isolate, vec.size());
40664063
for (size_t i = 0; i < vec.size(); ++i) {
@@ -4071,8 +4068,7 @@ struct to_v8_value {
40714068
return array;
40724069
}
40734070
Local<Value> operator()(
4074-
const std::pmr::unordered_map<std::pmr::string, transfer::literal>& map)
4075-
const {
4071+
const std::unordered_map<std::string, transfer::literal>& map) const {
40764072
LocalVector<Name> obj_keys(isolate);
40774073
LocalVector<Value> obj_values(isolate);
40784074
for (const auto& [key, value] : map) {
@@ -4126,7 +4122,7 @@ Maybe<transfer::value> ToValue(Isolate* isolate, Local<Object> object) {
41264122
}
41274123
const uint32_t length = property_names->Length();
41284124
Local<Context> context = isolate->GetCurrentContext();
4129-
std::pmr::unordered_map<std::pmr::string, literal> map;
4125+
std::unordered_map<std::string, literal> map;
41304126
map.reserve(length);
41314127
for (uint32_t i = 0; i < length; ++i) {
41324128
Local<Value> key;
@@ -4153,7 +4149,7 @@ Maybe<transfer::value> ToValue(Isolate* isolate, Local<Object> object) {
41534149
Maybe<transfer::value> ToValue(Isolate* isolate, Local<Array> array) {
41544150
const uint32_t length = array->Length();
41554151
Local<Context> context = isolate->GetCurrentContext();
4156-
std::pmr::vector<literal> vec;
4152+
std::vector<literal> vec;
41574153
vec.reserve(length);
41584154
for (uint32_t i = 0; i < length; ++i) {
41594155
Local<Value> value;
@@ -4203,8 +4199,8 @@ class alignas(64) OperationResult {
42034199
const char* error_description = sqlite3_errstr(error_code);
42044200
return Rejected{
42054201
error_code,
4206-
error_message != nullptr ? error_message : std::pmr::string{},
4207-
error_description != nullptr ? error_description : std::pmr::string{},
4202+
error_message != nullptr ? error_message : std::string{},
4203+
error_description != nullptr ? error_description : std::string{},
42084204
};
42094205
}
42104206
static Rejected LastError(sqlite3* connection) {
@@ -4257,14 +4253,14 @@ class alignas(64) OperationResult {
42574253

42584254
private:
42594255
Rejected(int error_code,
4260-
std::pmr::string error_message,
4261-
std::pmr::string error_description)
4256+
std::string error_message,
4257+
std::string error_description)
42624258
: error_message_(std::move(error_message)),
42634259
error_description_(std::move(error_description)),
42644260
error_code_(error_code) {}
42654261

4266-
std::pmr::string error_message_;
4267-
std::pmr::string error_description_;
4262+
std::string error_message_;
4263+
std::string error_description_;
42684264
int error_code_;
42694265
};
42704266
class Void {
@@ -4329,7 +4325,7 @@ class alignas(64) OperationResult {
43294325
};
43304326
class Values {
43314327
public:
4332-
explicit Values(std::pmr::vector<transfer::value> values)
4328+
explicit Values(std::vector<transfer::value> values)
43334329
: values_(std::move(values)) {}
43344330

43354331
void Connect(Environment* env,
@@ -4348,7 +4344,7 @@ class alignas(64) OperationResult {
43484344
}
43494345

43504346
private:
4351-
std::pmr::vector<transfer::value> values_;
4347+
std::vector<transfer::value> values_;
43524348
};
43534349
class RunResult {
43544350
public:
@@ -4395,8 +4391,8 @@ class alignas(64) OperationResult {
43954391
transfer::value&& value) {
43964392
return OperationResult{origin, Value{std::move(value)}};
43974393
}
4398-
static OperationResult ResolveValues(
4399-
OperationBase* origin, std::pmr::vector<transfer::value>&& values) {
4394+
static OperationResult ResolveValues(OperationBase* origin,
4395+
std::vector<transfer::value>&& values) {
44004396
return OperationResult{origin, Values{std::move(values)}};
44014397
}
44024398
static OperationResult ResolveRunResult(OperationBase* origin,
@@ -4436,7 +4432,7 @@ class PrepareStatementOperation : private OperationBase {
44364432
public:
44374433
PrepareStatementOperation(Global<Promise::Resolver>&& resolver,
44384434
BaseObjectPtr<Database>&& db,
4439-
std::pmr::string&& sql,
4435+
std::string&& sql,
44404436
statement_options options = {})
44414437
: OperationBase(std::move(resolver)),
44424438
db_(std::move(db)),
@@ -4455,7 +4451,7 @@ class PrepareStatementOperation : private OperationBase {
44554451

44564452
private:
44574453
BaseObjectPtr<Database> db_;
4458-
std::pmr::string sql_;
4454+
std::string sql_;
44594455
statement_options options_;
44604456
};
44614457

@@ -4534,7 +4530,7 @@ class StatementAllOperation : private OperationBase {
45344530
}
45354531
auto reset_statement = OnScopeLeave([&] { sqlite3_reset(stmt_); });
45364532

4537-
std::pmr::vector<transfer::value> rows;
4533+
std::vector<transfer::value> rows;
45384534
int r;
45394535
int num_cols = sqlite3_column_count(stmt_);
45404536
for (r = sqlite3_step(stmt_); r == SQLITE_ROW; r = sqlite3_step(stmt_)) {
@@ -4585,7 +4581,7 @@ class StatementRunOperation : private OperationBase {
45854581

45864582
class ExecOperation : private OperationBase {
45874583
public:
4588-
ExecOperation(Global<Promise::Resolver>&& resolver, std::pmr::string&& sql)
4584+
ExecOperation(Global<Promise::Resolver>&& resolver, std::string&& sql)
45894585
: OperationBase(std::move(resolver)), sql_(std::move(sql)) {}
45904586

45914587
OperationResult operator()(sqlite3* connection) {
@@ -4597,7 +4593,7 @@ class ExecOperation : private OperationBase {
45974593
}
45984594

45994595
private:
4600-
std::pmr::string sql_;
4596+
std::string sql_;
46014597
};
46024598

46034599
class CloseOperation : private OperationBase {
@@ -4631,8 +4627,7 @@ class IsInTransactionOperation : private OperationBase {
46314627

46324628
class LocationOperation : private OperationBase {
46334629
public:
4634-
LocationOperation(Global<Promise::Resolver>&& resolver,
4635-
std::pmr::string&& db_name)
4630+
LocationOperation(Global<Promise::Resolver>&& resolver, std::string&& db_name)
46364631
: OperationBase(std::move(resolver)), db_name_(std::move(db_name)) {}
46374632

46384633
OperationResult operator()(sqlite3* connection) {
@@ -4646,7 +4641,7 @@ class LocationOperation : private OperationBase {
46464641
}
46474642

46484643
private:
4649-
std::pmr::string db_name_;
4644+
std::string db_name_;
46504645
};
46514646

46524647
class UpdateDbConfigOperation : private OperationBase {
@@ -4673,7 +4668,7 @@ class UpdateDbConfigOperation : private OperationBase {
46734668
class LoadExtensionOperation : private OperationBase {
46744669
public:
46754670
LoadExtensionOperation(Global<Promise::Resolver>&& resolver,
4676-
std::pmr::string&& extension_path)
4671+
std::string&& extension_path)
46774672
: OperationBase(std::move(resolver)),
46784673
extension_path_(std::move(extension_path)) {}
46794674

@@ -4689,7 +4684,7 @@ class LoadExtensionOperation : private OperationBase {
46894684
}
46904685

46914686
private:
4692-
std::pmr::string extension_path_;
4687+
std::string extension_path_;
46934688
};
46944689

46954690
using Operation = std::variant<ExecOperation,
@@ -4724,9 +4719,7 @@ enum class QueuePushResult {
47244719

47254720
class DatabaseOperationQueue {
47264721
public:
4727-
explicit DatabaseOperationQueue(size_t capacity,
4728-
std::pmr::memory_resource* memory_resource)
4729-
: operations_(memory_resource), results_(memory_resource) {
4722+
explicit DatabaseOperationQueue(size_t capacity) : operations_(), results_() {
47304723
operations_.reserve(capacity);
47314724
results_.reserve(capacity);
47324725
}
@@ -4774,8 +4767,8 @@ class DatabaseOperationQueue {
47744767
}
47754768

47764769
private:
4777-
std::pmr::vector<Operation> operations_;
4778-
std::pmr::vector<OperationResult> results_;
4770+
std::vector<Operation> operations_;
4771+
std::vector<OperationResult> results_;
47794772
size_t pending_index_ = 0;
47804773
size_t completed_index_ = 0;
47814774
Mutex results_mutex_;
@@ -4900,8 +4893,7 @@ class DatabaseOperationExecutor final : private ThreadPoolWork {
49004893

49014894
void Database::PrepareNextBatch() {
49024895
CHECK_NULL(next_batch_);
4903-
next_batch_ = std::make_unique<DatabaseOperationQueue>(
4904-
kDefaultBatchSize, std::pmr::get_default_resource());
4896+
next_batch_ = std::make_unique<DatabaseOperationQueue>(kDefaultBatchSize);
49054897

49064898
// TODO(BurningEnlightenment): Do I need to retain a BaseObjectPtr?
49074899
env()->isolate()->EnqueueMicrotask(
@@ -5097,8 +5089,7 @@ Local<Promise> Database::AsyncDisposeImpl() {
50975089
// because e.g. PrepareStatementOperations need to connect their results
50985090
// first.
50995091
ProcessNextBatch();
5100-
next_batch_ = std::make_unique<DatabaseOperationQueue>(
5101-
1U, std::pmr::get_default_resource());
5092+
next_batch_ = std::make_unique<DatabaseOperationQueue>(1U);
51025093
CHECK_NE(next_batch_->PushEmplace<CloseOperation>(isolate, resolver),
51035094
QueuePushResult::kQueueFull);
51045095
ProcessNextBatch();
@@ -5212,9 +5203,7 @@ void Database::Prepare(const v8::FunctionCallbackInfo<v8::Value>& args) {
52125203
}
52135204

52145205
args.GetReturnValue().Set(db->Schedule<PrepareStatementOperation>(
5215-
BaseObjectPtr<Database>(db),
5216-
std::pmr::string(*sql, sql.length()),
5217-
options));
5206+
BaseObjectPtr<Database>(db), std::string(*sql, sql.length()), options));
52185207
}
52195208

52205209
void Database::TrackStatement(Statement* statement) {
@@ -5238,7 +5227,7 @@ void Database::Exec(const v8::FunctionCallbackInfo<v8::Value>& args) {
52385227

52395228
Utf8Value sql(env->isolate(), args[0].As<String>());
52405229
args.GetReturnValue().Set(
5241-
db->Schedule<ExecOperation>(std::pmr::string(*sql, sql.length())));
5230+
db->Schedule<ExecOperation>(std::string(*sql, sql.length())));
52425231
}
52435232

52445233
void Database::IsInTransaction(
@@ -5277,15 +5266,15 @@ void Database::Location(const v8::FunctionCallbackInfo<v8::Value>& args) {
52775266
REJECT_AND_RETURN_ON_INVALID_STATE(
52785267
env, args, !db->IsOpen(), "database is not open");
52795268

5280-
std::pmr::string db_name;
5269+
std::string db_name;
52815270
if (args.Length() > 0) {
52825271
REJECT_AND_RETURN_ON_INVALID_ARG_TYPE(
52835272
env,
52845273
args,
52855274
!args[0]->IsString(),
52865275
"The \"dbName\" argument must be a string.");
52875276
Utf8Value db_name_utf8(env->isolate(), args[0].As<String>());
5288-
db_name = std::pmr::string(*db_name_utf8, db_name_utf8.length());
5277+
db_name = std::string(*db_name_utf8, db_name_utf8.length());
52895278
} else {
52905279
db_name = "main";
52915280
}
@@ -5359,8 +5348,8 @@ void Database::LoadExtension(const v8::FunctionCallbackInfo<v8::Value>& args) {
53595348
BufferValue path(env->isolate(), args[0]);
53605349
ToNamespacedPath(env, &path);
53615350

5362-
args.GetReturnValue().Set(db->Schedule<LoadExtensionOperation>(
5363-
std::pmr::string(path.ToStringView())));
5351+
args.GetReturnValue().Set(
5352+
db->Schedule<LoadExtensionOperation>(std::string(path.ToStringView())));
53645353
}
53655354

53665355
Statement::~Statement() {

0 commit comments

Comments
 (0)