Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2024-08-30
15:31 • Ticket [f4a4bd7f10] lseq "count" argument also dictates output formatt... artifact: 1043860b5e user: sebres
2024-08-29
14:04
Merge [30f3c945a69e5c83]: Ultimately closes [f05f5ef759c1f7f9]: since bigints are not supported yet... check-in: 826742d69f user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
12:40
Merge [bb7ef31f683f8a10]: "more fixes for [f05f5ef759c1f7f9]: don't swallow expr errors, further cod... check-in: 1fb8568aba user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
12:12
Merge [433888e35e85c724]: lseq: code review, several fixes for [f05f5ef759c1f7f9], unneeded code r... check-in: 8ddee847cf user: pooryorick tags: unchained, INCOMPATIBLE_LICENSE
2024-06-12
20:21 • Closed ticket [f05f5ef759]: lseq - duplicative expr eval, bottleneck by usage of... artifact: 8f141c10b2 user: sebres
19:54
ultimately closes [f05f5ef759c1f7f9]: since bigints are not supported yet, trying to use them as ser... check-in: 362b0c5c71 user: sebres tags: core-8-branch
19:04 • Ticket [f05f5ef759] lseq - duplicative expr eval, bottleneck by usage of operato... artifact: 720563c12f user: sebres
19:01
more fixes for [f05f5ef759c1f7f9]: don't swallow expr errors, further code review and simplification check-in: c279bfc1b2 user: sebres tags: core-8-branch
11:50 • Ticket [f05f5ef759] lseq - duplicative expr eval, bottleneck by usage of operato... artifact: f23f179884 user: sebres
05:09 • Ticket [f05f5ef759]: 3 changes artifact: cb865b8ea5 user: griffin
00:53
list.perf.tcl: added performance regression tests for lseq (illustrates fix for [f05f5ef759c1f7f9], ... check-in: 094f5d892f user: sebres tags: core-8-branch
00:22 • Ticket [f05f5ef759] lseq - duplicative expr eval, bottleneck by usage of operato... artifact: c8e70f8fcd user: sebres
00:17
merge 8.7 (fix for [f05f5ef759c1f7f9]) check-in: 433888e35e user: sebres tags: trunk, main
00:09
lseq: code review, several fixes for [f05f5ef759c1f7f9], unneeded code removed, etc: 1. avoid duplic... check-in: d0a617b9ea user: sebres tags: core-8-branch
00:03
tests illustrating bug [f05f5ef759c1f7f9], incorrect behavior of lseq check-in: f56667ab48 user: sebres tags: core-8-branch
2024-06-11
23:59 • New ticket [f05f5ef759] lseq - duplicative expr eval, bottleneck by usage of ope... artifact: d1ce0f2783 user: sebres

lseq - duplicative expr eval, bottleneck by usage of operators, inconstistent handling of indices (int vs. double vs. error), etc

Closed / Fixedf05f5ef759c1f7f93903fd873250a4c76147c4b2Created 2 years agoUpdated 2 years ago
Descriptionwiki2024-06-11 23:59:30
In an attempt to fix the bottleneck, I found that command 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.

Referenced by1
  • 1043860b5eTicket [f4a4bd7f10] lseq "count" argument also dictates output formatting status still Open with 3 other changes ticket2024-08-30 15:31:54
sebreswiki2024-06-12 00:22:17

4 of the issues (1-3 and 6) are fixed now in 8.7/9.0.
Remaining to be fixed are 4 and 5.

griffinplain2024-06-12 05:09:40
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.
sebreswiki2024-06-12 11:50:46

> 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.

sebreswiki2024-06-12 19:04:49

[c279bfc1b2b30cc1] fixes #5 (lseq will not swallow expr errors anymore).

sebreswiki2024-06-12 20:21:20

[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.

Log in (anonymous is fine) to add a comment.