Artifact 4d2bef5686646af01ef2d3591bd94410fe24018a299196a1a9978ff739bb3027:
- File typeparse/exemples/geometrie/geo_c.tcl — part of check-in [06ad98bead] at 2026-10-25 03:35:19 on branch tip-764_tcl90 — What changes: Consider a list stored in contiguous memory blocks ("cases") where the layout of the elements matches the requested byte representation exactly—same values in the same positions, same size, and following the machine's byte order. Encoding copies these blocks as a single unit, provided there is no padding. Decoding populates these blocks directly, without creating individual objects for each element. This only applies to lists already stored in contiguous blocks (and not when the `-strictpad` option is used). In all other cases, nothing changes. Benchmarks (for a `polyC` with 10,000 points, using `-align natural`): op | without contiguous blocks | with contiguous blocks | encoding | 474 µs | 15 µs | decoding | 3,056 µs | 10 µs | In `geo_c.tcl`, when using `{-compact}pointC`, the byte-processing path for an area now takes approximately 1.0 ms—down from 4.5 ms in v15t—or 1.2 ms without the `{-compact}` option. Encoding now accounts for only a small fraction of this time. The bulk of the processing occurs during the construction of the `polyC` from the polygon: extracting content: 0.25 ms; normalizing numbers: 0.59 ms. The logical next step would be to implement a cast from `list.point` to `list.pointC` that copies the contiguous memory blocks directly—since their layouts are identical—bypassing any text-based conversion. I have left `geo_c.tcl` as is for now. Value stored in C-style contiguous memory buffers. `Tcl_AnnotatedCreateCells(interp, type, data, n, stride, &res)` converts a C array of structs into a Tcl value stored in contiguous memory buffers, without creating individual Tcl objects for each element. Pending a proper stub function, this is implemented via a new command: `tcl::unsupported::compact cells TYPE BYTES ?STRIDE?`. This command verifies that the stride matches the size of an element in the buffer, that the total byte length is a multiple of that size, and that the number of elements corresponds to the `list(N)` type definition. `geo.c` utilizes this for `bezier_echantillons` (Bézier sampling), reducing execution time from 2.3 ms to 0.27 ms. Casting to a buffer-backed list. `type cast list.pointC $P` either copies the buffer contents from a source with an identical layout in a single block, or writes the elements of a standard Tcl list directly into a contiguous memory buffer. If the element types differ only by their signedness, or if the input text is invalid, the standard casting mechanism—along with its associated error messages—is applied. For 10,000 points: performance improves from 990 µs to 237 µs when starting from a standard Tcl list, and from 2,990 µs to 12 µs when starting from an existing buffer-backed list. `geo_c.tcl` has been updated since the last iteration. `pointC` is now defined with `{-compact}`, and `geo::octets` now uses `type cast list.pointC $v` instead of manually extracting the content. All four versions remain consistent across all 22 test cases. Status: All 55 tests pass, including the new `t77_binaire_cases` test. It produces the same bytes as the Tcl codec—both with and without padding—using the 'natural', 'packed', and '4' formats. It also handles decoding, alternative byte order, the '-strictpad' option, an incorrect count, and an empty list, yielding the same output as in version 15v. The output remains the same whether or not the compact form is used. As before, a compact form using a byte order different from that of the host machine is rejected. (user: Florentis, size: 3561) [annotate] [blame] [check-ins using]
# geo_c.tcl -- # # FR : Étape 3 : l'application = ../geometrie.tcl (étape 2, annotée) dont les # calculs lourds passent en C (geo.c). Les autres procédures restent # celles de l'étape 2, inchangées. Les procédures remplacées gardent leur # nom, leurs arguments et leurs types de résultat : le reste du programme # ne voit pas la différence. # # Deux voies de passage vers C (variable geo::voie) : # octets le polygone est encodé selon la disposition C (polyC, ci- # dessous) ; C lit un (const Poly *) ; # objet C reçoit la valeur annotée, vérifie son type, lit ses points. # # EN : Stage 3: the application = ../geometrie.tcl (stage 2, annotated) whose # heavy computations move to C (geo.c). Replaced procedures keep their # name, arguments and result types. Two ways into C (variable geo::voie): # "octets" (bytes in the C layout) or "objet" (the annotated value). set geo_ici [file dirname [file normalize [info script]]] set geo_racine [file normalize [file join $geo_ici .. ..]] source [file join $geo_racine layout filebinary.tcl] source [file join $geo_racine layout binaliases.tcl] source [file join $geo_ici .. geometrie.tcl] load [file join $geo_ici geo[info sharedlibextension]] Geo # FR : la disposition C (geo.c) : struct Poly {uint32_t n; Point pts[];}. # {-compact} : toute liste de pointC est rangée en cases, exactement le # tableau de Point du C ; type binary encode les copie d'un bloc. # EN : the C layout (geo.c); {-compact}: lists of pointC are stored as cells. type def {-compact}pointC {{f64le}x; {f64le}y} type def polyC {{u32le}n; {list($n).pointC}pts} namespace eval geo {variable voie octets} # FR : un polygone, un nuage, une Bézier -> les octets d'un polyC. point et # pointC sont deux nominaux distincts (même forme, types différents) : un # polygone n'entre pas tel quel dans list($n).pointC, il y entre par un # cast vers list.pointC, écrit directement en cases (copié d'un bloc si # le polygone est lui-même en cases). # EN : point and pointC are distinct nominals: cast to list.pointC first. proc geo::octets {v} { set {polyC}p [llength $v] [type cast list.pointC $v] type binary encode polyC $p -align natural } # --------------------------------------------------------------------------- # FR : les procédures remplacées / EN: the replaced procedures proc geo::aire {p} {( P is polygone = $p; $::geo::voie eq "octets" ? [geo::c::aire_raw [geo::octets $P]] : [geo::c::aire_obj $P] )} proc geo::perimetre {p} {( P is polygone = $p; $::geo::voie eq "octets" ? [geo::c::perimetre_raw [geo::octets $P]] : [geo::c::perimetre_obj $P] )} # FR : C rend directement un rectangle annoté / EN: C returns an annotated rectangle proc geo::englobant {p} {( P is polygone = $p; $::geo::voie eq "octets" ? [geo::c::englobant_raw [geo::octets $P]] : [geo::c::englobant_obj $P] )} proc geo::dedans_tous {pts q} {( N is nuage = $pts; Q is polygone = $q; $::geo::voie eq "octets" ? [geo::c::dedans_raw [geo::octets $N] [geo::octets $Q]] : [geo::c::dedans_obj $N $Q] )} # FR : C rend directement une polyligne annotée / EN: C returns an annotated polyline proc geo::bezier_echantillons {b n} {( B is bezier = $b; $::geo::voie eq "octets" ? [geo::c::bezier_raw [geo::octets $B] $n] : [geo::c::bezier_obj $B $n] )}