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

Small over allocation in each aws_string #776

Closed
giltho opened this issue Mar 12, 2021 · 1 comment
Closed

Small over allocation in each aws_string #776

giltho opened this issue Mar 12, 2021 · 1 comment
Assignees
Labels
bug This issue is a bug. investigating This issue is being investigated and/or work is in progress to resolve the issue. p3 This is a minor priority issue

Comments

@giltho
Copy link

giltho commented Mar 12, 2021

Hi !

It seems that aws_string_new_from_array is over allocating a bit of memory in each string.
Because of the fact that aws_string is defined as follow :

struct aws_string {
    struct aws_allocator *const allocator;
    const size_t len;
    const uint8_t bytes[1];
};

given alignment constraints in C, sizeof (struct aws_string) is 24.
The offset in a string struct denoted by str->bytes is 16 (allocator is at 0, len is at 8).

Now, aws_string_new_from_array allocates memory in the following way:

size_t malloc_size;
    if (aws_add_size_checked(sizeof(struct aws_string) + 1, len, &malloc_size)) {
        return NULL;
    }
    struct aws_string *str = aws_mem_acquire(allocator, malloc_size);
    if (!str) {
        return NULL;
    }

so it allocates 24 + length + 1 bytes, while only 16 + length + 1 bytes are needed.
So every aws_string contains 8 too many bytes that will never be accessed at the end of the object.
It's really not a lot, but it's an easy fix.

@jmklix jmklix self-assigned this Sep 21, 2023
@jmklix jmklix added investigating This issue is being investigated and/or work is in progress to resolve the issue. p3 This is a minor priority issue bug This issue is a bug. labels Sep 21, 2023
@jmklix jmklix assigned DmitriyMusatkin and unassigned jmklix Mar 25, 2024
@DmitriyMusatkin
Copy link
Contributor

#1099 should resolve the overallocation.

@jmklix jmklix closed this as completed Apr 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. investigating This issue is being investigated and/or work is in progress to resolve the issue. p3 This is a minor priority issue
Projects
None yet
Development

No branches or pull requests

3 participants