Artifact 107a0849402fa101c3cfe4dc075131317d656a2280eed2813356d90058cc55f5:
- File typeparse/tests_tclsh/t81_codec_c_chaines.tcl — part of check-in [203a80b8f5] at 2026-10-25 08:04:02 on branch tip-764_tcl90 — `lappend` under contract. When the variable is the sole holder of its value—which is the typical scenario in an `lappend L ...` loop—the element is validated individually and then added directly to the content. The existing elements are already guaranteed to be valid by the contract. If the element is rejected, the state remains unchanged: the standard execution path proceeds, issuing the usual error message, and the variable remains exactly as it was. The standard execution path is still used in three cases: the value is shared (e.g., copied into another variable or currently being iterated over by a `foreach` loop); the list has a fixed size, `list(N)`; the element is a sum type, a `var` link, `list.any`, or a binary link. For 20,000 `lappend` operations within a procedure: list v15y v15z "Bare" Tcl `list.point` 7.7 s ≈ 15 ms ≈ 10 ms compact form 0.19 s ≈ 15 ms Performance returns to linear time, at approximately 1.4 times the speed of "bare" Tcl. I encountered a crash during development. An earlier version of the shortcut also handled lists containing `var` links, which caused a crash in `t62` because canonical names must be constructed in those cases. I have restricted it to simple element types, and all tests now pass again. Verified so far: All 58 tests pass, including the new `t80_lappend_contrat`. It produces the same output as in `v15y` across various scenarios: shared copies, rejected elements, multiple elements, compact form, sum types, `list.any`, `list(2)`, and `lappend` operations during a `foreach` loop. The output remains identical regardless of whether the compact form is used. A bug was identified while writing the test; it was already present in `v15y` and has not been fixed here. With the `forme` structure (`{double}cote`, `{string}nom`), appending `S {nom}` to a list stores the element without its variant; the `get` method returns an empty string instead of `forme.nom`. I have noted this in the schedule and in the README. ----------------------------- Version v15z. The contracted `lappend` implementation is delivered, following a fix for the memory leak detected by Valgrind. Valgrind now reports no issues, and the full Tcl test suite shows the same 61 failures as in v15. Additional features covered by the C-based codec (v16a): Fixed-size strings: `string(raw,8)` for raw bytes, `string(utf-8,8)` for text. These are padded with zeros upon writing, and trailing zeros are stripped upon reading. Null-terminated strings: `string(…,z)`, using a two-byte null terminator for UTF-16. Literal-based tags: signatures such as "MZ". Tagged fields—`S($field)`: the tag (already read) determines the variant, and the parsed value retains that variant. Any case that the C implementation cannot handle is passed back to the Tcl layer, accompanied by appropriate error messages—for instance, in the event of an unknown tag, an incorrect literal, truncated data, or text that cannot be represented by the specified encoding. Benchmarks: Tcl codec C codec 34-byte header, encoding 41 µs 1.0 µs 34-byte header, decoding 57 µs 1.3 µs Tagged-field block, decoding 66 µs 1.2 µs A performance bottleneck was identified during benchmarking, affecting the C-based decoding codec. The `binary decode` operation was converting data into a byte array before even checking whether the C codec supported that specific type. If it did not, the Tcl layer would then convert the data back into a string to search for the null terminator (as required for a C-style string). Consequently, the entire file’s representation was being toggled back and forth with every single call. The type is now examined first. niveau2.tcl, which reads the tclsh executable, goes from 1.8 s to 0.1 s. Furthermore, these examples maintain the same speed, as their types still rely on calculated sizes, `at(...)` constructs, or links—this corresponds to point 3. Verified thus far: All 59 tests pass, including the new `t81_codec_c_chaines`, which yields the same bytes, values, types, and variants as the Tcl layer. All examples produce identical output whether using the C codec or with `set ::tcl_typebinaryc 0`. Outputs remain the same both with and without the compact format. --------------------------- Version v16a is included in typeparse.zip: the C-based codec can now handle strings, sums of literals, and tagged sums. When processing a small header, encoding and decoding are 40 to 50 times faster than with the Tcl-based codec. Regarding verification: Valgrind reports a clean run, aside from the usual leaks upon exiting `executable_pe`. The full Tcl test suite shows the same 61 failures as in v15. Only `tclTypes.c` has been modified; there are no kernel patches. Version v16a also addresses the performance issues with `type binary decode` for types not covered by the C implementation—previously, the entire file was being converted with every call. As a result, the execution time for `niveau2.tcl` has dropped from 1.8 seconds to 0.1 seconds. As for the C-based codec, the next item to address is point 3: calculated sizes and prefixes (e.g., `string(raw,$len)`, `list(u16).T`). This will provide significant gains for real-world file formats, such as the ELF or PE examples. Following this, the next steps will be to implement `at(...)` and, finally, support for links. (user: Florentis, size: 2325) [annotate] [blame] [check-ins using]
# t81 : codec C pour les chaînes (taille fixe, cstring), les sommes de # littéraux et les sommes balisées S($champ) : mêmes octets et mêmes # valeurs (texte, types des champs, variante) que la couche Tcl. 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 champs {v} { if {[catch {set c [::tcl::unsupported::content $v]}]} {return [type get $v]} set r [type get $v] foreach f [type get -fields [type get $v]] {catch {lappend r $f=[type get $v($f)]}} set r } 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 "%-12s %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 mz {"MZ"} type def entete {{mz}sig; {u16le}n; {string(raw,4)}tag; {string(utf-8,8)}nom; {string(utf-8,z)}texte; {string(utf-16le,z)}large} set {entete}e MZ 7 ab\x01d Été cstring héhé essai entete entete $e essai naturel entete $e -align natural type def pp {{u16le}a; {u16le}b} type def corps {{pp}1, {string(raw,3)}2, {u32le}7} type def bloc {{u8}genre; {corps($genre)}corps; {u8}fin = 255} set {pp}p 5 6 set {bloc}b1(genre,corps) 1 {1}$p set {bloc}b2(genre,corps) 2 {2}xyz set {bloc}b7(genre,corps) 7 {7}99 foreach b [list $b1 $b2 $b7] {essai "bloc [lindex $b 0]" bloc $b} type def corps2 {{u8}AA, {u16le}BB} type def bloc2 {{string(raw,2)}k; {corps2($k)}v} set {bloc2}c(k,v) BB {BB}513 essai "balise txt" bloc2 $c set d [type binary decode bloc [type binary encode bloc $b1]] puts "variante relue : [type get $d(corps)] $d(corps.1.b)" puts "balise inconnue : [catch {type binary decode bloc [binary format cu 9]} m] [string range $m 0 60]" puts "littéral faux : [catch {type binary decode entete [binary format a2 ZZ]} m] [string range $m 0 50]" puts "tronqué : [catch {type binary decode entete [binary format a5 MZ\x07\x00a]} m] [string range $m 0 40]"