From 2d8085fd8474b28ceeafadc32fb440ab8dfd332e Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Thu, 30 Jan 2025 20:16:34 -0800 Subject: get-csv: use unsafe version string-extend. Another almost 16% speedup. * lib.c (us_length_STR): New static function. (string_extend): Use us_length_STR, since we know the object is of type STR. (us_string_extend_STR_CHR): New function. (length_str): Handle STR case via use_length_STR. * lib.h (us_string_extend_STR_CHR): Declared. * stream.c (get_csv): Use us_string_extend_STR_CHR instead of string_extend. --- lib.c | 49 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) (limited to 'lib.c') diff --git a/lib.c b/lib.c index d0667a57..ed0a47fe 100644 --- a/lib.c +++ b/lib.c @@ -5384,6 +5384,15 @@ val downcase_str(val str) return out; } +static val us_length_STR(val str, val self) +{ + if (!str->st.len) { + set(mkloc(str->st.len, str), num(wcslen(str->st.str))); + str->st.alloc = c_num(str->st.len, self) + 1; + } + return str->st.len; +} + val string_extend(val str, val tail, val finish_in) { val self = lit("string-extend"); @@ -5391,7 +5400,7 @@ val string_extend(val str, val tail, val finish_in) type_check(self, str, STR); { val finish = default_null_arg(finish_in); - cnum len = c_fixnum(length_str(str), self); + cnum len = c_fixnum(us_length_STR(str, self), self); cnum oalloc = str->st.alloc; cnum alloc = oalloc, delta, needed; @@ -5436,6 +5445,38 @@ val string_extend(val str, val tail, val finish_in) return str; } +val us_string_extend_STR_CHR(val str, val ch, val finish, val self) +{ + cnum len = c_fixnum(us_length_STR(str, self), self); + cnum oalloc = str->st.alloc; + cnum alloc = oalloc, needed; + + if (NUM_MAX - 2 < len) + uw_throwf(alloc_error_s, lit("~a: overflow"), self, nao); + + needed = len + 2; + + if (needed > alloc || finish) { + if (finish) + alloc = needed; + else if (alloc >= (NUM_MAX - NUM_MAX / 3)) + alloc = NUM_MAX; + else + alloc = max(alloc + alloc / 2, needed); + + if (alloc != oalloc) { + str->st.str = chk_wrealloc(str->st.str, alloc); + str->st.alloc = alloc; + } + } + + str->st.len = num_fast(len + 1); + str->st.str[len] = convert(wchar_t, c_n(ch)); + str->st.str[len + 1] = 0; + + return str; +} + val string_finish(val str) { val self = lit("string-finish"); @@ -5528,11 +5569,7 @@ val length_str(val str) lazy_str_force(str); return length_str(str->ls.prefix); case STR: - if (!str->st.len) { - set(mkloc(str->st.len, str), num(wcslen(str->st.str))); - str->st.alloc = c_num(str->st.len, self) + 1; - } - return str->st.len; + return us_length_STR(str, self); default: type_mismatch(lit("length-str: ~s is not a string"), str, nao); } -- cgit v1.2.3