From 9c4e70ed01f3fa6992f785c9ce5afdb281b99bc9 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Wed, 6 Aug 2025 09:07:45 +0900 Subject: [PATCH] time.c: optimize getutc and getlocal to skip unnecessary datetime updates skip time_update_datetime() call when timezone conversion is not needed, eliminating expensive gmtime_r/localtime_r system calls for redundant conversions like time.utc.getutc or time.local.getlocal. Co-authored-by: Claude --- mrbgems/mruby-time/src/time.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mrbgems/mruby-time/src/time.c b/mrbgems/mruby-time/src/time.c index 59c9f4cee..f0eaa53f9 100644 --- a/mrbgems/mruby-time/src/time.c +++ b/mrbgems/mruby-time/src/time.c @@ -1107,8 +1107,10 @@ time_getutc(mrb_state *mrb, mrb_value self) struct mrb_time *tm = time_get_ptr(mrb, self); struct mrb_time *tm2 = (struct mrb_time*)mrb_malloc(mrb, sizeof(*tm)); *tm2 = *tm; - tm2->timezone = MRB_TIMEZONE_UTC; - time_update_datetime(mrb, tm2, TRUE); + if (tm2->timezone != MRB_TIMEZONE_UTC) { + tm2->timezone = MRB_TIMEZONE_UTC; + time_update_datetime(mrb, tm2, TRUE); + } return time_wrap(mrb, mrb_obj_class(mrb, self), tm2); } @@ -1129,8 +1131,10 @@ time_getlocal(mrb_state *mrb, mrb_value self) struct mrb_time *tm = time_get_ptr(mrb, self); struct mrb_time *tm2 = (struct mrb_time*)mrb_malloc(mrb, sizeof(*tm)); *tm2 = *tm; - tm2->timezone = MRB_TIMEZONE_LOCAL; - time_update_datetime(mrb, tm2, TRUE); + if (tm2->timezone != MRB_TIMEZONE_LOCAL) { + tm2->timezone = MRB_TIMEZONE_LOCAL; + time_update_datetime(mrb, tm2, TRUE); + } return time_wrap(mrb, mrb_obj_class(mrb, self), tm2); }