|
2008-08-07
| ||
| 11:14 | • Closed ticket [2017946fff]: Tailcalls into non-compiled scripts don't work p... artifact: 9f6f082d88 user: msofer | |
| 08:29 | • Ticket [2017946fff]: 4 changes artifact: 61b2f9b7d3 user: msofer | |
|
2008-07-30
| ||
| 06:44 | • Ticket [2017946fff]: 1 change artifact: d59d240c09 user: msofer | |
|
2008-07-21
| ||
| 11:23 | • Ticket [2017946fff]: 1 change artifact: 8c9522a37c user: msofer | |
| 11:19 | • Ticket [2017946fff]: 4 changes artifact: 6f64de1fdf user: msofer | |
|
2008-07-14
| ||
| 21:40 | • Ticket [2017946fff]: 1 change artifact: a3c6013d59 user: tallniel | |
| 21:40 | • Ticket [2017946fff]: 4 changes artifact: 174a114ff2 user: tallniel | |
| 12:53 | • New ticket [2017946fff]. artifact: 4b330bae9a user: tallniel | |
Tailcalls into non-compiled scripts don't work properly
Closed / Fixed2017946fffffffffffffffffffffffffffffffffCreated 18 years agoUpdated 18 years ago
Descriptionplain2008-07-14 12:53:33
A simple tail-recursive factorial function using NRE bombs out at around N=10,000 due to out of memory panic in Tcl bignum code. Equivalent iterative version completes fine on same input. It seems like the C stack is growing despite NRE, and locals aren't being reclaimed? Is this [if] causing the tailcall to not work? Test proc:
proc fac-tr {n {k 1}} {
if {$n <= 1} {
return $k
} else {
tailcall fac-tr [- $n 1] [* $n $k]
}
}
fac-tr 10000
(abort reproducible on Windows XP, with 4GB RAM). Stack dump at abort:
tclsh86sg.exe!_chkstk() Line 91Asm
tclsh86sg.exe!_NMSG_WRITE(int rterrnum=10) Line 194 + 0x18C
tclsh86sg.exe!abort() Line 44 + 0x7C
tclsh86sg.exe!Tcl_PanicVA(const char * format=0x005603e8, char * argList=0x00033538) Line 104C
tclsh86sg.exe!Tcl_Panic(const char * format=0x005603e8, ...) Line 131 + 0xdC
tclsh86sg.exe!Tcl_Realloc(char * ptr=0x148e7fe8, unsigned int size=12944) Line 1108 + 0xeC
tclsh86sg.exe!TclBN_mp_grow(mp_int * a=0x000340b4, int size=3236) Line 35 + 0x13C
tclsh86sg.exe!TclBN_mp_copy(mp_int * a=0x00033584, mp_int * b=0x000340b4) Line 31 + 0xfC
tclsh86sg.exe!TclBN_mp_init_copy(mp_int * a=0x000340b4, mp_int * b=0x00033584) Line 26 + 0xdC
tclsh86sg.exe!GetBignumFromObj(Tcl_Interp * interp=0x00000000, Tcl_Obj * objPtr=0x14f92310, int copy=0, mp_int * bignumValue=0x000340b4) Line 2797 + 0xdC
tclsh86sg.exe!Tcl_TakeBignumFromObj(Tcl_Interp * interp=0x00000000, Tcl_Obj * objPtr=0x14f92310, mp_int * bignumValue=0x000340b4) Line 2902 + 0x13C
tclsh86sg.exe!TclExecuteByteCode(Tcl_Interp * interp=0x00afa8a8, ByteCode * codePtr=0x00bb7fb8) Line 6428 + 0x15C
tclsh86sg.exe!Tcl_EvalObjv(Tcl_Interp * interp=0x00afa8a8, int objc=3, Tcl_Obj * const * objv=0x14cb1fe0, int flags=524288) Line 4116 + 0x10C
tclsh86sg.exe!TailcallFromTebc(void * * data=0x14f922cc, Tcl_Interp * interp=0x00afa8a8, int result=0) Line 7838 + 0x16C
>tclsh86sg.exe!TclEvalObjv_NR2(Tcl_Interp * interp=0x00afa8a8, int result=0, TEOV_record * rootPtr=0x06a2ea10) Line 4238 + 0x16C
tclsh86sg.exe!Tcl_EvalObjv(Tcl_Interp * interp=0x00afa8a8, int objc=3, Tcl_Obj * const * objv=0x14cb1f98, int flags=524288) Line 4207 + 0x11C
tclsh86sg.exe!TailcallFromTebc(void * * data=0x14f92344, Tcl_Interp * interp=0x00afa8a8, int result=0) Line 7838 + 0x16C
tclsh86sg.exe!TclEvalObjv_NR2(Tcl_Interp * interp=0x00afa8a8, int result=0, TEOV_record * rootPtr=0x06a2ea10) Line 4238 + 0x16C
tclsh86sg.exe!Tcl_EvalObjv(Tcl_Interp * interp=0x00afa8a8, int objc=3, Tcl_Obj * const * objv=0x14cb1f50, int flags=524288) Line 4207 + 0x11C
tclsh86sg.exe!TailcallFromTebc(void * * data=0x14f923bc, Tcl_Interp * interp=0x00afa8a8, int result=0) Line 7838 + 0x16C
... stack then has several hundred repeats of these TailcallFromTebc/Tcl_EvalObjv combinations....
I thought this might be the [if] preventing the tailcall reclaiming the stack, but I get the same abort with the simpler:
proc fac-tr {n {k 1}} {
if {$n <= 1} { return $k }
tailcall fac-tr [- $n 1] [* $n $k]
}
tallnielplain2008-07-14 21:40:18
Logged In: YES
user_id=102050
Originator: YES
The bug seems to disappear when fac-tr is itself called from a byte-compiled
context (e.g. a proc or loop body), e.g.:
proc foo {} { fac-tr 10000 }
# or
foreach _ _ { fac-tr 10000 }
This completes fine, apparently consuming constant stack. It is only when
fac-tr is called from a non-byte-compiled context that [tailcall] seems to
not work as expected (C stack grows as pre-NRE).
tallniel Ticket [2017946fff]: 1 change
[History diff]2008-07-14 21:40:37
msoferplain2008-07-21 11:19:19
Logged In: YES user_id=148712 Originator: NO That should have errored with "too many nested evaluations (infinite loop?)"! Did you up the recursionLimit too? The problem is that tailcalls are not (yet) allowed to fall back from bytecode to interpreted code.
msofer Ticket [2017946fff]: 1 change
[History diff]2008-07-21 11:23:46
msofer Ticket [2017946fff]: 1 change
[History diff]2008-07-30 06:44:38
msoferplain2008-08-07 08:29:48
Logged In: YES user_id=148712 Originator: NO Indeed, Tcl_EvalEx is not (yet?) nre-enabled. Any command that bytecompiles this ([time], [if 1], [uplevel 0], [namespace eval ::]) will finish the job in less than 2 secs here (if you do not attempt to print out the result to the terminal, that takes longer)
msoferplain2008-08-07 11:14:51
Logged In: YES user_id=148712 Originator: NO The issue was not Tcl_EvalEx but rather tailcalls falling out of TEBC into Tcl_EvalEx. Fixed in HEAD.
Log in (anonymous is fine) to add a comment.