From 22de385eb72aca9759e51e6e7a4eb259d48ed33f Mon Sep 17 00:00:00 2001 From: Ganesh Reddy Date: Tue, 30 Sep 2025 20:54:29 +0530 Subject: [PATCH 1/2] docs: update CopyElement benchmark walkthrough to reflect current implementation --- docs/benchmark.md | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/docs/benchmark.md b/docs/benchmark.md index 95fc0de17..996c11fab 100644 --- a/docs/benchmark.md +++ b/docs/benchmark.md @@ -34,23 +34,36 @@ demonstrates all of these principles and how they are used to implement an actual improvement to the library. Let's analyze an improvement to the performance of `stumpless_copy_element` to do this. -An early version of `stumpless_copy_element` iterated through all of the -params in the element, adding them to the copy one by one. The code for this -looked like this snippet, which has been abbreviated to focus on the performance -of the code: +Previously, `stumpless_copy_element` manually iterated through all parameters +in an element, copying and adding them one by one. While this approach was +straightforward, it was inefficient due to repeated memory reallocations. + +The function has since been optimized to handle parameter copying internally. +The benchmark now reflects this cleaner implementation: + ```c -// first create a new element -copy = stumpless_new_element( stumpless_get_element_name( element ) ); +static void CopyElement(benchmark::State& state){ + struct stumpless_element *element; + const struct stumpless_element *result; -// then handle all of the parameters -for( i = 0; i < element->param_count; i++ ) { - // copy the parameter - param_copy = stumpless_copy_param( element->params[i] ); + INIT_MEMORY_COUNTER( copy_element ); - // and then add it - stumpless_add_param( copy, param_copy ); -} + element = stumpless_new_element( "copy-element-perf" ); + stumpless_add_new_param( element, "param-1", "value-1" ); + stumpless_add_new_param( element, "param-2", "value-2" ); + + for(auto _ : state){ + result = stumpless_copy_element( element ); + if( !result ) { + state.SkipWithError( "the element copy failed" ); + } else { + stumpless_destroy_element_and_contents( result ); + } + } + + stumpless_destroy_element_and_contents( element ); + stumpless_free_all( ); ``` While it is logical and easy to follow, this method is inefficient because From 179d2bc558c9db2821de6d0ed28a3173b78eeac9 Mon Sep 17 00:00:00 2001 From: Ganesh Reddy Date: Wed, 1 Oct 2025 12:28:53 +0530 Subject: [PATCH 2/2] fix: correct result check in CopyElement benchmark --- docs/benchmark.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/benchmark.md b/docs/benchmark.md index 996c11fab..80dd5fe57 100644 --- a/docs/benchmark.md +++ b/docs/benchmark.md @@ -91,8 +91,8 @@ static void CopyElement(benchmark::State& state){ for(auto _ : state){ result = stumpless_copy_element( element ); - if( result <= 0 ) { - state.SkipWithError( "could not send an entry to the target" ); + if( !result ) { + state.SkipWithError( "the element copy failed" ); } else { stumpless_destroy_element_and_contents( result ); }