Skip to content

Commit 7152fe3

Browse files
lundibunditargos
authored andcommittedApr 20, 2020
src: improve KVStore API
This adds `const char*` based APIs to KVStore to avoid multiple string conversions (char -> Utf8 -> Local -> char etc.) when possible. PR-URL: #31773 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f9123eb commit 7152fe3

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed
 

‎src/env.h

+2
Original file line numberDiff line numberDiff line change
@@ -596,11 +596,13 @@ class KVStore {
596596

597597
virtual v8::MaybeLocal<v8::String> Get(v8::Isolate* isolate,
598598
v8::Local<v8::String> key) const = 0;
599+
virtual v8::Maybe<std::string> Get(const char* key) const = 0;
599600
virtual void Set(v8::Isolate* isolate,
600601
v8::Local<v8::String> key,
601602
v8::Local<v8::String> value) = 0;
602603
virtual int32_t Query(v8::Isolate* isolate,
603604
v8::Local<v8::String> key) const = 0;
605+
virtual int32_t Query(const char* key) const = 0;
604606
virtual void Delete(v8::Isolate* isolate, v8::Local<v8::String> key) = 0;
605607
virtual v8::Local<v8::Array> Enumerate(v8::Isolate* isolate) const = 0;
606608

‎src/node_env_var.cc

+46-20
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,21 @@ using v8::Value;
2929
class RealEnvStore final : public KVStore {
3030
public:
3131
MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override;
32+
Maybe<std::string> Get(const char* key) const override;
3233
void Set(Isolate* isolate, Local<String> key, Local<String> value) override;
3334
int32_t Query(Isolate* isolate, Local<String> key) const override;
35+
int32_t Query(const char* key) const override;
3436
void Delete(Isolate* isolate, Local<String> key) override;
3537
Local<Array> Enumerate(Isolate* isolate) const override;
3638
};
3739

3840
class MapKVStore final : public KVStore {
3941
public:
4042
MaybeLocal<String> Get(Isolate* isolate, Local<String> key) const override;
43+
Maybe<std::string> Get(const char* key) const override;
4144
void Set(Isolate* isolate, Local<String> key, Local<String> value) override;
4245
int32_t Query(Isolate* isolate, Local<String> key) const override;
46+
int32_t Query(const char* key) const override;
4347
void Delete(Isolate* isolate, Local<String> key) override;
4448
Local<Array> Enumerate(Isolate* isolate) const override;
4549

@@ -58,26 +62,36 @@ Mutex env_var_mutex;
5862
std::shared_ptr<KVStore> system_environment = std::make_shared<RealEnvStore>();
5963
} // namespace per_process
6064

61-
MaybeLocal<String> RealEnvStore::Get(Isolate* isolate,
62-
Local<String> property) const {
65+
Maybe<std::string> RealEnvStore::Get(const char* key) const {
6366
Mutex::ScopedLock lock(per_process::env_var_mutex);
6467

65-
node::Utf8Value key(isolate, property);
6668
size_t init_sz = 256;
6769
MaybeStackBuffer<char, 256> val;
68-
int ret = uv_os_getenv(*key, *val, &init_sz);
70+
int ret = uv_os_getenv(key, *val, &init_sz);
6971

7072
if (ret == UV_ENOBUFS) {
7173
// Buffer is not large enough, reallocate to the updated init_sz
7274
// and fetch env value again.
7375
val.AllocateSufficientStorage(init_sz);
74-
ret = uv_os_getenv(*key, *val, &init_sz);
76+
ret = uv_os_getenv(key, *val, &init_sz);
7577
}
7678

7779
if (ret >= 0) { // Env key value fetch success.
78-
MaybeLocal<String> value_string =
79-
String::NewFromUtf8(isolate, *val, NewStringType::kNormal, init_sz);
80-
return value_string;
80+
return v8::Just(std::string(*val, init_sz));
81+
}
82+
83+
return v8::Nothing<std::string>();
84+
}
85+
86+
MaybeLocal<String> RealEnvStore::Get(Isolate* isolate,
87+
Local<String> property) const {
88+
node::Utf8Value key(isolate, property);
89+
Maybe<std::string> value = Get(*key);
90+
91+
if (value.IsJust()) {
92+
std::string val = value.FromJust();
93+
return String::NewFromUtf8(
94+
isolate, val.data(), NewStringType::kNormal, val.size());
8195
}
8296

8397
return MaybeLocal<String>();
@@ -97,14 +111,12 @@ void RealEnvStore::Set(Isolate* isolate,
97111
uv_os_setenv(*key, *val);
98112
}
99113

100-
int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
114+
int32_t RealEnvStore::Query(const char* key) const {
101115
Mutex::ScopedLock lock(per_process::env_var_mutex);
102116

103-
node::Utf8Value key(isolate, property);
104-
105117
char val[2];
106118
size_t init_sz = sizeof(val);
107-
int ret = uv_os_getenv(*key, val, &init_sz);
119+
int ret = uv_os_getenv(key, val, &init_sz);
108120

109121
if (ret == UV_ENOENT) {
110122
return -1;
@@ -121,6 +133,11 @@ int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
121133
return 0;
122134
}
123135

136+
int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
137+
node::Utf8Value key(isolate, property);
138+
return Query(*key);
139+
}
140+
124141
void RealEnvStore::Delete(Isolate* isolate, Local<String> property) {
125142
Mutex::ScopedLock lock(per_process::env_var_mutex);
126143

@@ -174,13 +191,19 @@ std::shared_ptr<KVStore> KVStore::Clone(v8::Isolate* isolate) const {
174191
return copy;
175192
}
176193

177-
MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const {
194+
Maybe<std::string> MapKVStore::Get(const char* key) const {
178195
Mutex::ScopedLock lock(mutex_);
196+
auto it = map_.find(key);
197+
return it == map_.end() ? v8::Nothing<std::string>() : v8::Just(it->second);
198+
}
199+
200+
MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const {
179201
Utf8Value str(isolate, key);
180-
auto it = map_.find(std::string(*str, str.length()));
181-
if (it == map_.end()) return Local<String>();
182-
return String::NewFromUtf8(isolate, it->second.data(),
183-
NewStringType::kNormal, it->second.size());
202+
Maybe<std::string> value = Get(*str);
203+
if (value.IsNothing()) return Local<String>();
204+
std::string val = value.FromJust();
205+
return String::NewFromUtf8(
206+
isolate, val.data(), NewStringType::kNormal, val.size());
184207
}
185208

186209
void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) {
@@ -193,11 +216,14 @@ void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) {
193216
}
194217
}
195218

196-
int32_t MapKVStore::Query(Isolate* isolate, Local<String> key) const {
219+
int32_t MapKVStore::Query(const char* key) const {
197220
Mutex::ScopedLock lock(mutex_);
221+
return map_.find(key) == map_.end() ? -1 : 0;
222+
}
223+
224+
int32_t MapKVStore::Query(Isolate* isolate, Local<String> key) const {
198225
Utf8Value str(isolate, key);
199-
auto it = map_.find(std::string(*str, str.length()));
200-
return it == map_.end() ? -1 : 0;
226+
return Query(*str);
201227
}
202228

203229
void MapKVStore::Delete(Isolate* isolate, Local<String> key) {

0 commit comments

Comments
 (0)
Please sign in to comment.