Artifact e78b7d6064646b7ec50ecf99c57220fff4ff1623a0e9041fe865604268e2739a:
- File typeparse/tests_tclsh/t70_compact.tcl — part of check-in [40e39579b9] at 2026-10-23 16:08:45 on branch tip-764_tcl90 — The three points are complete: this is version v15p, found in `typeparse.zip`. Along the way, `valgrind` uncovered six memory errors, all of which have been fixed. 1. `tcl::unsupported::abi` provides the byte order, pointer size, and the size and alignment—within a structure—for `i8` through `i64`, `f32`, `f64`, and `ptr` types. These values are determined at compile time. 2. The compact (prototype) form is enabled via `tcl::unsupported::compact on`. For testing purposes, setting the environment variable `TCL_TYPES_COMPACT=1` achieves the same result. The state of a value can be inspected using `... info $P`, and the layout of a type can be viewed using `... layout T`. Four key design decisions have been implemented: use of a canonical form only, support for `int(s,n)`, `int(u,n)`, and `double` types, natural alignment, and the inclusion of an `unsupported` command. From the script perspective, nothing changes; all tests and examples produce identical output regardless of whether the compact form is enabled. The new test `t70_compact` verifies this on a case-by-case basis. Reading: `lindex` and `foreach` provide a view into a memory slot without copying the element. The syntax `$P(i.x)` reads the value directly from the slot. Writing: `set P(i.x)`, `lset`, and `lappend` perform writes directly into the slots. A copy—or a previously read view—retains its original value. C-side: `typeparse/tclCompact.h` provides access to these memory slots. `geo.c` now reads points in-place. standard form compact form memory, 200,000 points 264 bytes/point 16 bytes/point 10,000-point array in C (object-based approach) 564 µs 29 µs `lappend` of 20,000 points 8.7 s 0.25 s array in Tcl (using `lindex` followed by `$a(x)`) 15 ms 23 ms The last row represents the trade-off: when accessed element-by-element from Tcl, each access creates a view and a number. The gains are in memory usage, at the C interface, and during modifications. The `lappend` figure for the standard form is a separate issue: each addition copies the entire typed list. I have added this to the roadmap. 3. type c T … generates a C header containing: `struct`, `enum`, or `union` definitions for sum types; a flexible array member as the final field for variable-length lists; an `_INIT` macro for default values; and `_Static_assert` checks for `sizeof` and `offsetof`. I have verified this using `gcc`. When compiled in 32-bit mode (`-m32`), a structure like `{int(s,32); double}` causes the assertions to fail, as expected. `geo.c` now includes `geo_types.h`, which is generated by running `tclsh geo_types.tcl > geo_types.h`. Memory errors: I identified these using `Valgrind` on a `tclsh` build configured with `-DPURIFY`, which ensures that Tcl objects are allocated via `malloc`. Most of these were already present in v15: The most serious issue: after executing `set e [lindex $P 0]`, attempting to read `$P(0.x)` would cause a crash; furthermore, executing `set P(0.x) 7.5` would overwrite the entire point structure with the value `7.5`. In my v15m version: the error message "fits variants … equally" would inadvertently free the value it was referencing. Rejected declarations: when a declaration failed (e.g., `x is T = v`, `set {T}x`), the code would attempt to set a flag on a variable that had already been destroyed. Memory leak in `type binary decode`: fields that had already been read would leak when the C codec handed control back to the Tcl layer. Regarding the code in your TIP 759 branch: `ParseExpr` would read `nestedPtr->term` after it had already been freed; additionally, every evaluated word `(...)` would leak the result returned by `Tcl_ExprObj`. The fix is included in the `tip759-memoire.patch` file. I have added it to `ORDRE.txt`, but since this is your code, you will need to merge it into your branch. Feel free to remove it from `ORDRE.txt` if you would prefer to handle the fix yourself. Following corrections, Valgrind reports no issues for any tests or examples in either format. The only exception—which is inconsequential—involves a few objects that remain unreleased when a script terminates via an `exit` command mid-execution (in `executable_pe.tcl`). Compared to v15: Project: The only new differences are expected ones—the error message for type-related subcommands now references `c`, and there are new tests: `t69_abi`, `t70_compact`, and `t71_type_c`. All `.out` outputs pass. Full Tcl test suite: 61 failures out of 51,090—exactly the same as in v15. Everything is documented: `doc/ANNOTATIONS.fr.md`: § 12.1 (compact format) and § 12.2 (type `c`), with § 17 also updated; `noyau/LISEZMOI.md`: `v15p` section; `exemples/geometrie/LISEZMOI.md`. Regarding the TIP 764 roadmap, all three items are checked off; I have also added the following: pointer-based cases, direct reading via compiled `expr`, stub functions, and `lappend`. The two TIP 759 fixes are noted in their own dedicated roadmap. (user: Florentis, size: 3809) [annotate] [blame] [check-ins using]
# t70 : représentation compacte (prototype). Chaque cas tourne deux fois, # forme ordinaire puis forme compacte : les résultats doivent être les mêmes. # « forme » dit si la valeur finale est compacte. source [file join [file dirname [file normalize [info script]]] .. layout filebinary.tcl] type def point {{double}x; {double}y} type def ptC {{double(64,le)}x; {double(64,le)}y} type def pt3 {{int(s,32)}id; {point}p; {double(32)}w} proc deux {l s} { set r {} foreach mode {off on} { tcl::unsupported::compact $mode uplevel 1 {unset -nocomplain P Q A e} set rc [catch {uplevel 1 $s} m] lappend r "$rc $m" } tcl::unsupported::compact off set f [uplevel 1 {if {[info exists P]} {tcl::unsupported::compact info $P}}] set forme [expr {$f eq "" ? "ordinaire" : "compacte"}] if {[lindex $r 0] eq [lindex $r 1]} { puts [format "%-30s identique %-9s %s" $l $forme [lindex $r 1]] } else { puts [format "%-30s DIFFÉRENT : %s / %s" $l [lindex $r 0] [lindex $r 1]] } } tcl::unsupported::compact on puts "disposition point : [tcl::unsupported::compact layout point]" puts "disposition pt3 : [tcl::unsupported::compact layout pt3]" catch {tcl::unsupported::compact layout {{string}s}} m; puts "string : $m" catch {tcl::unsupported::compact layout int} m; puts "int : $m" tcl::unsupported::compact off deux "construire" {set {list.point}P {0.5 1.5} {2.5 3.5} {-1.0 4.0}; set P} deux "type, llength" {set {list.point}P {0.5 1.5} {2.5 3.5}; list [type get $P] [llength $P]} deux "lindex, type" {set {list.point}P {0.5 1.5} {2.5 3.5}; set e [lindex $P 1]; list $e [type get $e] $e(y)} deux "chemin P(1.y)" {set {list.point}P {0.5 1.5} {2.5 3.5}; list $P(1.y) [type get $P(1.y)] $P(end.x)} deux "foreach" {set {list.point}P {0.5 1.5} {2.5 3.5}; set o {}; foreach e $P {lappend o $e(x) [type get $e]}; set o} deux "écrire P(1.y)" {set {list.point}P {0.5 1.5} {2.5 3.5}; set P(1.y) 9.5; set P} deux "écrire 9 (non canonique)" {set {list.point}P {0.5 1.5} {2.5 3.5}; set P(1.y) 9; set P} deux "lappend" {set {list.point}P {0.5 1.5}; lappend P {7.5 8.5}; set P} deux "lappend invalide" {set {list.point}P {0.5 1.5}; lappend P {a b}} deux "lset" {set {list.point}P {0.5 1.5} {2.5 3.5}; lset P 0 {1.25 2.25}; set P} deux "lrange, lsort" {set {list.point}P {2.5 3.5} {0.5 1.5}; list [lrange $P 1 end] [lsort $P]} deux "non canonique 1 2" {set {list.point}P {1 2} {3 4}; set P} deux "espaces gardés" {set P [type cast list.point "{0.5 1.5} {2.5 3.5}"]; set P} deux "produit imbriqué" {set {list.pt3}P {7 {0.5 0.25} 0.5} {-3 {1.0 2.0} 0.25}; list $P $P(1.p.y) [type get $P(1.p)]} deux "int(s,32) trop grand" {set {list.pt3}P {7 {0.5 0.25} 0.5}; set P(0.id) 99999999999} deux "double(32) inexact" {set {list.pt3}P {7 {0.5 0.25} 0.1}; set P} deux "u8" {set {list.int(u,8)}P 1 2 3 255; list $P [catch {lappend P 256} m] $m $P} deux "vecteur de double" {set {list.double}P 0.1 0.2 0.30000000000000004; list $P [expr {$P(0) + $P(2)}]} deux "lire après lindex" {set {list.point}P {0.5 1.5} {2.5 3.5}; set e [lindex $P 0]; list $P(0.x) $P(0.y)} deux "vue gardée après écriture" {set {list.point}P {0.5 1.5} {2.5 3.5}; set e [lindex $P 0]; set P(0.x) 7.5; list $e $P} deux "copie puis écriture" {set {list.point}P {0.5 1.5}; set Q $P; set Q(0.y) 5.5; list $P $Q} deux "élément vers autre liste" {set {list.point}A {0.5 1.5} {2.5 3.5}; set {list.point}P [lindex $A 1] [lindex $A 0]; set P} deux "string length" {set {list.point}P {0.5 1.5}; list [string length $P] [type get $P]} deux "contenu" {set {list.point}P {0.5 1.5}; tcl::unsupported::content $P} deux "binary decode" {set P [type binary decode list(2).ptC [binary format q4 {1.5 2.5 3.5 4.5}]]; set P}