Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

utils: improve vector macros #213

Merged
merged 2 commits into from
Oct 18, 2024
Merged

utils: improve vector macros #213

merged 2 commits into from
Oct 18, 2024

Conversation

jow-
Copy link
Owner

@jow- jow- commented Jul 24, 2024

  • Make uc_vector_push() and uc_vector_grow() return pointers to the first added vector element

  • Refactor uc_vector_push() to be able to take struct initialization literals as macro argument

  • Make uc_vector_last() NULL safe

As a side effect, the size of libucode.so decreases by about 2KB on x86.

@IdWV
Copy link
Contributor

IdWV commented Oct 11, 2024

As a side effect, the size of libucode.so decreases by about 2KB on x86.

As basically the same functionality is implemented, I suppose this is caused by the optimizer choosing not to inline the function uc_vector_grow_() when called more than once in the same compilation unit?

About the unsuccessful check, where am I looking at? Scan-build says there are 8 errors found, but I see no errors. I do see warnings though. Are these promoted to errors by scan-build?

If that is the case, is it right that these potential errors have always been there, and are only now recognized by the compiler due to the code change?

@jow- jow- force-pushed the improve-vector-macros branch 3 times, most recently from a4afe2d to d0e017f Compare October 17, 2024 13:58
jow- added 2 commits October 18, 2024 14:33
This commits introduces a number of new helper macros to deal with vectors
and refactors the existing code for better resource utilization.

The allocation strategy is changed from multiple of 8 to exponential growth
by factor 1.5 in order to minimize the number of reallocations and
potentially needed memory copies.

The newly introduced macros are:

 - uc_vector_capacity(init_capacity, add_items)
   Derive the resulting vector capacity from the given item count
   and initial capacity.

 - uc_vector_extend(vector, add_items)
   Increase vector capacity by given amount of items, zero-initialize
   added capacity and return pointer to first new item past the current
   length.

 - uc_vector_reduce(vector, remove_items)
   Reduce vector capacity by given amount of items.

 - uc_vector_pop(vector)
   Return pointer to last element and decrement count, or NULL if the
   vector is empty.

 - uc_vector_foreach(vector, itervar)
   A for() loop wrapper to iterate vectors, providing an iter variable
   to the loop body.

 - uc_vector_foreach_reverse(vector, itervar)
   A for() loop wrapper to iterate vectors backwards, providing an iter
   variable to the loop body.

The uc_vector_push() macro has been changed into a variadic macro which
internally prefixes the argument list with a cast to the vector element
type, allowing user to pass compound expressions like struct initializers
in order to simplify adding elements:

    uc_vector_push(&my_collection, {
        .foo = 1,
        .bar = "qrx"
    });

Like uc_vector_pop(), the uc_vector_last() macro has been made safe to
use on empty vectors, it'll now return NULL in this case.

Finally the vector realloc logic was moved into static functions within
the header file, allowing all vector using code of a compilation unit to
share the reallocation, shrinking the size of libucode.so by 1-2KB as a
side effect.

Signed-off-by: Jo-Philipp Wich <[email protected]>
Utilize uc_vector_push() and uc_vector_foreach() where applicable.

Signed-off-by: Jo-Philipp Wich <[email protected]>
@jow- jow- force-pushed the improve-vector-macros branch from d0e017f to e5fe6b1 Compare October 18, 2024 12:33
@jow- jow- merged commit 4b18a9b into master Oct 18, 2024
7 checks passed
@jow- jow- deleted the improve-vector-macros branch October 18, 2024 13:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants