From c11a67a818c9fc2de47a812a39761de5be48ccb2 Mon Sep 17 00:00:00 2001 From: Turiiya Date: Sun, 17 Nov 2024 19:31:44 +0100 Subject: [PATCH 1/2] term: improve performance by caching redundant can_show_color calls --- vlib/term/term.v | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/vlib/term/term.v b/vlib/term/term.v index 75d67b33900e36..ac7fac829acbc8 100644 --- a/vlib/term/term.v +++ b/vlib/term/term.v @@ -1,3 +1,4 @@ +@[has_globals] module term import os @@ -13,16 +14,33 @@ pub mut: y int } +__global can_show_color_on_stdout_cache = init_can_show_color_cache() +__global can_show_color_on_stderr_cache = init_can_show_color_cache() + +fn init_can_show_color_cache() ?bool { + return none +} + // can_show_color_on_stdout returns true if colors are allowed in stdout; // returns false otherwise. pub fn can_show_color_on_stdout() bool { - return supports_escape_sequences(1) + if status := can_show_color_on_stdout_cache { + return status + } + status := supports_escape_sequences(1) + can_show_color_on_stdout_cache = status + return status } // can_show_color_on_stderr returns true if colors are allowed in stderr; // returns false otherwise. pub fn can_show_color_on_stderr() bool { - return supports_escape_sequences(2) + if status := can_show_color_on_stderr_cache { + return status + } + status := supports_escape_sequences(2) + can_show_color_on_stderr_cache = status + return status } // failed returns a bold white on red version of the string `s` From 1326911f58fd344595939dd2b6518a8df2eab8eb Mon Sep 17 00:00:00 2001 From: Turiiya Date: Sun, 17 Nov 2024 21:18:54 +0100 Subject: [PATCH 2/2] apply suggestion --- vlib/term/term.v | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/vlib/term/term.v b/vlib/term/term.v index ac7fac829acbc8..551a3f52253665 100644 --- a/vlib/term/term.v +++ b/vlib/term/term.v @@ -14,12 +14,8 @@ pub mut: y int } -__global can_show_color_on_stdout_cache = init_can_show_color_cache() -__global can_show_color_on_stderr_cache = init_can_show_color_cache() - -fn init_can_show_color_cache() ?bool { - return none -} +__global can_show_color_on_stdout_cache = ?bool(none) +__global can_show_color_on_stderr_cache = ?bool(none) // can_show_color_on_stdout returns true if colors are allowed in stdout; // returns false otherwise.