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

sort: Avoid linear realloc loops #2137

Merged
merged 1 commit into from
Jul 3, 2024
Merged

Conversation

markjdb
Copy link
Contributor

@markjdb markjdb commented Jul 1, 2024

These interact quite poorly with MRS with revocation enabled, which implements realloc() by always creating a new allocation. Even a modification to return the original pointer if its bounds satisfy the request doesn't help much there.

Apply a simple bandaid: grow the allocation exponentially. This has no major downside except when virtual address space is scarce, and that's something we don't typically worry about. There doesn't appear to be any explicit justification for growing buffers so conservatively.

This yields a significant reduction in the amount of time needed to build a cscope database (cscope invokes sort(1) under the hood).

These interact quite poorly with MRS with revocation enabled, which implements
realloc() by always creating a new allocation.  Even a modification to return
the original pointer if its bounds satisfy the request doesn't help much there.

Apply a simple bandaid: grow the allocation exponentially.  This has no major
downside except when virtual address space is scarce, and that's something we
don't typically worry about.  There doesn't appear to be any explicit
justification for growing buffers so conservatively.
@markjdb markjdb requested a review from brooksdavis July 1, 2024 16:43
@bsdjhb
Copy link
Collaborator

bsdjhb commented Jul 1, 2024

This may be a quirk worth documenting in the CHERI C programmers guide. I feel like I've seen various programs that cap geometric growth so that at some point only a fixed count of items is added instead of always doubling.

@markjdb
Copy link
Contributor Author

markjdb commented Jul 2, 2024

This may be a quirk worth documenting in the CHERI C programmers guide. I feel like I've seen various programs that cap geometric growth so that at some point only a fixed count of items is added instead of always doubling.

See my comment in PR #2133 - I think we can alleviate the problem if jemalloc is willing to loosen its bounds to those of the underlying allocation size, and that's probably closer the right solution.

@brooksdavis brooksdavis merged commit 57ab9b3 into CTSRD-CHERI:dev Jul 3, 2024
29 checks passed
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.

3 participants