Skip to content
Tcl Source Code History Of Ticket 2017946
Login

History Of Ticket 2017946

Artifacts Associated With Ticket 2017946

  1. Ticket change [4b330bae9a] (rid 126658) by tallniel on 2008-07-14 12:53:33:

    1. assignee initialized to: "msofer"
    2. cmimetype initialized to: "text/plain"
    3. comment initialized to:
      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]
       }
      
      
      
    4. foundin initialized to: "obsolete: 8.6a1"
    5. is_private initialized to: "0"
    6. priority initialized to: "5 Medium"
    7. resolution initialized to: "None"
    8. status initialized to: "Open"
    9. submitter initialized to: "tallniel"
    10. subsystem initialized to: "45. Parsing and Eval"
    11. title initialized to: "Mem leak in NRE/lack of trampoline"
    12. type initialized to: "Bug"
  2. Ticket change [174a114ff2] (rid 122463) by tallniel on 2008-07-14 21:40:18:

    1. icomment:
      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).
      
      
    2. login: "tallniel"
    3. mimetype: "text/plain"
    4. username: "tallniel"
  3. Ticket change [a3c6013d59] (rid 110096) by tallniel on 2008-07-14 21:40:37:

    1. title changed to: "Mem leak in [tailcall]/lack of trampoline"
  4. Ticket change [6f64de1fdf] (rid 100310) by msofer on 2008-07-21 11:19:19:

    1. icomment:
      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.
      
      
    2. login: "msofer"
    3. mimetype: "text/plain"
    4. username: "msofer"
  5. Ticket change [8c9522a37c] (rid 127766) by msofer on 2008-07-21 11:23:46:

    1. title changed to:
      Tailcalls into non-compiled scripts don't work properly
      
  6. Ticket change [d59d240c09] (rid 123652) by msofer on 2008-07-30 06:44:38:

    1. priority changed to: "6"
  7. Ticket change [61b2f9b7d3] (rid 92285) by msofer on 2008-08-07 08:29:48:

    1. icomment:
      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)
      
      
      
    2. login: "msofer"
    3. mimetype: "text/plain"
    4. username: "msofer"
  8. Ticket change [9f6f082d88] (rid 96643) by msofer on 2008-08-07 11:14:51:

    1. closedate changed to: "2008-08-07T04:14:51"
    2. closer changed to: "msofer"
    3. icomment:
      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.
      
      
    4. login: "msofer"
    5. mimetype: "text/plain"
    6. resolution changed to: "Fixed"
    7. status changed to: "Closed"
    8. username: "msofer"