diff options
author | Tavian Barnes <tavianator@tavianator.com> | 2022-01-18 11:45:58 -0500 |
---|---|---|
committer | Tavian Barnes <tavianator@tavianator.com> | 2022-01-18 12:27:29 -0500 |
commit | 60dc18c9fcb2550e15a35809818764ee43a178c7 (patch) | |
tree | b2ff70dffc27e9c1f119baab3456d3507a3a5969 | |
parent | 4cdb17edde114f1907e27dbf8f101b1261f1021f (diff) | |
download | bfs-60dc18c9fcb2550e15a35809818764ee43a178c7.tar.xz |
dstring: Set a minimum capacity to avoid reallocating for small strings
-rw-r--r-- | dstring.c | 7 |
1 files changed, 6 insertions, 1 deletions
@@ -1,6 +1,6 @@ /**************************************************************************** * bfs * - * Copyright (C) 2016-2020 Tavian Barnes <tavianator@tavianator.com> * + * Copyright (C) 2016-2022 Tavian Barnes <tavianator@tavianator.com> * * * * Permission to use, copy, modify, and/or distribute this software for any * * purpose with or without fee is hereby granted. * @@ -42,6 +42,11 @@ static size_t dstrsize(size_t capacity) { /** Allocate a dstring with the given contents. */ static char *dstralloc_impl(size_t capacity, size_t length, const char *data) { + // Avoid reallocations for small strings + if (capacity < 7) { + capacity = 7; + } + struct dstring *header = malloc(dstrsize(capacity)); if (!header) { return NULL; |