From b0ea6e0dc77542b6f10e856ae3c10a8cb13d7351 Mon Sep 17 00:00:00 2001 From: Yusuke Tanaka Date: Sat, 24 Aug 2024 09:41:49 +0900 Subject: [PATCH] chore(ext/node): use array instead of Vec to avoid wrong capacity allocation (#25183) This commit gets deno_node's customizer to use fixed-length array instead of `Vec` to avoid wrong capacity allocation. In the previous code we reserve a capacity of 14 for `external_references`. However, after pushing all the necessary `ExternalReference`s, it ends up with a length of 21, not 14. This means another allocation happens even though we reserve some space. To make sure that there will no longer be extra allocation, it should be a good idea to use fixed-length array here. --- ext/node/lib.rs | 213 ++++++++++++++++++++++++------------------------ 1 file changed, 107 insertions(+), 106 deletions(-) diff --git a/ext/node/lib.rs b/ext/node/lib.rs index ed6713eed8..f2c1576cf5 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -633,115 +633,116 @@ deno_core::extension!(deno_node, global_template_middleware = global_template_middleware, global_object_middleware = global_object_middleware, customizer = |ext: &mut deno_core::Extension| { - let mut external_references = Vec::with_capacity(14); + let external_references = [ + vm::QUERY_MAP_FN.with(|query| { + ExternalReference { + named_query: *query, + } + }), + vm::GETTER_MAP_FN.with(|getter| { + ExternalReference { + named_getter: *getter, + } + }), + vm::SETTER_MAP_FN.with(|setter| { + ExternalReference { + named_setter: *setter, + } + }), + vm::DESCRIPTOR_MAP_FN.with(|descriptor| { + ExternalReference { + named_getter: *descriptor, + } + }), + vm::DELETER_MAP_FN.with(|deleter| { + ExternalReference { + named_deleter: *deleter, + } + }), + vm::ENUMERATOR_MAP_FN.with(|enumerator| { + ExternalReference { + enumerator: *enumerator, + } + }), + vm::DEFINER_MAP_FN.with(|definer| { + ExternalReference { + named_definer: *definer, + } + }), - vm::QUERY_MAP_FN.with(|query| { - external_references.push(ExternalReference { - named_query: *query, - }); - }); - vm::GETTER_MAP_FN.with(|getter| { - external_references.push(ExternalReference { - named_getter: *getter, - }); - }); - vm::SETTER_MAP_FN.with(|setter| { - external_references.push(ExternalReference { - named_setter: *setter, - }); - }); - vm::DESCRIPTOR_MAP_FN.with(|descriptor| { - external_references.push(ExternalReference { - named_getter: *descriptor, - }); - }); - vm::DELETER_MAP_FN.with(|deleter| { - external_references.push(ExternalReference { - named_deleter: *deleter, - }); - }); - vm::ENUMERATOR_MAP_FN.with(|enumerator| { - external_references.push(ExternalReference { - enumerator: *enumerator, - }); - }); - vm::DEFINER_MAP_FN.with(|definer| { - external_references.push(ExternalReference { - named_definer: *definer, - }); - }); + vm::INDEXED_QUERY_MAP_FN.with(|query| { + ExternalReference { + indexed_query: *query, + } + }), + vm::INDEXED_GETTER_MAP_FN.with(|getter| { + ExternalReference { + indexed_getter: *getter, + } + }), + vm::INDEXED_SETTER_MAP_FN.with(|setter| { + ExternalReference { + indexed_setter: *setter, + } + }), + vm::INDEXED_DESCRIPTOR_MAP_FN.with(|descriptor| { + ExternalReference { + indexed_getter: *descriptor, + } + }), + vm::INDEXED_DELETER_MAP_FN.with(|deleter| { + ExternalReference { + indexed_deleter: *deleter, + } + }), + vm::INDEXED_DEFINER_MAP_FN.with(|definer| { + ExternalReference { + indexed_definer: *definer, + } + }), + vm::INDEXED_ENUMERATOR_MAP_FN.with(|enumerator| { + ExternalReference { + enumerator: *enumerator, + } + }), - vm::INDEXED_QUERY_MAP_FN.with(|query| { - external_references.push(ExternalReference { - indexed_query: *query, - }); - }); - vm::INDEXED_GETTER_MAP_FN.with(|getter| { - external_references.push(ExternalReference { - indexed_getter: *getter, - }); - }); - vm::INDEXED_SETTER_MAP_FN.with(|setter| { - external_references.push(ExternalReference { - indexed_setter: *setter, - }); - }); - vm::INDEXED_DESCRIPTOR_MAP_FN.with(|descriptor| { - external_references.push(ExternalReference { - indexed_getter: *descriptor, - }); - }); - vm::INDEXED_DELETER_MAP_FN.with(|deleter| { - external_references.push(ExternalReference { - indexed_deleter: *deleter, - }); - }); - vm::INDEXED_DEFINER_MAP_FN.with(|definer| { - external_references.push(ExternalReference { - indexed_definer: *definer, - }); - }); - vm::INDEXED_ENUMERATOR_MAP_FN.with(|enumerator| { - external_references.push(ExternalReference { - enumerator: *enumerator, - }); - }); + global::GETTER_MAP_FN.with(|getter| { + ExternalReference { + named_getter: *getter, + } + }), + global::SETTER_MAP_FN.with(|setter| { + ExternalReference { + named_setter: *setter, + } + }), + global::QUERY_MAP_FN.with(|query| { + ExternalReference { + named_query: *query, + } + }), + global::DELETER_MAP_FN.with(|deleter| { + ExternalReference { + named_deleter: *deleter, + } + }), + global::ENUMERATOR_MAP_FN.with(|enumerator| { + ExternalReference { + enumerator: *enumerator, + } + }), + global::DEFINER_MAP_FN.with(|definer| { + ExternalReference { + named_definer: *definer, + } + }), + global::DESCRIPTOR_MAP_FN.with(|descriptor| { + ExternalReference { + named_getter: *descriptor, + } + }), + ]; - global::GETTER_MAP_FN.with(|getter| { - external_references.push(ExternalReference { - named_getter: *getter, - }); - }); - global::SETTER_MAP_FN.with(|setter| { - external_references.push(ExternalReference { - named_setter: *setter, - }); - }); - global::QUERY_MAP_FN.with(|query| { - external_references.push(ExternalReference { - named_query: *query, - }); - }); - global::DELETER_MAP_FN.with(|deleter| { - external_references.push(ExternalReference { - named_deleter: *deleter, - }); - }); - global::ENUMERATOR_MAP_FN.with(|enumerator| { - external_references.push(ExternalReference { - enumerator: *enumerator, - }); - }); - global::DEFINER_MAP_FN.with(|definer| { - external_references.push(ExternalReference { - named_definer: *definer, - }); - }); - global::DESCRIPTOR_MAP_FN.with(|descriptor| { - external_references.push(ExternalReference { - named_getter: *descriptor, - }); - }); ext.external_references.to_mut().extend(external_references); }, );