diff --git a/benchmark/time_strftime.yml b/benchmark/time_strftime.yml new file mode 100644 index 0000000000..28f62aec87 --- /dev/null +++ b/benchmark/time_strftime.yml @@ -0,0 +1,7 @@ +prelude: | + # frozen_string_literal: true + time = Time.now +benchmark: + - time.strftime("%FT%T") # 19B + - time.strftime("%FT%T.%3N") # 23B + - time.strftime("%FT%T.%6N") # 26B diff --git a/strftime.c b/strftime.c index 33e7d3fdb8..906e8360a9 100644 --- a/strftime.c +++ b/strftime.c @@ -171,7 +171,9 @@ resize_buffer(VALUE ftime, char *s, const char **start, const char **endp, ptrdiff_t n, size_t maxsize) { size_t len = s - *start; - size_t nlen = len + n * 2; + size_t need = len + n * 2; + size_t nlen = rb_str_capacity(ftime); + while (nlen < need) nlen <<= 1; if (nlen < len || nlen > maxsize) { return 0;