| Ticket UUID: | 466037 | ||
| Title: | Optimise getting the head/tail of a list | ||
| Type: | RFE | Created on: | 2001-09-28 13:35:33 |
| Submitter: | msofer | Assigned to: | kennykb |
| Subsystem: | 47. Bytecode Compiler | Severity: | |
| Priority: | 5 Medium | Last modified: | 2001-11-21 00:34:45 |
| Status: | Closed | Closed by: | kennykb |
| Resolution: | None | Closed on: | 2001-11-20 17:34:45 |
| Version: | None | ||
| Description: | ||||
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. | ||||
| User Comments: | ||||
msofer added on 2001-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 hobbs added on 2001-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. mdejong added on 2001-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. msofer added on 2001-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_kupries added on 2001-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. hobbs added on 2001-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_kupries added on 2001-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. msofer added on 2001-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.
kennykb added on 2001-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.
kennykb added on 2001-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." | ||||
