Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: a30bf24dc7c1b43d52138a347675198f4a1851939927a166fa1796ff34439389
Ticket: db36fa5122259be74d0ccadd102e076094703052
compiled lindex botches index artihmetic
User & Date: sebres on 2018-03-07 16:14:02
Changes

  1. icomment:
    > [lreplace] is a complete abomination.
    
    Yeap, IMHO it should rather get own instruction.
    For example considering the performance of resulting construct for the
    byte-code, it is hardly faster as direct invocation (by special cases like remove) and 
    even slower! as not compiled invoke-variant (by replace).
    
    ## remove:
    
    % tcl::unsupported::disassemble script {lreplace a 1 2}
    ByteCode ...
      Source "lreplace a 1 2"
      Cmds 1, src 14, inst 52, litObjs 4, aux 0, stkDepth 3, code/src 0.00
      Commands 1:
          1: pc 0-50, src 0-13
      Command 1: "lreplace a 1 2"
        (0) push1 0         # "a"
        (2) dup
        (3) listLength
        (4) push1 1         # "1"
        (6) ge
        (7) jumpTrue1 +19   # pc 26
        (9) dup
        (10) listLength
        (11) jumpFalse1 +15         # pc 26
        (13) push1 2        # "list doesn't contain element 1"
        (15) push1 3        # "-errorcode {TCL OPERATION LREPLACE BADID..."
        (17) returnImm +1 0
        (26) dup
        (27) listRangeImm 0 0
        (36) reverse 2
        (41) listRangeImm 3 end
        (50) listConcat
        (51) done
    
    % tcl::unsupported::disassemble script {lreplace a end+1 end+2}
    ByteCode ...
      Source "lreplace a end+1 end+2"
      Cmds 1, src 22, inst 11, litObjs 4, aux 0, stkDepth 4, code/src 0.00
      Commands 1:
          1: pc 0-9, src 0-21
      Command 1: "lreplace a end+1 end+2"
        (0) push1 0         # "lreplace"
        (2) push1 1         # "a"
        (4) push1 2         # "end+1"
        (6) push1 3         # "end+2"
        (8) invokeStk1 4
        (10) done
    
    
    % timerate {lreplace a 1 2}; # compiled
    0.176311 µs/# 4526429 # 5671797 #/sec 798.059 nett-ms
    % timerate {lreplace a end+1 end+2}; # invoke
    0.205086 µs/# 4004816 # 4876013 #/sec 821.330 nett-ms
    
    ## replace:
    
    % tcl::unsupported::disassemble script {lreplace a 1 2 x}
    ByteCode ...
      Source "lreplace a 1 2 x"
      Cmds 1, src 16, inst 75, litObjs 5, aux 0, stkDepth 4, code/src 0.00
      Commands 1:
          1: pc 0-73, src 0-15
      Command 1: "lreplace a 1 2 x"
        (0) push1 0         # "a"
        (2) push1 1         # "x"
        (4) list 1
        (9) reverse 2
        (14) dup
        (15) listLength
        (16) push1 2        # "1"
        (18) ge
        (19) jumpTrue1 +19  # pc 38
        (21) dup
        (22) listLength
        (23) jumpFalse1 +15         # pc 38
        (25) push1 3        # "list doesn't contain element 1"
        (27) push1 4        # "-errorcode {TCL OPERATION LREPLACE BADID..."
        (29) returnImm +1 0
        (38) dup
        (39) listRangeImm 0 0
        (48) reverse 2
        (53) listRangeImm 3 end
        (62) reverse 3
        (67) listConcat
        (68) reverse 2
        (73) listConcat
        (74) done
    
    % tcl::unsupported::disassemble script {lreplace a end+1 end+2 x}
    ByteCode ...
      Source "lreplace a end+1 end+2 x"
      Cmds 1, src 24, inst 13, litObjs 5, aux 0, stkDepth 5, code/src 0.00
      Commands 1:
          1: pc 0-11, src 0-23
      Command 1: "lreplace a end+1 end+2 x"
        (0) push1 0         # "lreplace"
        (2) push1 1         # "a"
        (4) push1 2         # "end+1"
        (6) push1 3         # "end+2"
        (8) push1 4         # "x"
        (10) invokeStk1 5
        (12) done
    
    
    % timerate {lreplace a 1 2 x}; # compiled
    0.303333 µs/# 2874003 # 3296706 #/sec 871.780 nett-ms
    % timerate {lreplace a end+1 end+2 x}; # invoke
    0.223937 µs/# 3723690 # 4465541 #/sec 833.872 nett-ms
    
    
    Note that by invoke it does additionally NRE-, stack to local normalization (decache stack info, etc),
    TEBC_YIELD, then exits from TEBCResume.
    
    Let alone the code complexity, the growth of the byte-code size, CPU-cache washouts, etc.
    It is very questionable why such constructs should be implemented with tcl byte-code instead of pure C.
    
    
    > I see [lsort] uses a parallel scheme of encoding index values as integers.
    
    Yes, as you possibly saw, I have already added a test cases for it. 
    Unfortunately, it affects basically many other things, that using TclGetIntForIndexM (TclGetIntForIndex) 
    with fixed (supplied before known) value of `endValue`,
    like TCL_INDEX_END, SORTIDX_END, etc.
    
  2. login: "sebres"
  3. mimetype: "text/plain"