Skip to content

Conversation

@apocelipes
Copy link
Contributor

Consider some corner cases:

  • allocate was 28, need a new cap of 31 bytes. The old code grows the cap to 56 bytes. 24 bytes were wasted. (see below, you could waste more than 24 bytes)
  • allocate was 8, need a new cap of 18 bytes. allocate was 8 -> 16 -> 32, no waste but multiplicated twice.
  • allocate was 5, need a 11 bytes new cap. allocate was 5 -> 10 -> 20. You may think the new code will waste 12 bytes. However on the most of modern systems small memory areas are managed by blocks, blocks' sizes usually are powers of 2 or multiples of 16. Unfortunately, 20 is neither a power of 2 nor a multiple of 16, so it will be allocated into a 32-bytes block. Eventually there's no waste.

if 0 and 1 can be extended to 32 why 2, 3, 4, 5 cannot? It didn't hold water. Use FASTFETCH_STRBUF_DEFAULT_ALLOC as the smallest allocation size is more reasonable and more useful. Change if(allocate < 2) to if(allocate == 0) is resaonable but not useful.

In addition, the corner case actually exists in fastfetch -c all:

old

After fixing:

new

In other cases, memory usage did not change.

@CarterLi CarterLi merged commit 7b81ffc into fastfetch-cli:dev Nov 10, 2024
16 checks passed
@apocelipes apocelipes deleted the fix-ffStrbufEnsureFree branch November 10, 2024 10:38
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