-
Notifications
You must be signed in to change notification settings - Fork 203
Use calloc rather than malloc + memset #519
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1378,6 +1378,11 @@ static size_t js_malloc_usable_size_unknown(const void *ptr) | |
return 0; | ||
} | ||
|
||
void *js_calloc_rt(JSRuntime *rt, size_t count, size_t size) | ||
{ | ||
return rt->mf.js_calloc(&rt->malloc_state, count, size); | ||
} | ||
|
||
void *js_malloc_rt(JSRuntime *rt, size_t size) | ||
{ | ||
return rt->mf.js_malloc(&rt->malloc_state, size); | ||
|
@@ -1404,13 +1409,16 @@ size_t js_malloc_usable_size_rt(JSRuntime *rt, const void *ptr) | |
return rt->mf.js_malloc_usable_size(ptr); | ||
} | ||
|
||
/** | ||
* This used to be implemented as malloc + memset, but using calloc | ||
* yields better performance in initial, bursty allocations, something useful | ||
* for QuickJS. | ||
* | ||
* More information: https://github.com/quickjs-ng/quickjs/pull/519 | ||
*/ | ||
void *js_mallocz_rt(JSRuntime *rt, size_t size) | ||
{ | ||
void *ptr; | ||
ptr = js_malloc_rt(rt, size); | ||
if (!ptr) | ||
return NULL; | ||
return memset(ptr, 0, size); | ||
return js_calloc_rt(rt, 1, size); | ||
} | ||
|
||
/* called by libbf */ | ||
|
@@ -1420,6 +1428,18 @@ static void *js_bf_realloc(void *opaque, void *ptr, size_t size) | |
return js_realloc_rt(rt, ptr, size); | ||
} | ||
|
||
/* Throw out of memory in case of error */ | ||
void *js_calloc(JSContext *ctx, size_t count, size_t size) | ||
{ | ||
void *ptr; | ||
ptr = js_calloc_rt(ctx->rt, count, size); | ||
if (unlikely(!ptr)) { | ||
JS_ThrowOutOfMemory(ctx); | ||
return NULL; | ||
} | ||
return ptr; | ||
} | ||
|
||
/* Throw out of memory in case of error */ | ||
void *js_malloc(JSContext *ctx, size_t size) | ||
{ | ||
|
@@ -1631,10 +1651,9 @@ JSRuntime *JS_NewRuntime2(const JSMallocFunctions *mf, void *opaque) | |
ms.opaque = opaque; | ||
ms.malloc_limit = 0; | ||
|
||
rt = mf->js_malloc(&ms, sizeof(JSRuntime)); | ||
rt = mf->js_calloc(&ms, 1, sizeof(JSRuntime)); | ||
if (!rt) | ||
return NULL; | ||
memset(rt, 0, sizeof(*rt)); | ||
rt->mf = *mf; | ||
if (!rt->mf.js_malloc_usable_size) { | ||
/* use dummy function if none provided */ | ||
|
@@ -1699,6 +1718,30 @@ void JS_SetRuntimeOpaque(JSRuntime *rt, void *opaque) | |
rt->user_opaque = opaque; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto. |
||
} | ||
|
||
static void *js_def_calloc(JSMallocState *s, size_t count, size_t size) | ||
{ | ||
void *ptr; | ||
|
||
/* Do not allocate zero bytes: behavior is platform dependent */ | ||
assert(count != 0 && size != 0); | ||
|
||
if (size > 0) | ||
if (unlikely(count != (count * size) / size)) | ||
return NULL; | ||
|
||
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */ | ||
if (unlikely(s->malloc_size + (count * size) > s->malloc_limit - 1)) | ||
return NULL; | ||
|
||
ptr = calloc(count, size); | ||
if (!ptr) | ||
return NULL; | ||
|
||
s->malloc_count++; | ||
s->malloc_size += js__malloc_usable_size(ptr) + MALLOC_OVERHEAD; | ||
return ptr; | ||
} | ||
|
||
static void *js_def_malloc(JSMallocState *s, size_t size) | ||
{ | ||
void *ptr; | ||
|
@@ -1738,13 +1781,11 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size) | |
return NULL; | ||
return js_def_malloc(s, size); | ||
} | ||
old_size = js__malloc_usable_size(ptr); | ||
if (size == 0) { | ||
s->malloc_count--; | ||
s->malloc_size -= old_size + MALLOC_OVERHEAD; | ||
free(ptr); | ||
if (unlikely(size == 0)) { | ||
js_def_free(s, ptr); | ||
return NULL; | ||
} | ||
old_size = js__malloc_usable_size(ptr); | ||
/* When malloc_limit is 0 (unlimited), malloc_limit - 1 will be SIZE_MAX. */ | ||
if (s->malloc_size + size - old_size > s->malloc_limit - 1) | ||
return NULL; | ||
|
@@ -1758,6 +1799,7 @@ static void *js_def_realloc(JSMallocState *s, void *ptr, size_t size) | |
} | ||
|
||
static const JSMallocFunctions def_malloc_funcs = { | ||
js_def_calloc, | ||
js_def_malloc, | ||
js_def_free, | ||
js_def_realloc, | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
count * size
can overflow, you should probably handle that:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An interesting tidbit... I was going to suggest to do the overflow check like this:
...on the assumption it's faster because it's replacing a variable (
count
) with a constant (SIZE_MAX
) but... it's not!gcc at -O3 compiles it to mul + jo (jump if overflow) in both cases, but in the SIZE_MAX code, it prepends a test + jz (jump if zero). Must be a missed optimization because omitting the test + jz doesn't affect correctness.
Even more interestingly: gcc replaces malloc+memset with calloc calls!
Code:
Assembly: