mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-07-05 18:58:30 +00:00
Make realloc() go 100x faster on Linux/NetBSD
Cosmopolitan now supports mremap(), which is only supported on Linux and NetBSD. First, it allows memory mappings to be relocated without copying them; this can dramatically speed up data structures like std::vector if the array size grows larger than 256kb. The mremap() system call is also 10x faster than munmap() when shrinking large memory mappings. There's now two functions, getpagesize() and getgransize() which help to write portable code that uses mmap(MAP_FIXED). Alternative sysconf() may be called with our new _SC_GRANSIZE. The madvise() system call now has a better wrapper with improved documentation.
This commit is contained in:
parent
196942084b
commit
f7780de24b
71 changed files with 1301 additions and 640 deletions
|
@ -36,35 +36,12 @@ static inline void tree_set_red(struct Tree *node, int red) {
|
|||
node->word |= red;
|
||||
}
|
||||
|
||||
forceinline optimizespeed struct Tree *tree_floor(const struct Tree *node,
|
||||
const void *key,
|
||||
tree_search_f *cmp) {
|
||||
struct Tree *left = 0;
|
||||
while (node) {
|
||||
if (cmp(key, node) >= 0) {
|
||||
left = (struct Tree *)node;
|
||||
node = tree_get_left(node);
|
||||
} else {
|
||||
node = node->right;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
static inline struct Tree *tree_ceil(const struct Tree *node, const void *key,
|
||||
tree_search_f *cmp) {
|
||||
struct Tree *right = 0;
|
||||
while (node) {
|
||||
if (cmp(key, node) < 0) {
|
||||
right = (struct Tree *)node;
|
||||
node = tree_get_left(node);
|
||||
} else {
|
||||
node = node->right;
|
||||
}
|
||||
}
|
||||
return right;
|
||||
}
|
||||
|
||||
// Returns node equal to given key.
|
||||
//
|
||||
// [1 3 5 7] [1 3 5 7] [1 3 5 7]
|
||||
// NULL ↑ NULL
|
||||
// 4 3 8
|
||||
//
|
||||
static inline struct Tree *tree_get(const struct Tree *node, const void *key,
|
||||
tree_search_f *cmp) {
|
||||
while (node) {
|
||||
|
@ -80,6 +57,72 @@ static inline struct Tree *tree_get(const struct Tree *node, const void *key,
|
|||
return 0;
|
||||
}
|
||||
|
||||
// Returns last node less than or equal to given key.
|
||||
//
|
||||
// [1 3 5 7] [1 3 5 7] [1 3 5 7]
|
||||
// ↑ ↑ ↑
|
||||
// 4 3 8
|
||||
//
|
||||
forceinline optimizespeed struct Tree *tree_floor(const struct Tree *node,
|
||||
const void *key,
|
||||
tree_search_f *cmp) {
|
||||
struct Tree *left = 0;
|
||||
while (node) {
|
||||
int c = cmp(key, node);
|
||||
if (c < 0) {
|
||||
node = tree_get_left(node);
|
||||
} else if (c > 0) {
|
||||
left = (struct Tree *)node;
|
||||
node = node->right;
|
||||
} else {
|
||||
return (struct Tree *)node;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
// Returns first node not less than given key.
|
||||
//
|
||||
// [1 3 5 7] [1 3 5 7] [1 3 5 7]
|
||||
// ↑ ↑ NULL
|
||||
// 4 3 8
|
||||
//
|
||||
static inline struct Tree *tree_lower(const struct Tree *node, const void *key,
|
||||
tree_search_f *cmp) {
|
||||
struct Tree *left = 0;
|
||||
while (node) {
|
||||
int c = cmp(key, node);
|
||||
if (c <= 0) {
|
||||
left = (struct Tree *)node;
|
||||
node = tree_get_left(node);
|
||||
} else {
|
||||
node = node->right;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
// Returns first node greater than than given key.
|
||||
//
|
||||
// [1 3 5 7] [1 3 5 7] [1 3 5 7]
|
||||
// ↑ ↑ NULL
|
||||
// 4 3 8
|
||||
//
|
||||
static inline struct Tree *tree_ceil(const struct Tree *node, const void *key,
|
||||
tree_search_f *cmp) {
|
||||
struct Tree *left = 0;
|
||||
while (node) {
|
||||
int c = cmp(key, node);
|
||||
if (c < 0) {
|
||||
left = (struct Tree *)node;
|
||||
node = tree_get_left(node);
|
||||
} else {
|
||||
node = node->right;
|
||||
}
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
struct Tree *tree_next(struct Tree *) libcesque;
|
||||
struct Tree *tree_prev(struct Tree *) libcesque;
|
||||
struct Tree *tree_first(struct Tree *) libcesque;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue