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

Reduce reallocation in buffer expansion #436

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

wyattgill9
Copy link

Summary

Optimizes buffer growth strategy by reducing unnecessary reallocations realloc() (calls) improving performance for large reads, with no small read difference

Changes: run.c

  • Changed buffer expansion logic for efficiency.
  • Reduces realloc calls, improving large read performance.

Code changed

if (bytes.len == capacity - 1) {
  capacity *= 2;
  bytes.buf = realloc(bytes.buf, capacity);
}

=>

if (bytes.len + 16 >= capacity) {  // Grow in larger increments
  capacity = capacity * 2 + 128;   // Avoid frequent realloc
  bytes.buf = realloc(bytes.buf, capacity);
}

I can tweak the exact numbers, but this is a good start

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.

1 participant