From 60dc18c9fcb2550e15a35809818764ee43a178c7 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 18 Jan 2022 11:45:58 -0500 Subject: dstring: Set a minimum capacity to avoid reallocating for small strings --- dstring.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/dstring.c b/dstring.c index 58e74fe..f344d09 100644 --- a/dstring.c +++ b/dstring.c @@ -1,6 +1,6 @@ /**************************************************************************** * bfs * - * Copyright (C) 2016-2020 Tavian Barnes * + * Copyright (C) 2016-2022 Tavian Barnes * * * * 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; -- cgit v1.2.3