提交 d534ea41 编写于 作者: R Ryan Dahl

Optimization: Use std::map for tracking zero-copy buffers

Instead of V8 map.
上级 3a226f16
......@@ -20,34 +20,23 @@ Deno* FromIsolate(v8::Isolate* isolate) {
return static_cast<Deno*>(isolate->GetData(0));
}
void LazilyCreateDataMap(Deno* d) {
if (d->async_data_map.IsEmpty()) {
v8::HandleScope handle_scope(d->isolate);
// It's important for security reasons that async_data_map is not exposed to
// the VM.
auto async_data_map = v8::Map::New(d->isolate);
d->async_data_map.Reset(d->isolate, async_data_map);
}
DCHECK(!d->async_data_map.IsEmpty());
}
void AddDataRef(Deno* d, int32_t req_id, v8::Local<v8::Value> data_v) {
LazilyCreateDataMap(d);
auto async_data_map = d->async_data_map.Get(d->isolate);
auto context = d->context.Get(d->isolate);
auto req_id_v = v8::Integer::New(d->isolate, req_id);
auto r = async_data_map->Set(context, req_id_v, data_v);
CHECK(!r.IsEmpty());
// TODO Use std::unique_ptr
auto pair =
std::make_pair(req_id, new v8::Persistent<v8::Value>(d->isolate, data_v));
d->async_data_map.insert(pair);
}
void DeleteDataRef(Deno* d, int32_t req_id) {
LazilyCreateDataMap(d);
auto context = d->context.Get(d->isolate);
// Delete persistent reference to data ArrayBuffer.
auto async_data_map = d->async_data_map.Get(d->isolate);
auto req_id_v = v8::Integer::New(d->isolate, req_id);
auto maybe_deleted = async_data_map->Delete(context, req_id_v);
CHECK(maybe_deleted.IsJust());
auto it = d->async_data_map.find(req_id);
if (it != d->async_data_map.end()) {
auto pair = *it;
auto p = pair.second;
p->Reset();
delete p;
d->async_data_map.erase(it);
}
}
// Extracts a C string from a v8::V8 Utf8Value.
......
......@@ -2,6 +2,7 @@
#ifndef INTERNAL_H_
#define INTERNAL_H_
#include <map>
#include <string>
#include "deno.h"
#include "third_party/v8/include/v8.h"
......@@ -22,7 +23,7 @@ struct deno_s {
int32_t pending_promise_events;
v8::Persistent<v8::Context> context;
v8::Persistent<v8::Map> async_data_map;
std::map<int32_t, v8::Persistent<v8::Value>*> async_data_map;
deno_recv_cb cb;
int32_t next_req_id;
void* user_data;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册