Skip to content

Commit a1c3feb

Browse files
angavrilovspearce
authored andcommitted
git-gui: Optimize encoding name resolution using a lookup table.
Encoding menu construction does almost a hundred of encoding resolutions, which with the old implementation led to a small but noticeable delay. Signed-off-by: Alexander Gavrilov <angavrilov@gmail.com> Tested-by: Johannes Sixt <johannes.sixt@telecom.at> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 parent 3fe0162 commit a1c3feb

File tree

1 file changed

+53
-29
lines changed

1 file changed

+53
-29
lines changed

lib/encoding.tcl

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -286,39 +286,63 @@ set encoding_groups {
286286
{"Symbol" Symbol Dingbats MacDingbats MacCentEuro}}
287287
}
288288

289+
proc build_encoding_table {} {
290+
global encoding_aliases encoding_lookup_table
291+
292+
# Prepare the lookup list; cannot use lsort -nocase because
293+
# of compatibility issues with older Tcl (e.g. in msysgit)
294+
set names [list]
295+
foreach item [encoding names] {
296+
lappend names [list [string tolower $item] $item]
297+
}
298+
set names [lsort -ascii -index 0 $names]
299+
# neither can we use lsearch -index
300+
set lnames [list]
301+
foreach item $names {
302+
lappend lnames [lindex $item 0]
303+
}
304+
305+
foreach grp $encoding_aliases {
306+
set target {}
307+
foreach item $grp {
308+
set i [lsearch -sorted -ascii $lnames \
309+
[string tolower $item]]
310+
if {$i >= 0} {
311+
set target [lindex $names $i 1]
312+
break
313+
}
314+
}
315+
if {$target eq {}} continue
316+
foreach item $grp {
317+
set encoding_lookup_table([string tolower $item]) $target
318+
}
319+
}
320+
321+
foreach item $names {
322+
set encoding_lookup_table([lindex $item 0]) [lindex $item 1]
323+
}
324+
}
325+
289326
proc tcl_encoding {enc} {
290-
global encoding_aliases
291-
set names [encoding names]
292-
set lcnames [string tolower $names]
293-
set enc [string tolower $enc]
294-
set i [lsearch -exact $lcnames $enc]
295-
if {$i < 0} {
296-
# look for "isonnn" instead of "iso-nnn" or "iso_nnn"
297-
if {[regsub {^(iso|cp|ibm|jis)[-_]} $enc {\1} encx]} {
298-
set i [lsearch -exact $lcnames $encx]
327+
global encoding_lookup_table
328+
if {$enc eq {}} {
329+
return {}
330+
}
331+
if {![info exists encoding_lookup_table]} {
332+
build_encoding_table
299333
}
300-
}
301-
if {$i < 0} {
302-
foreach l $encoding_aliases {
303-
set ll [string tolower $l]
304-
if {[lsearch -exact $ll $enc] < 0} continue
305-
# look through the aliases for one that tcl knows about
306-
foreach e $ll {
307-
set i [lsearch -exact $lcnames $e]
308-
if {$i < 0} {
309-
if {[regsub {^(iso|cp|ibm|jis)[-_]} $e {\1} ex]} {
310-
set i [lsearch -exact $lcnames $ex]
311-
}
334+
set enc [string tolower $enc]
335+
if {![info exists encoding_lookup_table($enc)]} {
336+
# look for "isonnn" instead of "iso-nnn" or "iso_nnn"
337+
if {[regsub {^(iso|cp|ibm|jis)[-_]} $enc {\1} encx]} {
338+
set enc $encx
312339
}
313-
if {$i >= 0} break
314-
}
315-
break
316340
}
317-
}
318-
if {$i >= 0} {
319-
return [lindex $names $i]
320-
}
321-
return {}
341+
if {[info exists encoding_lookup_table($enc)]} {
342+
return $encoding_lookup_table($enc)
343+
} else {
344+
return {}
345+
}
322346
}
323347

324348
proc force_path_encoding {path enc} {

0 commit comments

Comments
 (0)