Artifact aa2f16f5dc1fed4b2c9c64b1ddeda23bb1ea6884bee11fc30eac65363e5fef60:
- File typeparse/exemples/geometrie/LISEZMOI.md — part of check-in [7952bb1028] at 2026-10-24 04:37:06 on branch tip-764_tcl90 — V15q `type def {-compact}T`, `type is {compact?}`, and `type {c!}export` are now in place, following the chosen design—this is version v15q, available in `typeparse.zip`. `type is {compact?}polygone` checks the type, while `type is {compact?}$P` checks the value. I did not need to modify local annotations for value-based checks; the value retains only its content, which is precisely what holds the underlying data cells. tcl type def {-compact}polygone list.point set {polygone}P {0 0} {4 0} {4 3} ;# {0.0 0.0} {4.0 0.0} {4.0 3.0} type is {compact?}polygone ;# 1: the type requires it type is {compact?}$P ;# 1: the value is stored in cells string length $P; type is {compact?}$P ;# still 1 type {c!}export point ;# the C header (replaces `type c`) Normalization upon construction: `0` becomes `0.0`, and `0x10` becomes `16`. A `double(32)` is rounded to a `float`: `0.1` becomes `0.10000000149011612`. This normalization also applies to `set P(i.x)`, `lset`, and `lappend`. Conversions on a copy: core string and byte functions now operate on a string copy, maintained alongside the data cells. This is implemented via the new kernel patch `compact-conversions.patch`, which modifies `tclStringObj.c` and `tclBinary.c`. `tcl::unsupported::content` returns a copy that shares the same underlying data cells. This also applies to the global switch: in v15p, `string length $P` would break the compact form, but that is no longer the case. The following are disallowed in a definition: a type that is not a list of fixed-size elements (the definition is discarded, and the name remains available); an unknown option; `{-compact}` applied to an alias; a parameterized family (which I have not yet implemented). Export: `{llvm!}export` returns "not available yet," and an export without a target—or with an unknown target—is rejected. I discovered a flaw along the way: `string reverse` could modify the content of an annotated value in-place. This is no longer the case. A limitation regarding the `is` type check: if the text of a value also happens to be a type name (a value that is literally a type name), the query is interpreted as a type check. I have noted this in the planning. Tests against v15p: All expected outputs pass, with the exception of `t70_compact.out`; I have regenerated it because `string length` now maintains the compact format there. All tests and examples yield the same output, regardless of whether `TCL_TYPES_COMPACT=1` is set. The Tcl `string*`, `binary`, `stringObj`, `utf`, `dict`, `lset*`, and `list*` test suites show 3,482 passes out of 3,579, with 97 skipped and zero failures—consistent with v15. `valgrind` (PURIFY build) reports no issues for `t70_compact`, `t71_type_c`, and the new `t72_compact_type`. (user: Florentis, size: 4067) [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!}export 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), ou un type déclaré type def {-compact}polygone list.point, 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.