Skip to content

Commit 9c381d0

Browse files
araujoguiaduh95
authored andcommitted
sqlite: cache the SQLTagStore constructor template
Store the template on the environment like the other sqlite classes, so stores no longer get a fresh template and prototype each time. Signed-off-by: Guilherme Araújo <arauujogui@gmail.com> PR-URL: #66912 Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent c38a503 commit 9c381d0

3 files changed

Lines changed: 28 additions & 18 deletions

File tree

‎src/env_properties.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@
457457
V(sqlite_statement_sync_constructor_template, v8::FunctionTemplate) \
458458
V(sqlite_statement_sync_iterator_constructor_template, v8::FunctionTemplate) \
459459
V(sqlite_session_constructor_template, v8::FunctionTemplate) \
460+
V(sqlite_tag_store_constructor_template, v8::FunctionTemplate) \
460461
V(srv_record_template, v8::DictionaryTemplate) \
461462
V(streambaseoutputstream_constructor_template, v8::ObjectTemplate) \
462463
V(tcp_constructor_template, v8::FunctionTemplate) \

‎src/node_sqlite.cc‎

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4353,24 +4353,27 @@ static inline void SetSideEffectFreeGetter(
43534353
SQLTagStore::~SQLTagStore() {}
43544354

43554355
Local<FunctionTemplate> SQLTagStore::GetConstructorTemplate(Environment* env) {
4356-
Isolate* isolate = env->isolate();
4357-
Local<FunctionTemplate> tmpl =
4358-
NewFunctionTemplate(isolate, IllegalConstructor);
4359-
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "SQLTagStore"));
4360-
tmpl->InstanceTemplate()->SetInternalFieldCount(
4361-
SQLTagStore::kInternalFieldCount);
4362-
SetProtoMethod(isolate, tmpl, "get", Get);
4363-
SetProtoMethod(isolate, tmpl, "all", All);
4364-
SetProtoMethod(isolate, tmpl, "iterate", Iterate);
4365-
SetProtoMethod(isolate, tmpl, "run", Run);
4366-
SetProtoMethod(isolate, tmpl, "clear", Clear);
4367-
SetSideEffectFreeGetter(isolate,
4368-
tmpl,
4369-
FIXED_ONE_BYTE_STRING(isolate, "capacity"),
4370-
CapacityGetter);
4371-
SetSideEffectFreeGetter(
4372-
isolate, tmpl, FIXED_ONE_BYTE_STRING(isolate, "db"), DatabaseGetter);
4373-
SetSideEffectFreeGetter(isolate, tmpl, env->size_string(), SizeGetter);
4356+
Local<FunctionTemplate> tmpl = env->sqlite_tag_store_constructor_template();
4357+
if (tmpl.IsEmpty()) {
4358+
Isolate* isolate = env->isolate();
4359+
tmpl = NewFunctionTemplate(isolate, IllegalConstructor);
4360+
tmpl->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "SQLTagStore"));
4361+
tmpl->InstanceTemplate()->SetInternalFieldCount(
4362+
SQLTagStore::kInternalFieldCount);
4363+
SetProtoMethod(isolate, tmpl, "get", Get);
4364+
SetProtoMethod(isolate, tmpl, "all", All);
4365+
SetProtoMethod(isolate, tmpl, "iterate", Iterate);
4366+
SetProtoMethod(isolate, tmpl, "run", Run);
4367+
SetProtoMethod(isolate, tmpl, "clear", Clear);
4368+
SetSideEffectFreeGetter(isolate,
4369+
tmpl,
4370+
FIXED_ONE_BYTE_STRING(isolate, "capacity"),
4371+
CapacityGetter);
4372+
SetSideEffectFreeGetter(
4373+
isolate, tmpl, FIXED_ONE_BYTE_STRING(isolate, "db"), DatabaseGetter);
4374+
SetSideEffectFreeGetter(isolate, tmpl, env->size_string(), SizeGetter);
4375+
env->set_sqlite_tag_store_constructor_template(tmpl);
4376+
}
43744377
return tmpl;
43754378
}
43764379

‎test/parallel/test-sqlite-template-tag.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,12 @@ test('sql.db returns the associated Database instance', () => {
295295
assert.strictEqual(sql.db, db);
296296
});
297297

298+
test('tag stores share a prototype', () => {
299+
const other = new Database(':memory:').createTagStore();
300+
assert.strictEqual(Object.getPrototypeOf(other), Object.getPrototypeOf(sql));
301+
assert.strictEqual(other.constructor, sql.constructor);
302+
});
303+
298304
test('sql error messages are descriptive', () => {
299305
assert.strictEqual(sql.run`INSERT INTO foo (text) VALUES (${'test'})`.changes, 1);
300306

0 commit comments

Comments
 (0)