-
Notifications
You must be signed in to change notification settings - Fork 130
Introduce generic dynamic array with unit tests #212
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
base: master
Are you sure you want to change the base?
Conversation
Introduce a reusable, arena-allocated dynamic array implementation. The new 'dynarr_t' type encapsulates element size, current size, capacity, and a pointer to the arena allocator. Core operations include: - 'dynarr_init': initialize array with specified capacity. - 'dynarr_reserve': reserve given capacity. - 'dynarr_resize': adjust array size, growing if needed. - 'dynarr_push_raw/byte/word': append elements of arbitrary types. - 'dynarr_extend': bulk append buffer of elements. - 'dynarr_get_raw/byte/word': retrieve elements by index with checks. - 'dynarr_set_raw': overwrite an element at a given index. Relying on the existing arena allocator ensures proper byte alignment and eliminates failure checks. This implementation can replace the current 'strbuf_t' and most fixed-size arrays. In addition to improving consistency in memory management, the built-in boundary checks enhance safety, while the impact on performance remains within an acceptable margin. Co-authored-by: Jim Hsu <[email protected]>
Add a set of unit tests for 'dynarr_t' to verify correctness and serve as usage examples. These tests cover initialization, resizing, appending, retrieval, and error conditions, helping contributors understand how to use the API safely and correctly.
Replaces the "strbuf_t *SOURCE" in "src/global.c" with "dynarr_t *". Co-authored-by: Jim Hsu <[email protected]>
I’ve come up with a possible solution for this issue. Maybe we can create a new This structure would also make it easy to add unit tests for other components like the Would this approach be acceptable? If so, I’ll include this change in the |
It is not necessary to create |
Thank you for the guidance! Do you mean writing the unit tests directly in If it’s writing them in If it’s the latter (writing
Is there a recommended workaround or a more flexible way to handle these kinds of tests? |
You can improve
You can create another shell script like |
Although, as you stated previously, shecc currently has an out-of-bounds access on
Therefore, I suggest removing this statement unless you've confirmed this behavior through further testing. |
I'm afraid that the inclusion of
What do you think? @jserv |
It sounds fine. However the snapshot was a hack when we lack of analysis tools for IR. Maybe we can drop it later. |
Motivation
TYPES
andPH2_IR_FLATTEN
are hard to extend.strbuf_t
stores bytes only; it cannot handle pointers or large structs (type_t
, etc.).What’s in this PR
Add generic dynamic array
dynarr_t
storessize
,capacity
,elem_size
, and an arena pointer.init
,reserve
,resize
,push_*
,extend
,get_*
,set_raw
.Add unit tests (
tests/test_dynarr.c
)Replace
strbuf_t *SOURCE
insrc/global.c
withdynarr_t *
Advantages
dynarr_get_byte
untilSOURCE
OOB issue is fixed).Known issues
test_dynarr.c
currently#include "src/globals.c"
, so any change inglobals.c
requiresmake update-snapshots
to pass snapshot checkshecc currently has OOB access on SOURCE bytes.
Next steps
strbuf_t
useses todynarr_t
.dynarr_t
.Performance impact
benchmark
Benchmark Machine
Benchmark Machine
Benchmark Script
Benchmark Script
Before
Result
After
Result
After benchmark, we observed an average runtime difference of around 15ms in stage-0 compilation.
Considering the total build time and the scope of changes, this delta seems reasonable and within acceptable limits.
Summary by Bito
This pull request introduces a generic dynamic array implementation, replacing fixed-size buffers to enhance flexibility, performance, and memory management. It includes comprehensive unit tests covering various functionalities and updates to global state management, lexer, and parser to utilize the new structure, improving overall code quality.