From 2e55172dbee84e7880b4b49394e74964e4f05346 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sat, 10 Oct 2026 05:16:53 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Pre-allocate=20collections?= =?UTF-8?q?=20in=20VM=20executor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Tcode-Motion <188012755+Tcode-Motion@users.noreply.github.com> --- runtime/vm/src/executor.rs | 22 +++++++-- stdlib/src/net.rs | 2 +- stdlib/src/net_handler.rs | 15 +++---- stdlib/src/strings.rs | 87 ++++++++++++++++++++++++++++-------- stdlib/src/web.rs | 66 ++++++++++++++++++--------- stdlib/tests/stdlib_tests.rs | 9 ++-- 6 files changed, 145 insertions(+), 56 deletions(-) diff --git a/runtime/vm/src/executor.rs b/runtime/vm/src/executor.rs index a7543495..ace9bb00 100644 --- a/runtime/vm/src/executor.rs +++ b/runtime/vm/src/executor.rs @@ -488,7 +488,12 @@ impl VM { Opcode::Call => { if let Some(Operand::Count(arg_count)) = inst_operands.first() { - let mut args = Vec::new(); + // ⚡ Bolt Performance Optimization: + // Pre-allocate the argument vector capacity up to the available stack size. + // This avoids dynamic reallocation overhead in the hot VM loop while + // preventing potential OOM panics from malformed bytecode with huge argument counts. + let mut args = + Vec::with_capacity((*arg_count as usize).min(self.stack.len())); for _ in 0..*arg_count { args.push(self.stack.pop()?); } @@ -693,7 +698,11 @@ impl VM { Opcode::MakeList => { if let Some(Operand::Count(n)) = inst_operands.first() { - let mut items = Vec::new(); + // ⚡ Bolt Performance Optimization: + // Pre-allocate the list capacity up to the available stack size. + // This avoids dynamic reallocation overhead in the hot VM loop while + // preventing potential OOM panics from malformed bytecode with huge item counts. + let mut items = Vec::with_capacity((*n as usize).min(self.stack.len())); for _ in 0..*n { items.push(self.stack.pop()?); } @@ -709,7 +718,14 @@ impl VM { Opcode::MakeMap => { if let Some(Operand::Count(n)) = inst_operands.first() { - let mut entries = indexmap::IndexMap::new(); + // ⚡ Bolt Performance Optimization: + // Pre-allocate the map capacity up to half the available stack size + // (since each entry consumes a key and a value on the stack). + // This avoids dynamic reallocation overhead in the hot VM loop while + // preventing potential OOM panics from malformed bytecode with huge item counts. + let mut entries = indexmap::IndexMap::with_capacity( + (*n as usize).min(self.stack.len() / 2), + ); for _ in 0..*n { let val = self.stack.pop()?; let key_val = self.stack.pop()?; diff --git a/stdlib/src/net.rs b/stdlib/src/net.rs index 2a57f8b1..c36c22c4 100644 --- a/stdlib/src/net.rs +++ b/stdlib/src/net.rs @@ -1,8 +1,8 @@ +use crate::net_handler::{tcp_connect_fn, tcp_listen_fn, tcp_recv_fn, tcp_send_fn}; use crate::{StdFunction, StdlibModule, StdlibRegistry}; use std::collections::HashMap; use std::rc::Rc; use techscript_runtime::context::Capability; -use crate::net_handler::{tcp_listen_fn, tcp_connect_fn, tcp_send_fn, tcp_recv_fn}; impl StdlibRegistry { pub fn register_net(&mut self) { diff --git a/stdlib/src/net_handler.rs b/stdlib/src/net_handler.rs index 965143b0..4c3e45d2 100644 --- a/stdlib/src/net_handler.rs +++ b/stdlib/src/net_handler.rs @@ -35,14 +35,13 @@ pub fn tcp_connect_fn( ) -> Result { let ip = args[0].try_into_string()?; let port = args[1].try_into_int()?; - let stream = - std::net::TcpStream::connect(format!("{}:{}", ip, port)).map_err(|e| { - RuntimeError::new( - RuntimeErrorKind::InvalidOperation(format!("TCP connect error: {}", e)), - None, - None, - ) - })?; + let stream = std::net::TcpStream::connect(format!("{}:{}", ip, port)).map_err(|e| { + RuntimeError::new( + RuntimeErrorKind::InvalidOperation(format!("TCP connect error: {}", e)), + None, + None, + ) + })?; let handle_id = ctx.resources.borrow_mut().insert(stream); let mut stream_map = IndexMap::new(); stream_map.insert("ip".to_string(), RuntimeValue::Str(ip)); diff --git a/stdlib/src/strings.rs b/stdlib/src/strings.rs index 970e61e7..94ee8fce 100644 --- a/stdlib/src/strings.rs +++ b/stdlib/src/strings.rs @@ -1,15 +1,18 @@ use crate::{StdFunction, StdlibModule, StdlibRegistry}; use std::collections::HashMap; use std::rc::Rc; -use techscript_runtime::{error::RuntimeError, error::RuntimeErrorKind, value::RuntimeValue}; use techscript_runtime::RuntimeContext; +use techscript_runtime::{error::RuntimeError, error::RuntimeErrorKind, value::RuntimeValue}; fn trim(_ctx: &mut RuntimeContext, args: Vec) -> Result { let s = args[0].try_into_string()?; Ok(RuntimeValue::Str(s.trim().to_string())) } -fn replace(_ctx: &mut RuntimeContext, args: Vec) -> Result { +fn replace( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { let source = args[0].try_into_string()?; let from = args[1].try_into_string()?; let to = args[2].try_into_string()?; @@ -49,50 +52,77 @@ fn join(_ctx: &mut RuntimeContext, args: Vec) -> Result) -> Result { +fn to_lower( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { let s = args[0].try_into_string()?; Ok(RuntimeValue::Str(s.to_lowercase())) } -fn to_upper(_ctx: &mut RuntimeContext, args: Vec) -> Result { +fn to_upper( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { let s = args[0].try_into_string()?; Ok(RuntimeValue::Str(s.to_uppercase())) } -fn contains(_ctx: &mut RuntimeContext, args: Vec) -> Result { +fn contains( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { let s = args[0].try_into_string()?; let sub = args[1].try_into_string()?; Ok(RuntimeValue::Bool(s.contains(&sub))) } -fn from_int(_ctx: &mut RuntimeContext, args: Vec) -> Result { +fn from_int( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { let val = args[0].try_into_int()?; Ok(RuntimeValue::Str(val.to_string())) } -fn from_float(_ctx: &mut RuntimeContext, args: Vec) -> Result { +fn from_float( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { let val = args[0].try_into_float()?; Ok(RuntimeValue::Str(val.to_string())) } -fn from_bool(_ctx: &mut RuntimeContext, args: Vec) -> Result { +fn from_bool( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { let val = args[0].try_into_bool()?; Ok(RuntimeValue::Str(val.to_string())) } -fn trim(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) -> Result { +fn trim( + _ctx: &mut techscript_runtime::RuntimeContext, + args: Vec, +) -> Result { let s = args[0].try_into_string()?; Ok(RuntimeValue::Str(s.trim().to_string())) } -fn replace(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) -> Result { +fn replace( + _ctx: &mut techscript_runtime::RuntimeContext, + args: Vec, +) -> Result { let source = args[0].try_into_string()?; let from = args[1].try_into_string()?; let to = args[2].try_into_string()?; Ok(RuntimeValue::Str(source.replace(&from, &to))) } -fn split(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) -> Result { +fn split( + _ctx: &mut techscript_runtime::RuntimeContext, + args: Vec, +) -> Result { let s = args[0].try_into_string()?; let pat = args[1].try_into_string()?; let parts: Vec = s @@ -105,7 +135,10 @@ fn split(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) }) } -fn join(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) -> Result { +fn join( + _ctx: &mut techscript_runtime::RuntimeContext, + args: Vec, +) -> Result { let sep = args[1].try_into_string()?; if let RuntimeValue::List { items, .. } = &args[0] { let mut str_parts = Vec::new(); @@ -125,33 +158,51 @@ fn join(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) } } -fn to_lower(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) -> Result { +fn to_lower( + _ctx: &mut techscript_runtime::RuntimeContext, + args: Vec, +) -> Result { let s = args[0].try_into_string()?; Ok(RuntimeValue::Str(s.to_lowercase())) } -fn to_upper(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) -> Result { +fn to_upper( + _ctx: &mut techscript_runtime::RuntimeContext, + args: Vec, +) -> Result { let s = args[0].try_into_string()?; Ok(RuntimeValue::Str(s.to_uppercase())) } -fn contains(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) -> Result { +fn contains( + _ctx: &mut techscript_runtime::RuntimeContext, + args: Vec, +) -> Result { let s = args[0].try_into_string()?; let sub = args[1].try_into_string()?; Ok(RuntimeValue::Bool(s.contains(&sub))) } -fn from_int(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) -> Result { +fn from_int( + _ctx: &mut techscript_runtime::RuntimeContext, + args: Vec, +) -> Result { let val = args[0].try_into_int()?; Ok(RuntimeValue::Str(val.to_string())) } -fn from_float(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) -> Result { +fn from_float( + _ctx: &mut techscript_runtime::RuntimeContext, + args: Vec, +) -> Result { let val = args[0].try_into_float()?; Ok(RuntimeValue::Str(val.to_string())) } -fn from_bool(_ctx: &mut techscript_runtime::RuntimeContext, args: Vec) -> Result { +fn from_bool( + _ctx: &mut techscript_runtime::RuntimeContext, + args: Vec, +) -> Result { let val = args[0].try_into_bool()?; Ok(RuntimeValue::Str(val.to_string())) } diff --git a/stdlib/src/web.rs b/stdlib/src/web.rs index f557d529..b1af888f 100644 --- a/stdlib/src/web.rs +++ b/stdlib/src/web.rs @@ -362,7 +362,10 @@ fn dsl_to_html_inner(val: &RuntimeValue, html: &mut String) { } } -fn web_start(ctx: &mut RuntimeContext, args: Vec) -> Result { +fn web_start( + ctx: &mut RuntimeContext, + args: Vec, +) -> Result { if !ctx.config.capabilities.contains(&Capability::Network) { return Err(RuntimeError::new( techscript_runtime::error::RuntimeErrorKind::InvalidOperation( @@ -414,17 +417,26 @@ fn web_start(ctx: &mut RuntimeContext, args: Vec) -> Result) -> Result { +fn web_page( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { let _path = args[0].to_string(); let content = args[1].to_string(); *PAGE_CONTENT.lock().unwrap() = content; Ok(RuntimeValue::Null) } -fn web_serve(ctx: &mut RuntimeContext, args: Vec) -> Result { +fn web_serve( + ctx: &mut RuntimeContext, + args: Vec, +) -> Result { if !ctx.config.capabilities.contains(&Capability::Network) { return Err(RuntimeError::new( techscript_runtime::error::RuntimeErrorKind::InvalidOperation( @@ -474,21 +486,33 @@ fn web_serve(ctx: &mut RuntimeContext, args: Vec) -> Result) -> Result { +fn web_stop( + _ctx: &mut RuntimeContext, + _args: Vec, +) -> Result { SERVER_RUNNING.store(false, Ordering::SeqCst); Ok(RuntimeValue::Null) } -fn web_set_content(_ctx: &mut RuntimeContext, args: Vec) -> Result { +fn web_set_content( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { let new_content = args[0].to_string(); *PAGE_CONTENT.lock().unwrap() = new_content; Ok(RuntimeValue::Str("Content updated".to_string())) } -fn web_fetch(ctx: &mut RuntimeContext, args: Vec) -> Result { +fn web_fetch( + ctx: &mut RuntimeContext, + args: Vec, +) -> Result { if !ctx.config.capabilities.contains(&Capability::Network) { return Err(RuntimeError::new( techscript_runtime::error::RuntimeErrorKind::InvalidOperation( @@ -510,17 +534,13 @@ fn web_fetch(ctx: &mut RuntimeContext, args: Vec) -> Result) -> Result) -> Result) -> Result { +fn web_render_html( + ctx: &mut RuntimeContext, + _args: Vec, +) -> Result { let env = ctx.global_env.borrow(); let blocks = match env.lookup("_dsl_blocks") { Ok(RuntimeValue::List { items, .. }) => items.borrow().clone(), @@ -551,7 +572,10 @@ fn web_render_html(ctx: &mut RuntimeContext, _args: Vec) -> Result Ok(RuntimeValue::Str(html)) } -fn web_render_dsl(_ctx: &mut RuntimeContext, args: Vec) -> Result { +fn web_render_dsl( + _ctx: &mut RuntimeContext, + args: Vec, +) -> Result { let html = dsl_to_html(&args[0]); Ok(RuntimeValue::Str(html)) } diff --git a/stdlib/tests/stdlib_tests.rs b/stdlib/tests/stdlib_tests.rs index e29bad95..1a3d914d 100644 --- a/stdlib/tests/stdlib_tests.rs +++ b/stdlib/tests/stdlib_tests.rs @@ -559,12 +559,11 @@ fn test_http_module() { let listen = http.exports.get("listen").unwrap(); let res_listen = listen.call( &mut ctx_unprivileged, - vec![ - RuntimeValue::Int(8080), - RuntimeValue::Null, - ], + vec![RuntimeValue::Int(8080), RuntimeValue::Null], + ); + assert!( + matches!(res_listen, Err(techscript_runtime::RuntimeError { kind: techscript_runtime::RuntimeErrorKind::InvalidOperation(msg), .. }) if msg.contains("Security policy violation")) ); - assert!(matches!(res_listen, Err(techscript_runtime::RuntimeError { kind: techscript_runtime::RuntimeErrorKind::InvalidOperation(msg), .. }) if msg.contains("Security policy violation"))); // Test with Network capability let mut caps = HashSet::new();