Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

term: improve performance by caching redundant can_show_color calls #22893

Merged
merged 2 commits into from
Nov 18, 2024

Conversation

ttytm
Copy link
Member

@ttytm ttytm commented Nov 17, 2024

When using the term module for CLI projects, functions like colorize and other functions that check first for the ability to display colors can be called quite a lot. Currently the value of can_show_color_on_stdout()->supports_escape_sequences() is re-processed on every call of these functions.

v/vlib/term/term.v

Lines 184 to 202 in 5bba92a

@[manualfree]
fn supports_escape_sequences(fd int) bool {
vcolors_override := os.getenv('VCOLORS')
defer {
unsafe { vcolors_override.free() }
}
if vcolors_override == 'always' {
return true
}
if vcolors_override == 'never' {
return false
}
env_term := os.getenv('TERM')
defer {
unsafe { env_term.free() }
}
if env_term == 'dumb' {
return false
}

The changes add a cache for the can_show_color status which results in a speedup of 100%+.

import term
import time

sw := time.new_stopwatch()

for _ in 0 .. 10_000 {
	term.ok_message('my message')
	term.colorize(term.red, 'red')
}

println(sw.elapsed())

Huly®: V_0.6-21336

vlib/term/term.v Outdated Show resolved Hide resolved
Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman spytheman merged commit 5e9e09f into vlang:master Nov 18, 2024
62 checks passed
@ttytm ttytm deleted the term/improve-perf branch November 18, 2024 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants