Make sorted serialization faster

Redbean Lua and JSON serialization now goes faster because we're now
inserting object entries into tree data structure rather than making
an array and sorting it at the end. For example, when serializing an
object with 10,000 entries this goes twice as fast. However it still
goes slower than saying EncodeJson(x, {sorted=false}).
This commit is contained in:
Justine Tunney 2022-07-22 04:19:01 -07:00
parent 9de3d8f1e6
commit 84caee23ba
12 changed files with 122 additions and 224 deletions

View file

@ -25,10 +25,15 @@
* Inserts 𝑢 into 𝑡.
* @param t tree
* @param u NUL-terminated string
* @return true if 𝑡 was mutated
* @return true if 𝑡 was mutated, or -1 w/ errno
* @note h/t djb and agl
*/
bool critbit0_insert(struct critbit0 *t, const char *u) {
size_t ulen = strlen(u);
return critbit0_emplace(t, memcpy(malloc(ulen + 1), u, ulen + 1), ulen);
int critbit0_insert(struct critbit0 *t, const char *u) {
char *p;
size_t n;
if ((p = malloc((n = strlen(u)) + 1))) {
return critbit0_emplace(t, memcpy(p, u, n + 1), n);
} else {
return -1;
}
}