Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2001-11-21
00:34 • Closed ticket [466037ffff]: Optimise getting the head/tail of a list plus 6 ... artifact: 7475c51a20 user: kennykb
2001-10-03
22:28 • Ticket [466037ffff]: 5 changes artifact: f550fecb92 user: kennykb
2001-10-01
19:13 • Ticket [466037ffff]: 4 changes artifact: d27780674c user: msofer
2001-09-29
06:40 • Ticket [466037ffff]: 4 changes artifact: 03dbfdc4de user: andreas_kupries
05:45 • Ticket [466037ffff]: 4 changes artifact: 1b5a67c1da user: hobbs
05:27 • Ticket [466037ffff]: 4 changes artifact: 8e533293e9 user: andreas_kupries
02:01 • Ticket [466037ffff]: 4 changes artifact: 8877e28507 user: msofer
01:57 • Ticket [466037ffff]: 4 changes artifact: 2110133ec6 user: mdejong
01:51 • Ticket [466037ffff]: 4 changes artifact: 91366f3d7f user: hobbs
2001-09-28
21:51 • Ticket [466037ffff]: 4 changes artifact: d7ec75f743 user: msofer
13:35 • New ticket [466037ffff]. artifact: c12620ff6b user: msofer

Optimise getting the head/tail of a list

Closed466037ffffffffffffffffffffffffffffffffffCreated 25 years agoUpdated 25 years ago
Descriptionplain2001-09-28 13:35:33
Let the compiler recognise the idioms
  [lindex $list 0] ;# get the head of $list
and
  [lrange $list 1 end] ;# get the tail of $list
and compile-in optimised code for them.

The idea is to make the list operations usual in
functional programming very efficient. This follows a
discussion started in c.l.t. by Acacio Cruz - the
thread does not yet show up in google, I will add the
reference as soon as available.
msoferplain2001-09-28 21:51:27
Logged In: YES 
user_id=148712

The clt thread is at
http://groups.google.com/groups?hl=en&threadm=3BB3F8D8.51932A75%40eur.autodesk.com&prev=/groups%3Foi%3Ddjq%26as_ugroup%3Dcomp.lang.tcl
hobbsplain2001-09-29 01:51:47
Logged In: YES 
user_id=72656

Note that lindex is already about as optimized as can be in 
the byte-code compiler.  lrange could be worked on.
mdejongplain2001-09-29 01:57:17
Logged In: YES 
user_id=90858

Is supporting lisp like recursion really that
important? It seems like the foreach command
is far better than list like recursion any
day of the week.
msoferplain2001-09-29 02:01:54
Logged In: YES 
user_id=148712

The idea was to recognize at compile time that the index to
[lindex] is an integer, so that the call to
TclGetIntForIndex can be avoided at runtime. In this sense,
head can be improved - I do not really know how much of a
difference it will make.
andreas_kupriesplain2001-09-29 05:27:31
Logged In: YES 
user_id=75003

Note that optimizing 'end', 'end-1' is harder than 
for '0', '1', etc. The index cannot be transmitted to the 
lindex bytecode as a simple number (behaviour for negative 
numbers is defined, return the empty string). This requires 
an additional 'index' objtype which can represent static 
indices from the end.
hobbsplain2001-09-29 05:45:59
Logged In: YES 
user_id=72656

Sure it can - just pass a negative number.  If you are 
handling immediates, you get to define your own syntax, and 
allow negative numbers.  This would have been easier all 
along (instead of the end-x), but people perceived that to 
be a compatability problem.
andreas_kupriesplain2001-09-29 06:40:37
Logged In: YES 
user_id=75003

So [lindex $foo -1] just pushes an empty object,
[lindex $foo end-1] passes index '-1' to the bytecode
and [lindex $foo $idx] compiles to something which goes 
through TclGetForIndex ? Could work.
msoferplain2001-10-01 19:13:01
Logged In: YES 
user_id=148712

A  small change to Andreas' description, to allow the
representation of "end" ('-0'?): let '-1' stand for 'end',
so that the previous description becomes:

So [lindex $foo -1] just pushes an empty object, [lindex
$foo end-1] passes index '-2' to the bytecode and [lindex
$foo $idx] compiles to something which goes through
TclGetForIndex ? Could work.
kennykbplain2001-10-03 22:28:56
Logged In: YES 
user_id=99768

The changes I'm working on for TIP #22 already have a Tcl_ObjType for 'end-' indices.  Note that 
Tcl_GetIntForIndex should be extremely fast if the index is constant because the first couple of lines of 
code check for 'int' and 'index' objtypes, and the string rep is examined only if both those checks fail.

We may at some point want to revisit what happens for [lindex $list end-$i], but that's farther out.

For what it's worth, what's compiled for [lindex $list $i], with $list and $i both local, is

    INST_LOAD_SCALAR1 <var # for list>
    INST_LOAD_SCALAR1 <var # for i>
    INST_LIST_INDEX

For [lindex $list 0], it'll be

    INST_LOAD_SCALAR1 <var # for list>
    INST_PUSH1 <literal # for 0>
    INST_LIST_LINDEX

INST_LIST_LINDEX does indeed call TclGetIntForIndex, but that's been changed to begin with looking for 
tclIntType and tclEndOffsetType and not do any parsing if the object is already a known type.

Let's see how that does on the benchmarks before we try to squeeze any more out of it.
kennykbplain2001-11-21 00:34:45
Logged In: YES 
user_id=99768

Just spoke with the originator, and he says that "he requested feature has been provided."
Log in (anonymous is fine) to add a comment.