Artifact 4aefe9c768af2c36ee1092fd2ee878d526001f4540969156bac6fa618aba213d:
- File typeparse/exemples/geometrie/LISEZMOI.md — 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: 3995) [annotate] [blame] [check-ins using]
# Géométrie en trois étapes Le même petit morceau de `math::geometry` (tcllib), écrit trois fois, selon le principe « script + compilé » : | Étape | Fichier | Ce que c'est | |---|---|---| | 1. prototype, Tcl nu | `geo_brut.tcl` | listes plates à la tcllib : un point est `{x y}`, rien ne distingue un segment d'une droite | | 2. prototype annoté | `../geometrie.tcl` | chaque objet a son type (`point`, `segment`, `droite`, `polygone`…) ; corps `expr` (TIP 759) | | 3. application | `geo_c.tcl` + `geo.c` | l'étape 2, dont cinq calculs lourds passent en C ; le reste est inchangé | Les trois étapes ont les mêmes noms (`geo::aire`, `geo::projete`…), les mêmes arguments et les mêmes résultats. ## À l'étape 3 Le type Tcl décrit la structure C : ``` type def pointC {{f64le}x; {f64le}y} typedef struct {double x, y;} Point; type def polyC {{u32le}n; typedef struct {uint32_t n; Point pts[];} Poly; {list($n).pointC}pts} ``` Deux voies vers le C, au choix (`set geo::voie octets|objet`) : - **octets** : le polygone est encodé par `type binary encode polyC … -align natural`, et C lit un `(const Poly *)` ; - **objet** : C reçoit la valeur annotée elle-même, vérifie son type (`geo: expected a polygone but got "nuage"`) et lit ses points. Fonctions en C : `aire`, `perimetre`, `englobant`, `dedans_tous` (un nuage de points dans un polygone), `bezier_echantillons` (n points d'une Bézier cubique). ## Lancer ``` tclsh ../geometrie.tcl ;# les exemples commentés (étape 2) tclsh verifier.tcl ;# les 22 exemples dans les 4 versions : mêmes résultats ? tclsh bench.tcl ?N? ;# vitesses comparées avec timerate (N = 10000 par défaut) ``` ## L'en-tête C vient des types `geo_types.h` est produit par `type c` depuis les `type def` (`point`, `pointC`, `polyC`), avec des `_Static_assert` qui arrêtent la compilation si le compilateur range autrement que Tcl : ``` tclsh geo_types.tcl > geo_types.h ``` ## Forme compacte Avec `tcl::unsupported::compact on` (ou `TCL_TYPES_COMPACT=1 tclsh bench.tcl`), un polygone est rangé en cases de 16 octets, exactement un tableau de `point` : la voie objet de `geo.c` le lit en place (`tclCompact.h`), sans relire chaque point. | Calcul (N = 10000, µs par appel) | voie objet, forme ordinaire | voie objet, forme compacte | |---|---:|---:| | aire | 564 | 29 | | périmètre | 629 | 94 | | englobant | 568 | 27 | L'étape 2 (Tcl annoté, `lindex` et `$a(x)` à chaque point) est plus lente en forme compacte (aire : 15 → 23 ms) : chaque accès fabrique une vue et un nombre. ## Compiler `geo.c` Linux : ``` gcc -shared -fPIC -O2 -DUSE_TCL_STUBS -I<src>/generic -I<src>/unix -o geo.so geo.c <build>/libtclstub.a -lm ``` Windows (MinGW) : ``` gcc -shared -O2 -DUSE_TCL_STUBS -I<src>/generic -I<src>/win -o geo.dll geo.c <build>/libtclstub.a ``` ## Mesures (Linux, N = 10000, µs par appel) | Calcul | 1 brut | 2 annoté | 3 C, octets | 3 C, objet | |---|---:|---:|---:|---:| | aire (N points) | 3 288 | 32 702 | 1 002 | 461 | | périmètre (N points) | 14 345 | 190 730 | 1 296 | 555 | | englobant (N points) | 1 526 | 26 520 | 897 | 516 | | dedans_tous (1000 points × polygone de 200) | 57 339 | 766 846 | 561 | 532 | | Bézier, N points | 22 716 | 114 739 | 4 458 | 4 805 | Voie octets, aire : 1 029 µs d'encodage en Tcl pour 19 µs de calcul en C. La frontière coûte plus que le calcul : il faut envoyer au C des opérations « grosses ». `englobant` et `bezier_echantillons` rendent directement une valeur annotée (`rectangle`, `polyligne`) : C construit la liste des points et la fait typer (`type cast`, en attendant une fonction de stub). Pour la Bézier, mesurée seule dans un interpréteur neuf : 3,8 ms avant (liste plate, puis `lmap` et typage en Tcl), 1,8 ms après. Dans `bench.tcl`, après les autres mesures, le même appel prend 4,5 ms : le tas déjà chargé par les grandes données pèse sur les allocations.