lseq - duplicative expr eval, bottleneck by usage of operators, inconstistent handling of indices (int vs. double vs. error), etc
lseq 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 incr). 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)
% set i 0; list [lseq {[puts "just a trace, i:$i"; incr i]-1} {[incr i]}] i:$i
just a trace, i:0
- just a trace, i:1
- {0 1 2 3} i:4
+ {0 1 2} i:2
% 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
- just a trace, i:1
- {0.0 1.0 2.0 3.0 4.0} i:4
+ {0.0 1.0 2.0} i:2
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:
% timerate {lseq 1 10}
0.237085 µs/# 3588607 # 4217895 #/sec 850.805 net-ms
% timerate {lseq 1 to 10}
9.000940 µs/# 110589 # 111099 #/sec 995.405 net-ms
% timerate {lseq 1 10 2}
0.248337 µs/# 3458008 # 4026792 #/sec 858.750 net-ms
% timerate {lseq 1 to 10 by 2}
18.5890 µs/# 53678 # 53795.4 #/sec 997.818 net-ms
3. Inconsistent handing by double (round-able to integer doubles sometimes causes integer series, sometimes double series and sometimes, mostly by count, an error 'expected integer but got "3.0"' (for example for lseq 3.0)
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:
% 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
max length of a Tcl list exceeded
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):
% lseq [expr {$i/0}]
divide by zero
% lseq {$i/0}
bad operation "$i/0": must be .., to, count, or by
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.
1043860b5eTicket [f4a4bd7f10] lseq "count" argument also dictates output formatting status still Open with 3 other changes ticket2024-08-30 15:31:54
4 of the issues (1-3 and 6) are fixed now in 8.7/9.0.
Remaining to be fixed are 4 and 5.
Thanks for uncovering these problems, and implementing the fixes. I will investigate/implement #4 & 5. I never considered big nums being used, but they should be supported. As for #3, it was intentional to have the string representation used to determine the number of significant fractional digits. It's the easiest and most straight forward way to write an lseq command and control the resolution of a floating point number sequence. It's how one can intentionally get: lseq 1.0 3.0 1 -> "1.0 2.0 3.0 4.0" versus lseq 1 3 1 -> "1 2 3 4". I'll review the changes in more detail in a few days, when I get the time.
> As for #3, it was intentional to have the string representation used to determine the number of significant fractional digits.
It was clear to me. I only spoke about that it happens inconsistently. As I said sometimes (e. g. in expressions) round-able doubles causes integer series. For instance this one avoided that it'd return original double and therefore the trigger with useDoubles would not be active.
Let alone another corner cases, e. g. for large (or small) doubles like 1e50 the expression floor(d) == d could be theoretically true, but I doubt it'd work in C (in opposite to Tcl) due to integer overflow.
>It's how one can intentionally get: lseq 1.0 3.0 1 -> "1.0 2.0 3.0 4.0" versus lseq 1 3 1 -> "1 2 3 4".
Yes, but not always. Here is an example where it was not so:
% lseq 1.0 3.0 1
1.0 2.0 3.0
% lseq [expr {1.0+0}] [expr {3.0+0}] 1
1.0 2.0 3.0
% lseq {1.0+0} {3.0+0} 1
1 2 3
% lseq {1.1+0} {3.1+0} 1
1.1 2.1 3.1
% lseq [expr {1e50+0}] [expr {1e50+1}]
1e+50
% lseq {1e50+0} {1e50+1}
bad operation "1e50+0": must be .., to, count, or by
This is exactly what one'd call inconsistency. So I fixed it during my review together with other stuff.
[c279bfc1b2b30cc1] fixes #5 (lseq will not swallow expr errors anymore).
[362b0c5c71c2220d] fixes the last issue here, at the moment by throwing a simplest error "integer value too large to represent" (also covered by test lseq-1.16.2) as long as bignum series are not supported completely.
If they get supported once, assignNumber (and test) can be adjusted to newest algorithms.
Thus I'll close the ticket for now as fixed.