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>
Alexander Gavrilov committed
Sep 18, 2008 at 01:07 UTC
a1c3feb7fac7fe90ff8a05779d7db9cf77e79ae1
1 file changed
+53
-29
lib/encoding.tcl
+53
-29
@@ -286,39 +286,63 @@ set encoding_groups {
286
{"Symbol" Symbol Dingbats MacDingbats MacCentEuro}}
287
}
288
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
+
326
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
333
}
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
339
}
313
- if {$i >= 0} break
314
- }
315
- break
340
}
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
+ }
346
}
347
348
proc force_path_encoding {path enc} {