Skip to content

Commit a117008

Browse files
KristofferCtecosaur
authored andcommitted
Avoid creating strings for ansi_4bit_color_code
These will just be printed anyway, converting a number to a string to immediately print it is unnecessary. Running a benchmark on a large, heavily annotated string we see a 5% reduction in runtime (13.6ms -> 12.9) and a 1% reduction in allocations (412k -> 408k).
1 parent 6863348 commit a117008

File tree

2 files changed

+21
-19
lines changed

2 files changed

+21
-19
lines changed

src/io.jl

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,18 @@ const ANSI_4BIT_COLORS = Dict{Symbol, Int}(
3232
"""
3333
ansi_4bit_color_code(color::Symbol, background::Bool=false)
3434
35-
Provide the color code (30-37, 40-47, 90-97, 100-107) for `color`, as a string.
35+
Provide the color code (30-37, 40-47, 90-97, 100-107) for `color`, as an integer.
3636
When `background` is set the background variant will be provided, otherwise
3737
the provided code is for setting the foreground color.
3838
"""
3939
function ansi_4bit_color_code(color::Symbol, background::Bool=false)
40-
if haskey(ANSI_4BIT_COLORS, color)
41-
code = ANSI_4BIT_COLORS[color]
40+
code = get(ANSI_4BIT_COLORS, color, nothing)
41+
if code !== nothing
4242
code >= 8 && (code += 52)
4343
background && (code += 10)
44-
string(code + 30)
44+
code + 30
4545
else
46-
ifelse(background, "49", "39")
46+
ifelse(background, 49, 39)
4747
end
4848
end
4949

@@ -123,15 +123,17 @@ function termcolor(io::IO, color::SimpleColor, category::Char)
123123
elseif (fg = get(FACES.current[], color.value, getface()).foreground) != SimpleColor(color.value)
124124
termcolor(io, fg, category)
125125
else
126-
print(io, "\e[",
127-
if category == '3' || category == '4'
128-
ansi_4bit_color_code(color.value, category == '4')
129-
elseif category == '5'
130-
if haskey(ANSI_4BIT_COLORS, color.value)
131-
string("58;5;", ANSI_4BIT_COLORS[color.value])
132-
else "59" end
133-
end,
134-
'm')
126+
print(io, "\e[")
127+
if category == '3' || category == '4'
128+
print(io, ansi_4bit_color_code(color.value, category == '4'))
129+
elseif category == '5'
130+
if haskey(ANSI_4BIT_COLORS, color.value)
131+
print(io, "58;5;", ANSI_4BIT_COLORS[color.value])
132+
else
133+
print(io, "59")
134+
end
135+
end
136+
print(io, "m")
135137
end
136138
end
137139

test/runtests.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,11 +495,11 @@ end
495495

496496
@testset "ANSI encoding" begin
497497
# 4-bit color
498-
@test StyledStrings.ansi_4bit_color_code(:cyan, false) == "36"
499-
@test StyledStrings.ansi_4bit_color_code(:cyan, true) == "46"
500-
@test StyledStrings.ansi_4bit_color_code(:bright_cyan, false) == "96"
501-
@test StyledStrings.ansi_4bit_color_code(:bright_cyan, true) == "106"
502-
@test StyledStrings.ansi_4bit_color_code(:nonexistant) == "39"
498+
@test StyledStrings.ansi_4bit_color_code(:cyan, false) == 36
499+
@test StyledStrings.ansi_4bit_color_code(:cyan, true) == 46
500+
@test StyledStrings.ansi_4bit_color_code(:bright_cyan, false) == 96
501+
@test StyledStrings.ansi_4bit_color_code(:bright_cyan, true) == 106
502+
@test StyledStrings.ansi_4bit_color_code(:nonexistant) == 39
503503
# 8-bit color
504504
@test sprint(StyledStrings.termcolor8bit, (r=0x40, g=0x63, b=0xd8), '3') == "\e[38;5;26m"
505505
@test sprint(StyledStrings.termcolor8bit, (r=0x38, g=0x98, b=0x26), '3') == "\e[38;5;28m"

0 commit comments

Comments
 (0)