Skip to content

Commit 296f35b

Browse files
lundibundiMylesBorins
authored andcommitted
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 <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent bd75688 commit 296f35b

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
@@ -608,11 +608,13 @@ class KVStore {
608608

609609
virtual v8::MaybeLocal<v8::String> Get(v8::Isolate* isolate,
610610
v8::Local<v8::String> key) const = 0;
611+
virtual v8::Maybe<std::string> Get(const char* key) const = 0;
611612
virtual void Set(v8::Isolate* isolate,
612613
v8::Local<v8::String> key,
613614
v8::Local<v8::String> value) = 0;
614615
virtual int32_t Query(v8::Isolate* isolate,
615616
v8::Local<v8::String> key) const = 0;
617+
virtual int32_t Query(const char* key) const = 0;
616618
virtual void Delete(v8::Isolate* isolate, v8::Local<v8::String> key) = 0;
617619
virtual v8::Local<v8::Array> Enumerate(v8::Isolate* isolate) const = 0;
618620

src/node_env_var.cc

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

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

@@ -72,26 +76,36 @@ void DateTimeConfigurationChangeNotification(Isolate* isolate, const T& key) {
7276
}
7377
}
7478

75-
MaybeLocal<String> RealEnvStore::Get(Isolate* isolate,
76-
Local<String> property) const {
79+
Maybe<std::string> RealEnvStore::Get(const char* key) const {
7780
Mutex::ScopedLock lock(per_process::env_var_mutex);
7881

79-
node::Utf8Value key(isolate, property);
8082
size_t init_sz = 256;
8183
MaybeStackBuffer<char, 256> val;
82-
int ret = uv_os_getenv(*key, *val, &init_sz);
84+
int ret = uv_os_getenv(key, *val, &init_sz);
8385

8486
if (ret == UV_ENOBUFS) {
8587
// Buffer is not large enough, reallocate to the updated init_sz
8688
// and fetch env value again.
8789
val.AllocateSufficientStorage(init_sz);
88-
ret = uv_os_getenv(*key, *val, &init_sz);
90+
ret = uv_os_getenv(key, *val, &init_sz);
8991
}
9092

9193
if (ret >= 0) { // Env key value fetch success.
92-
MaybeLocal<String> value_string =
93-
String::NewFromUtf8(isolate, *val, NewStringType::kNormal, init_sz);
94-
return value_string;
94+
return v8::Just(std::string(*val, init_sz));
95+
}
96+
97+
return v8::Nothing<std::string>();
98+
}
99+
100+
MaybeLocal<String> RealEnvStore::Get(Isolate* isolate,
101+
Local<String> property) const {
102+
node::Utf8Value key(isolate, property);
103+
Maybe<std::string> value = Get(*key);
104+
105+
if (value.IsJust()) {
106+
std::string val = value.FromJust();
107+
return String::NewFromUtf8(
108+
isolate, val.data(), NewStringType::kNormal, val.size());
95109
}
96110

97111
return MaybeLocal<String>();
@@ -112,14 +126,12 @@ void RealEnvStore::Set(Isolate* isolate,
112126
DateTimeConfigurationChangeNotification(isolate, key);
113127
}
114128

115-
int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
129+
int32_t RealEnvStore::Query(const char* key) const {
116130
Mutex::ScopedLock lock(per_process::env_var_mutex);
117131

118-
node::Utf8Value key(isolate, property);
119-
120132
char val[2];
121133
size_t init_sz = sizeof(val);
122-
int ret = uv_os_getenv(*key, val, &init_sz);
134+
int ret = uv_os_getenv(key, val, &init_sz);
123135

124136
if (ret == UV_ENOENT) {
125137
return -1;
@@ -136,6 +148,11 @@ int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
136148
return 0;
137149
}
138150

151+
int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
152+
node::Utf8Value key(isolate, property);
153+
return Query(*key);
154+
}
155+
139156
void RealEnvStore::Delete(Isolate* isolate, Local<String> property) {
140157
Mutex::ScopedLock lock(per_process::env_var_mutex);
141158

@@ -190,13 +207,19 @@ std::shared_ptr<KVStore> KVStore::Clone(v8::Isolate* isolate) const {
190207
return copy;
191208
}
192209

193-
MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const {
210+
Maybe<std::string> MapKVStore::Get(const char* key) const {
194211
Mutex::ScopedLock lock(mutex_);
212+
auto it = map_.find(key);
213+
return it == map_.end() ? v8::Nothing<std::string>() : v8::Just(it->second);
214+
}
215+
216+
MaybeLocal<String> MapKVStore::Get(Isolate* isolate, Local<String> key) const {
195217
Utf8Value str(isolate, key);
196-
auto it = map_.find(std::string(*str, str.length()));
197-
if (it == map_.end()) return Local<String>();
198-
return String::NewFromUtf8(isolate, it->second.data(),
199-
NewStringType::kNormal, it->second.size());
218+
Maybe<std::string> value = Get(*str);
219+
if (value.IsNothing()) return Local<String>();
220+
std::string val = value.FromJust();
221+
return String::NewFromUtf8(
222+
isolate, val.data(), NewStringType::kNormal, val.size());
200223
}
201224

202225
void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) {
@@ -209,11 +232,14 @@ void MapKVStore::Set(Isolate* isolate, Local<String> key, Local<String> value) {
209232
}
210233
}
211234

212-
int32_t MapKVStore::Query(Isolate* isolate, Local<String> key) const {
235+
int32_t MapKVStore::Query(const char* key) const {
213236
Mutex::ScopedLock lock(mutex_);
237+
return map_.find(key) == map_.end() ? -1 : 0;
238+
}
239+
240+
int32_t MapKVStore::Query(Isolate* isolate, Local<String> key) const {
214241
Utf8Value str(isolate, key);
215-
auto it = map_.find(std::string(*str, str.length()));
216-
return it == map_.end() ? -1 : 0;
242+
return Query(*str);
217243
}
218244

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

0 commit comments

Comments
 (0)