From a3f589640fd443deea230c27efd6bdfc92f5817f Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Wed, 4 Sep 2024 12:38:30 +0200 Subject: [PATCH] Time#strftime: grow the buffer faster Use a classic doubling of capacity rather than only adding twice as much capacity as is already known to be needed. ``` compare-ruby: ruby 3.4.0dev (2024-09-04T09:21:53Z opt-strftime-2 ae98d19cf9) +YJIT [arm64-darwin23] built-ruby: ruby 3.4.0dev (2024-09-04T11:46:02Z opt-strftime-growth 586263d6fb) +YJIT [arm64-darwin23] warming up... | |compare-ruby|built-ruby| |:---------------------------|-----------:|---------:| |time.strftime("%FT%T") | 1.754M| 1.889M| | | -| 1.08x| |time.strftime("%FT%T.%3N") | 1.508M| 1.749M| | | -| 1.16x| |time.strftime("%FT%T.%6N") | 1.488M| 1.756M| | | -| 1.18x| compare-ruby: ruby 3.4.0dev (2024-09-04T09:21:53Z opt-strftime-2 ae98d19cf9) +YJIT [arm64-darwin23] built-ruby: ruby 3.4.0dev (2024-09-04T09:21:53Z opt-strftime-2 ae98d19cf9) +YJIT [arm64-darwin23] warming up... ``` --- benchmark/time_strftime.yml | 7 +++++++ strftime.c | 4 +++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 benchmark/time_strftime.yml 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;