summaryrefslogtreecommitdiff
path: root/serde_v8/keys.rs
diff options
context:
space:
mode:
authorAaron O'Mullan <aaron.omullan@gmail.com>2022-03-24 11:23:40 +0100
committerGitHub <noreply@github.com>2022-03-24 11:23:40 +0100
commit6516130b0138ef382a0588f983287fb463222086 (patch)
treef250150295fb4d7a023c1fa7d92da621ac804340 /serde_v8/keys.rs
parentc52d72e8e115f30be32cbb2a91a4c3aa117c328c (diff)
chore: drop src/ in bench_util & serde_v8 (#14097)
To align with conventions used in our other crates
Diffstat (limited to 'serde_v8/keys.rs')
-rw-r--r--serde_v8/keys.rs31
1 files changed, 31 insertions, 0 deletions
diff --git a/serde_v8/keys.rs b/serde_v8/keys.rs
new file mode 100644
index 000000000..4811a87f8
--- /dev/null
+++ b/serde_v8/keys.rs
@@ -0,0 +1,31 @@
+// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+use std::collections::HashMap;
+
+// KeyCache stores a pool struct keys mapped to v8,
+// to minimize allocs and speed up decoding/encoding `v8::Object`s
+// TODO: experiment with in from_v8/to_v8
+pub struct KeyCache(HashMap<&'static str, v8::Global<v8::String>>);
+
+// creates an optimized v8::String for a struct field
+// TODO: experiment with external strings
+// TODO: evaluate if own KeyCache is better than v8's dedupe
+pub fn v8_struct_key<'s>(
+ scope: &mut v8::HandleScope<'s>,
+ field: &'static str,
+) -> v8::Local<'s, v8::String> {
+ // Internalized v8 strings are significantly faster than "normal" v8 strings
+ // since v8 deduplicates re-used strings minimizing new allocations
+ // see: https://github.com/v8/v8/blob/14ac92e02cc3db38131a57e75e2392529f405f2f/include/v8.h#L3165-L3171
+ v8::String::new_from_utf8(
+ scope,
+ field.as_ref(),
+ v8::NewStringType::Internalized,
+ )
+ .unwrap()
+
+ // TODO: consider external strings later
+ // right now non-deduped external strings (without KeyCache)
+ // are slower than the deduped internalized strings by ~2.5x
+ // since they're a new string in v8's eyes and needs to be hashed, etc...
+ // v8::String::new_external_onebyte_static(scope, field).unwrap()
+}