Skip to content

Add realloc#717

Merged
akheron merged 7 commits intoakheron:masterfrom
WilhelmWiens:Realloc
Jul 14, 2025
Merged

Add realloc#717
akheron merged 7 commits intoakheron:masterfrom
WilhelmWiens:Realloc

Conversation

@WilhelmWiens
Copy link
Contributor

Make use of realloc. This can be a significant performance improvement.

@akheron
Copy link
Owner

akheron commented Jul 6, 2025

Thanks for the PR!

I haven't benchmarked myself, but a quick google search suggests that malloc + free is faster when you don't want to keep the previous data. hashtable_do_rehash seems to do just this, so maybe using realloc is not justified there.

@WilhelmWiens
Copy link
Contributor Author

The old implementation was optimized to both insertions in the middle and at the end.

With Realloc, appending is now faster, when the memory area could be expanded, and about as fast when the array had to be copied.
When inserting in the middle, the best case is about the same. At worst case, the memory after the insertion is copied twice now.

What do you think about the change in the array? @akheron

@akheron
Copy link
Owner

akheron commented Jul 14, 2025

Well, I've always maintained the guideline that jansson is not a data structure or algorithm library. As such, raw speed is not a priority, only a convenience 😄.

I think the array code is now better, as it's easier to understand AND faster in some cases.

@akheron akheron merged commit 1f875ce into akheron:master Jul 14, 2025
17 checks passed
@WilhelmWiens WilhelmWiens deleted the Realloc branch July 16, 2025 22:44
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