From 66f70851b44e9358b3acdaa6e7d91561a6cf1c2e Mon Sep 17 00:00:00 2001 From: tignear Date: Wed, 28 Oct 2026 21:01:16 +0900 Subject: [PATCH] perf(vpi): avoid allocating empty names for temporary handles Every callback registration allocated two empty CString buffers for its handle name and full name. Keep unnamed callback, iterator and constant handles without owned names and return the shared empty C string for those properties. Named scopes and signals retain their owned strings. Extend existing public API regressions to check empty callback and iterator names alongside the named hierarchy. Validation: all 68 VPI unit/API tests, all 10 ignored cocotb integrations, all-target clippy with warnings denied, and workspace formatting. Change-Id: Ibf866696869af6e0ab4b5e2720e9c2a8a8f7a738 --- crates/celox-vpi/src/lib.rs | 30 +++++++++++++++++------------ crates/celox-vpi/tests/basic_vpi.rs | 7 +++++++ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/crates/celox-vpi/src/lib.rs b/crates/celox-vpi/src/lib.rs index 58899198424..4ec79997709 100644 --- a/crates/celox-vpi/src/lib.rs +++ b/crates/celox-vpi/src/lib.rs @@ -354,8 +354,9 @@ enum HandleKind { /// Opaque allocation referenced by the C `vpiHandle` pointer type. pub struct VpiHandleObject { kind: HandleKind, - name: CString, - full_name: CString, + // Unnamed temporary handles share the empty C string without allocating it. + name: Option, + full_name: Option, value_string: Vec, value_vector: Vec, } @@ -868,8 +869,8 @@ fn new_object_handle(object: ObjectRef) -> VpiHandle { .unwrap_or_default(); Box::into_raw(Box::new(VpiHandleObject { kind: HandleKind::Object(object), - name: c_string(&name), - full_name: c_string(&full_name), + name: Some(c_string(&name)), + full_name: Some(c_string(&full_name)), value_string: Vec::new(), value_vector: Vec::new(), })) @@ -881,8 +882,8 @@ fn new_iterator_handle(objects: Vec) -> VpiHandle { } Box::into_raw(Box::new(VpiHandleObject { kind: HandleKind::Iterator { objects, next: 0 }, - name: c_string(""), - full_name: c_string(""), + name: None, + full_name: None, value_string: Vec::new(), value_vector: Vec::new(), })) @@ -891,8 +892,8 @@ fn new_iterator_handle(objects: Vec) -> VpiHandle { fn new_constant_handle(value: i32) -> VpiHandle { Box::into_raw(Box::new(VpiHandleObject { kind: HandleKind::Constant(value), - name: c_string(""), - full_name: c_string(""), + name: None, + full_name: None, value_string: Vec::new(), value_vector: Vec::new(), })) @@ -901,8 +902,8 @@ fn new_constant_handle(value: i32) -> VpiHandle { fn new_callback_handle(id: u64) -> VpiHandle { Box::into_raw(Box::new(VpiHandleObject { kind: HandleKind::Callback(id), - name: c_string(""), - full_name: c_string(""), + name: None, + full_name: None, value_string: Vec::new(), value_vector: Vec::new(), })) @@ -1272,8 +1273,13 @@ pub unsafe extern "C" fn vpi_get_str(property: i32, reference: VpiHandle) -> *mu return ptr::null_mut(); }; match property { - VPI_NAME => handle.name.as_ptr().cast_mut(), - VPI_FULL_NAME => handle.full_name.as_ptr().cast_mut(), + VPI_NAME => handle.name.as_deref().unwrap_or(c"").as_ptr().cast_mut(), + VPI_FULL_NAME => handle + .full_name + .as_deref() + .unwrap_or(c"") + .as_ptr() + .cast_mut(), VPI_DEF_NAME => { let HandleKind::Object(ObjectRef::Scope(id)) = handle.kind else { return ptr::null_mut(); diff --git a/crates/celox-vpi/tests/basic_vpi.rs b/crates/celox-vpi/tests/basic_vpi.rs index ec7d936b646..4f64341ea6e 100644 --- a/crates/celox-vpi/tests/basic_vpi.rs +++ b/crates/celox-vpi/tests/basic_vpi.rs @@ -1031,6 +1031,8 @@ fn vpi_discovers_hierarchy_and_reads_and_writes_values() { assert_eq!(CStr::from_ptr(vpi_get_str(VPI_NAME, top)), c"Top"); let modules = vpi_iterate(VPI_MODULE, top); + assert_eq!(CStr::from_ptr(vpi_get_str(VPI_NAME, modules)), c""); + assert_eq!(CStr::from_ptr(vpi_get_str(VPI_FULL_NAME, modules)), c""); let child = vpi_scan(modules); assert!(!child.is_null()); assert_eq!( @@ -1940,6 +1942,11 @@ fn callback_registration_owns_copied_time_and_value_records() { let a = vpi_handle_by_name(c"Top.a".as_ptr(), ptr::null_mut()); let value_change = register_stack_value_callback(a); assert!(!value_change.is_null()); + assert_eq!(CStr::from_ptr(vpi_get_str(VPI_NAME, value_change)), c""); + assert_eq!( + CStr::from_ptr(vpi_get_str(VPI_FULL_NAME, value_change)), + c"" + ); register_stack_timer(a); // Reuse the caller stack after both registration functions returned.