Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: d1ce0f27831e00fd7f781f04245a51ff10333c591c4285484a0aa7651b0e61ab
Ticket: f05f5ef759c1f7f93903fd873250a4c76147c4b2
lseq - duplicative expr eval, bottleneck by usage of operators, inconstistent handling of indices (int vs. double vs. error), etc
User & Date: sebres on 2024-06-11 23:59:30
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    In an attempt to fix the bottleneck, I found that command <code>lseq</code> is a bit raw and has several bugs and inconsistencies.
    
    1. Duplicative evaluation of expressions, what not only inefficient, but also buggy and hardly deterministic (if someone makes storable operations inside, like <code>incr</code>). So every expression will be invoked twice, however, still worse, the value may be thereby from the first or the second eval depending on its result type (by int wins 1st eval, by double wins the 2nd eval).
    For example (red - current state, green - expected)
    <code><pre style="padding-left:2rem;">
      % set i 0; list [lseq {[puts "just a trace, i:$i"; incr i]-1} {[incr i]}] i:$i
      just a trace, i:0
    <b style="color:red;">- just a trace, i:1</b>
    <b style="color:red;">- {0 1 2 3} i:4</b>
    <b style="color:green;">+ {0 1 2} i:2</b>
    &nbsp;
      % set i 0; list [lseq {[puts "just a trace, i:$i"; incr i]-1} {[incr i]+0.1}] i:$i
      just a trace, i:0
    <b style="color:red;">- just a trace, i:1</b>
    <b style="color:red;">- {0.0 1.0 2.0 3.0 4.0} i:4</b>
    <b style="color:green;">+ {0.0 1.0 2.0} i:2</b>
    </pre></code>
    
    2. Usage of lseq-operators has a bottleneck by saving the result and attempt to eval it as expression (due to wrong order of checks). What causes almost 40x  -50x growth of the execution time per operator. For example:
    <code><pre style="padding-left:2rem;">
    % timerate {lseq 1 10}
    <span style="color:green;">0.237085 µs/# 3588607 # 4217895 #/sec 850.805 net-ms</span>
    % timerate {lseq 1 to 10}
    <span style="color:red;">9.000940 µs/# 110589 # 111099 #/sec 995.405 net-ms</span>
    % timerate {lseq 1 10 2}
    <span style="color:green;">0.248337 µs/# 3458008 # 4026792 #/sec 858.750 net-ms</span>
    % timerate {lseq 1 to 10 by 2}
    <span style="color:red;">18.5890 µs/# 53678 # 53795.4 #/sec 997.818 net-ms</span>
    </pre></code>
    
    3. Inconsistent handing by double (round-able to integer doubles sometimes causes integer series, sometimes double series and sometimes, mostly by count, an error '<code>expected integer but got "3.0"</code>' (for example for <code>lseq 3.0</code>)
    
    4. A series with few elements but big-integers can cause a max list length error. Either it'd need different error message, or it must be fixed for big-ints (support them). For example 3 elements series:
    <code><pre style="padding-left:2rem;">
    % lseq 0x[string repeat f 14]0 .. 0x[string repeat f 14]2
    1152921504606846960 1152921504606846961 1152921504606846962
    % lseq 0x[string repeat f 15]0 .. 0x[string repeat f 15]2
    <span style="color:red;">max length of a Tcl list exceeded</span>
    </pre></code>
    
    5. If lseq supports expressions, it must do that properly. For instance I expect the same error here (and definitely not "bad operation" in the 2nd case):
    <code><pre style="padding-left:2rem;">
    % lseq [expr {$i/0}]
    divide by zero
    % lseq {$i/0}
    <span style="color:red;">bad operation "$i/0": must be .., to, count, or by</span>
    </pre></code>
    
    6. During my performance tests I saw some leaks... no idea exactly where and don't intend to invest the time here, because as I was ready with my code review I cannot reproduce the leaks anymore.
    
  5. foundin changed to: "8.7+"
  6. is_private changed to: "0"
  7. login: "sebres"
  8. priority changed to: "5 Medium"
  9. private_contact changed to: "50e9528765c18296566374b3fde2df942c394efd"
  10. resolution changed to: "None"
  11. severity changed to: "Minor"
  12. status changed to: "Open"
  13. submitter changed to: "sebres"
  14. subsystem changed to: "17. Commands I-L"
  15. title changed to:
    lseq - duplicative expr eval, bottleneck by usage of operators, inconstistent handling of indices (int vs. double vs. error), etc
    
  16. type changed to: "Bug"