Skip to content
Tcl Source Code Check-in [203a80b8f5]
Login

Check-in [203a80b8f5]

Overview
Comment:`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.
Timelines: family | ancestors | descendants | both | tip-764_tcl90
Files: files | file ages | folders
SHA3-256: 203a80b8f59715d0ae55d2d68ccc4dba19200e4c73f66fda453ef9a36fea3e6c
User & Date: Florentis on 2026-10-25 08:04:02.635
Other Links: branch diff | manifest | tags
Context
2026-10-25
09:50
Points 4 and 5 have been completed and delivered in v16c. Regarding geometry, the annotated Tcl is n... check-in: 4a3a97279c user: Florentis tags: tip-764_tcl90
08:04
`lappend` under contract. When the variable is the sole holder of its value—which is the typical s... check-in: 203a80b8f5 user: Florentis tags: tip-764_tcl90
03:35
What changes: Consider a list stored in contiguous memory blocks ("cases") where the layout of the e... check-in: 06ad98bead user: Florentis tags: tip-764_tcl90
Changes