Artifact ab0faf8753ac980ddde88a81a727fb8ca4b9e43c305e41f327f824d0a44a62e8:
- File typeparse/tclCompact.h — 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: 2650) [annotate] [blame] [check-ins using]
/* * tclCompact.h -- * * FR : La forme compacte d'une liste typée, vue d'une extension C * (prototype, TIP 764 ; tcl::unsupported::compact on). * * Une liste list.T, où T est un scalaire à taille fixe (int(s,n), * int(u,n) avec n = 8, 16, 32, 64 ; double, double(32|64)) ou un produit * de tels champs, peut être rangée en cases : n éléments de stride * octets, disposés comme le struct C que produit `type c T` (alignement * naturel). TclCompactViewOf rend cette vue, ou NULL si la valeur n'est * pas sous forme compacte (forme ordinaire : la lire avec les fonctions * de liste de Tcl). * * Le pointeur data est valable tant que la valeur n'est ni modifiée ni * convertie : pendant la commande qui la reçoit, sans rappeler Tcl sur * elle. Vérifier elemType et stride avant de lire, puis lire en const. * * Prototype : une fonction de stub (Tcl_AnnotatedCompact...) remplacera * ce passage par les types d'objets enregistrés. * * EN : The compact form of a typed list, seen from a C extension (prototype). * TclCompactViewOf returns the view, or NULL when the value is not in * compact form. data is valid while the value is neither modified nor * converted. Check elemType and stride before reading; read it as const. */ #ifndef TCL_COMPACT_H #define TCL_COMPACT_H #include <tcl.h> #include <string.h> typedef struct TclCompactView { unsigned char *data; /* n cases de stride octets */ Tcl_Size n; size_t stride; const char *elemType; /* nom du type des éléments, ex. "point" */ } TclCompactView; static inline const TclCompactView * TclCompactViewOf(Tcl_Obj *obj) { static const Tcl_ObjType *annotatedType = NULL, *compactType = NULL; if (compactType == NULL) { compactType = Tcl_GetObjType("compactlist"); annotatedType = Tcl_GetObjType("annotated"); } if (compactType == NULL) { return NULL; /* Tcl sans forme compacte */ } if (annotatedType != NULL && obj->typePtr == annotatedType) { obj = (Tcl_Obj *) obj->internalRep.twoPtrValue.ptr1; /* le contenu V */ } if (obj->typePtr != compactType) { return NULL; } return (const TclCompactView *) obj->internalRep.twoPtrValue.ptr1; } /* La vue, si ses éléments sont du type nommé et font size octets. */ static inline const TclCompactView * TclCompactViewOfType(Tcl_Obj *obj, const char *elemType, size_t size) { const TclCompactView *v = TclCompactViewOf(obj); if (v == NULL || v->stride != size || strcmp(v->elemType, elemType) != 0) { return NULL; } return v; } #endif /* TCL_COMPACT_H */