Artifact d1c78399a902b148b102456a292f67076115a5d9d0c33572bbdfe3757a4de8b2:
- File typeparse/tests_tclsh/t85_codec_c_at.tcl — part of check-in [1a6fc7a4f8] at 2026-10-25 12:21:18 on branch tip-764_tcl90 — I have implemented the three points, delivered in v16d. The C codec now covers calculated sizes and prefixes, `at(...)`, and links. It produces the same bytes and values as the Tcl layer, with a performance gain of 30 to 100 times. 1. Calculated sizes and prefixes Length prefix: `string(utf-8,int(u,16,be))`, `string(raw,int(u,8))`, `list(int(u,8)).T`. Expressions based on previously read fields: `$len`, `$len - 4`, `($a + $b) * 2`, with `/` and `%` operators rounding toward negative infinity (matching Tcl’s `expr` behavior). Less common forms, such as `$h(off)` or a field from an external product, remain handled by the Tcl layer. 2. `at(EXPR).T` During reading, the value is retrieved from the calculated position without moving the cursor. During writing, the value is encoded separately and then placed at the specified position at the end, consistent with the Tcl layer’s behavior. 3. Links `*(ORIGIN,REPR).T` Origins: `//`, `.`, `./`, and `../`. Reading: Within a product, the target is read after all other fields; consequently, its size definitions may reference a field that appears later. Writing: Targets are written after the value, and their addresses are subsequently backfilled. `opt(T)` and linked lists: The C implementation supports up to 512 chained links; beyond this limit, the Tcl layer takes over. The following remain handled by the Tcl layer: named reference systems, finite `-linkdepth`, `-dedupe`, and null links for fields with a default value. Measurements, in µs, C codec vs. Tcl layer: |case | write | read | |:--------| ---- :| --------:| prefixed header | 1.3 / 36 | 1.7 / 54 | string(raw,$len) | 0.9 / 51 | 1.1 / 68 | at($off).list($n).pt| 1.9 / 107| 3.0 / 163 | two ./ links| 1.6 / 121| 1.6 / 108 | linked list of 400 nodes| 0.10 / 304 ms | 0.23 / 1,444 ms The Tcl layer exhibits quadratic behavior with linked lists, which accounts for the discrepancy in the last row. Fix implemented: A list of links in compact form (e.g., list($n).*(//,u8).rec) was being copied element-by-element, just like a list of ordinary values, without accounting for the links. This resulted in incorrect bytes being written. Verifications: All 64 tests pass, in both ordinary and compact forms. Three new tests cover these scenarios: t84, t85, and t86. All examples produce identical output, whether using the C codec or not. Valgrind reports no new issues, and a full `make test` run shows the same 61 failures as the reference. (user: Florentis, size: 2304) [annotate] [blame] [check-ins using]
# t85 : codec C pour at(EXPR).T : la valeur est lue à la position EXPR (depuis # le début de la valeur lue) sans déplacer le curseur, et écrite à part puis # posée à cette position à la fin (zéros entre deux) : mêmes octets et mêmes # valeurs que la couche Tcl, y compris at(...) imbriqués, listes à nombre # dynamique, -offset et -align natural. set ici [file dirname [file normalize [info script]]] source [file join $ici .. layout filebinary.tcl] source [file join $ici .. layout binaliases.tcl] proc tclc {args} {set ::tcl_typebinaryc 0; try {uplevel 1 $args} finally {set ::tcl_typebinaryc 1}} proc essai {nom T v args} { set o [type binary encode $T $v {*}$args] set ot [tclc type binary encode $T $v {*}$args] set d [type binary decode $T $o {*}$args] set dt [tclc type binary decode $T $o {*}$args] puts [format "%-14s %3d octets C=Tcl %d relu C=Tcl %d relu=v %d %s" $nom \ [string length $o] [expr {$o eq $ot}] [expr {$d eq $dt && [type get $d] eq [type get $dt]}] \ [expr {$d eq $v}] [binary encode hex $o]] } type def pt {{u16le}x; {u16le}y} type def tete {{u32le}off; {u8}n; {at($off).list($n).pt}pts; {u8}fin} set {tete}t 12 2 {{1 2} {3 4}} 255 essai "at liste" tete $t essai "at nat" tete $t -align natural type def hdr {{string(raw,2)}sig; {u16le}poff; {at($poff * 2).pt}p; {at($poff * 2 + 4).u32le}z} set {hdr}h MZ 4 {7 8} 99 essai "at calculé" hdr $h type def chaine {{u8}o; {u8}l; {at($o).string(utf-8,$l)}s} set {chaine}c 3 5 été essai "at chaîne" chaine $c type def inner {{u8}k; {at(10).u8}far} type def outer {{u8}a; {at(4).inner}in} set {inner}ii 5 6 set {outer}oo(a,in) 1 $ii essai "at imbriqué" outer $oo type def recouv {{u8}a; {at(0).u8}b} set {recouv}r 1 2 essai "recouvrement" recouv $r set o [type binary encode tete $t] set d [type binary decode tete "\x00\x00\x00$o" -offset 3] set dt [tclc type binary decode tete "\x00\x00\x00$o" -offset 3] puts "-offset : [expr {$d eq $dt}] $d" type binary decode tete $o -consumed n1 tclc type binary decode tete $o -consumed n2 puts "-consumed : $n1 $n2" puts "hors données : [catch {type binary decode tete [binary format iucu 100 1]} m] [string range $m 0 60]" type def p2 {{u8}o; {at($h(o)).u8}x} puts "chemin : [catch {essai chemin p2 [set {p2}z 1 2]} m] $m"