Artifact dafce9618b46bc6407659080538158621e8dcde34d5195aaf0578d5a66df59fe:
- File generic/tclVarRef.c — part of check-in [203a80b8f5] at 2026-10-25 08:04:02 on branch tip-764_tcl90 — `lappend` under contract. When the variable is the sole holder of its value—which is the typical scenario in an `lappend L ...` loop—the element is validated individually and then added directly to the content. The existing elements are already guaranteed to be valid by the contract. If the element is rejected, the state remains unchanged: the standard execution path proceeds, issuing the usual error message, and the variable remains exactly as it was. The standard execution path is still used in three cases: the value is shared (e.g., copied into another variable or currently being iterated over by a `foreach` loop); the list has a fixed size, `list(N)`; the element is a sum type, a `var` link, `list.any`, or a binary link. For 20,000 `lappend` operations within a procedure: list v15y v15z "Bare" Tcl `list.point` 7.7 s ≈ 15 ms ≈ 10 ms compact form 0.19 s ≈ 15 ms Performance returns to linear time, at approximately 1.4 times the speed of "bare" Tcl. I encountered a crash during development. An earlier version of the shortcut also handled lists containing `var` links, which caused a crash in `t62` because canonical names must be constructed in those cases. I have restricted it to simple element types, and all tests now pass again. Verified so far: All 58 tests pass, including the new `t80_lappend_contrat`. It produces the same output as in `v15y` across various scenarios: shared copies, rejected elements, multiple elements, compact form, sum types, `list.any`, `list(2)`, and `lappend` operations during a `foreach` loop. The output remains identical regardless of whether the compact form is used. A bug was identified while writing the test; it was already present in `v15y` and has not been fixed here. With the `forme` structure (`{double}cote`, `{string}nom`), appending `S {nom}` to a list stores the element without its variant; the `get` method returns an empty string instead of `forme.nom`. I have noted this in the schedule and in the README. ----------------------------- Version v15z. The contracted `lappend` implementation is delivered, following a fix for the memory leak detected by Valgrind. Valgrind now reports no issues, and the full Tcl test suite shows the same 61 failures as in v15. Additional features covered by the C-based codec (v16a): Fixed-size strings: `string(raw,8)` for raw bytes, `string(utf-8,8)` for text. These are padded with zeros upon writing, and trailing zeros are stripped upon reading. Null-terminated strings: `string(…,z)`, using a two-byte null terminator for UTF-16. Literal-based tags: signatures such as "MZ". Tagged fields—`S($field)`: the tag (already read) determines the variant, and the parsed value retains that variant. Any case that the C implementation cannot handle is passed back to the Tcl layer, accompanied by appropriate error messages—for instance, in the event of an unknown tag, an incorrect literal, truncated data, or text that cannot be represented by the specified encoding. Benchmarks: Tcl codec C codec 34-byte header, encoding 41 µs 1.0 µs 34-byte header, decoding 57 µs 1.3 µs Tagged-field block, decoding 66 µs 1.2 µs A performance bottleneck was identified during benchmarking, affecting the C-based decoding codec. The `binary decode` operation was converting data into a byte array before even checking whether the C codec supported that specific type. If it did not, the Tcl layer would then convert the data back into a string to search for the null terminator (as required for a C-style string). Consequently, the entire file’s representation was being toggled back and forth with every single call. The type is now examined first. niveau2.tcl, which reads the tclsh executable, goes from 1.8 s to 0.1 s. Furthermore, these examples maintain the same speed, as their types still rely on calculated sizes, `at(...)` constructs, or links—this corresponds to point 3. Verified thus far: All 59 tests pass, including the new `t81_codec_c_chaines`, which yields the same bytes, values, types, and variants as the Tcl layer. All examples produce identical output whether using the C codec or with `set ::tcl_typebinaryc 0`. Outputs remain the same both with and without the compact format. --------------------------- Version v16a is included in typeparse.zip: the C-based codec can now handle strings, sums of literals, and tagged sums. When processing a small header, encoding and decoding are 40 to 50 times faster than with the Tcl-based codec. Regarding verification: Valgrind reports a clean run, aside from the usual leaks upon exiting `executable_pe`. The full Tcl test suite shows the same 61 failures as in v15. Only `tclTypes.c` has been modified; there are no kernel patches. Version v16a also addresses the performance issues with `type binary decode` for types not covered by the C implementation—previously, the entire file was being converted with every call. As a result, the execution time for `niveau2.tcl` has dropped from 1.8 seconds to 0.1 seconds. As for the C-based codec, the next item to address is point 3: calculated sizes and prefixes (e.g., `string(raw,$len)`, `list(u16).T`). This will provide significant gains for real-world file formats, such as the ELF or PE examples. Following this, the next steps will be to implement `at(...)` and, finally, support for links. (user: Florentis, size: 10463) [annotate] [blame] [check-ins using]
- File typeparse/tclVarRef.c — part of check-in [203a80b8f5] at 2026-10-25 08:04:02 on branch tip-764_tcl90 — `lappend` under contract. When the variable is the sole holder of its value—which is the typical scenario in an `lappend L ...` loop—the element is validated individually and then added directly to the content. The existing elements are already guaranteed to be valid by the contract. If the element is rejected, the state remains unchanged: the standard execution path proceeds, issuing the usual error message, and the variable remains exactly as it was. The standard execution path is still used in three cases: the value is shared (e.g., copied into another variable or currently being iterated over by a `foreach` loop); the list has a fixed size, `list(N)`; the element is a sum type, a `var` link, `list.any`, or a binary link. For 20,000 `lappend` operations within a procedure: list v15y v15z "Bare" Tcl `list.point` 7.7 s ≈ 15 ms ≈ 10 ms compact form 0.19 s ≈ 15 ms Performance returns to linear time, at approximately 1.4 times the speed of "bare" Tcl. I encountered a crash during development. An earlier version of the shortcut also handled lists containing `var` links, which caused a crash in `t62` because canonical names must be constructed in those cases. I have restricted it to simple element types, and all tests now pass again. Verified so far: All 58 tests pass, including the new `t80_lappend_contrat`. It produces the same output as in `v15y` across various scenarios: shared copies, rejected elements, multiple elements, compact form, sum types, `list.any`, `list(2)`, and `lappend` operations during a `foreach` loop. The output remains identical regardless of whether the compact form is used. A bug was identified while writing the test; it was already present in `v15y` and has not been fixed here. With the `forme` structure (`{double}cote`, `{string}nom`), appending `S {nom}` to a list stores the element without its variant; the `get` method returns an empty string instead of `forme.nom`. I have noted this in the schedule and in the README. ----------------------------- Version v15z. The contracted `lappend` implementation is delivered, following a fix for the memory leak detected by Valgrind. Valgrind now reports no issues, and the full Tcl test suite shows the same 61 failures as in v15. Additional features covered by the C-based codec (v16a): Fixed-size strings: `string(raw,8)` for raw bytes, `string(utf-8,8)` for text. These are padded with zeros upon writing, and trailing zeros are stripped upon reading. Null-terminated strings: `string(…,z)`, using a two-byte null terminator for UTF-16. Literal-based tags: signatures such as "MZ". Tagged fields—`S($field)`: the tag (already read) determines the variant, and the parsed value retains that variant. Any case that the C implementation cannot handle is passed back to the Tcl layer, accompanied by appropriate error messages—for instance, in the event of an unknown tag, an incorrect literal, truncated data, or text that cannot be represented by the specified encoding. Benchmarks: Tcl codec C codec 34-byte header, encoding 41 µs 1.0 µs 34-byte header, decoding 57 µs 1.3 µs Tagged-field block, decoding 66 µs 1.2 µs A performance bottleneck was identified during benchmarking, affecting the C-based decoding codec. The `binary decode` operation was converting data into a byte array before even checking whether the C codec supported that specific type. If it did not, the Tcl layer would then convert the data back into a string to search for the null terminator (as required for a C-style string). Consequently, the entire file’s representation was being toggled back and forth with every single call. The type is now examined first. niveau2.tcl, which reads the tclsh executable, goes from 1.8 s to 0.1 s. Furthermore, these examples maintain the same speed, as their types still rely on calculated sizes, `at(...)` constructs, or links—this corresponds to point 3. Verified thus far: All 59 tests pass, including the new `t81_codec_c_chaines`, which yields the same bytes, values, types, and variants as the Tcl layer. All examples produce identical output whether using the C codec or with `set ::tcl_typebinaryc 0`. Outputs remain the same both with and without the compact format. --------------------------- Version v16a is included in typeparse.zip: the C-based codec can now handle strings, sums of literals, and tagged sums. When processing a small header, encoding and decoding are 40 to 50 times faster than with the Tcl-based codec. Regarding verification: Valgrind reports a clean run, aside from the usual leaks upon exiting `executable_pe`. The full Tcl test suite shows the same 61 failures as in v15. Only `tclTypes.c` has been modified; there are no kernel patches. Version v16a also addresses the performance issues with `type binary decode` for types not covered by the C implementation—previously, the entire file was being converted with every call. As a result, the execution time for `niveau2.tcl` has dropped from 1.8 seconds to 0.1 seconds. As for the C-based codec, the next item to address is point 3: calculated sizes and prefixes (e.g., `string(raw,$len)`, `list(u16).T`). This will provide significant gains for real-world file formats, such as the ELF or PE examples. Following this, the next steps will be to implement `at(...)` and, finally, support for links. (user: Florentis, size: 10463) [annotate] [blame] [check-ins using]
/* * tclVarRef.c -- * * Résolution des liens « var » (type var) vers les variables Tcl : voir * le contrat dans tclTypes.h. Un lien est le NOM CANONIQUE d'une * variable : * * ::ns::x, ::a(k) variable d'espace de noms (ou globale), tableau * frame:UID:x variable locale d'un appel de procédure * * Un appel de procédure reçoit un numéro (UID) la première fois qu'un * lien vise l'une de ses variables locales. Tcl_PopCallFrame appelle * TclVarRefFrameGone : l'appel disparaît du registre, et le lien devient * périmé (erreur franche, jamais une autre variable : un UID n'est * jamais réutilisé). Le registre est vide, donc inerte, tant qu'aucun * lien ne vise une variable locale. */ #include "tclInt.h" #include "tclTypes.h" #define REG_KEY "tclTypesFrameRegistry" typedef struct FrameReg { Tcl_HashTable byFrame; /* CallFrame * -> UID */ Tcl_HashTable byUid; /* UID -> CallFrame * */ Tcl_WideInt next; /* prochain UID, jamais réutilisé */ } FrameReg; static void RegDelete(void *clientData, Tcl_Interp *interp) { FrameReg *reg = (FrameReg *) clientData; if (interp != NULL && ((Interp *) interp)->varRefData == reg) { ((Interp *) interp)->varRefData = NULL; } Tcl_DeleteHashTable(®->byFrame); Tcl_DeleteHashTable(®->byUid); Tcl_Free(reg); } static FrameReg * GetReg(Tcl_Interp *interp, int create) { FrameReg *reg = (FrameReg *) ((Interp *) interp)->varRefData; if (reg == NULL && create) { reg = (FrameReg *) Tcl_Alloc(sizeof(FrameReg)); Tcl_InitHashTable(®->byFrame, TCL_ONE_WORD_KEYS); Tcl_InitHashTable(®->byUid, TCL_ONE_WORD_KEYS); reg->next = 1; Tcl_SetAssocData(interp, REG_KEY, RegDelete, reg); ((Interp *) interp)->varRefData = reg; } return reg; } /* Appelé par Tcl_PopCallFrame pour chaque appel qui se termine. */ void TclVarRefFrameGone(Tcl_Interp *interp, CallFrame *framePtr) { FrameReg *reg = GetReg(interp, 0); Tcl_HashEntry *hPtr; if (reg == NULL || reg->byFrame.numEntries == 0) { return; } hPtr = Tcl_FindHashEntry(®->byFrame, framePtr); if (hPtr != NULL) { Tcl_WideInt uid = (Tcl_WideInt) (ptrdiff_t) Tcl_GetHashValue(hPtr); Tcl_HashEntry *uPtr = Tcl_FindHashEntry(®->byUid, (void *) (ptrdiff_t) uid); if (uPtr != NULL) { Tcl_DeleteHashEntry(uPtr); } Tcl_DeleteHashEntry(hPtr); } } static Tcl_WideInt FrameUid(Tcl_Interp *interp, CallFrame *framePtr) { FrameReg *reg = GetReg(interp, 1); Tcl_HashEntry *hPtr; int isNew; Tcl_WideInt uid; hPtr = Tcl_CreateHashEntry(®->byFrame, framePtr, &isNew); if (!isNew) { return (Tcl_WideInt) (ptrdiff_t) Tcl_GetHashValue(hPtr); } uid = reg->next++; Tcl_SetHashValue(hPtr, (void *) (ptrdiff_t) uid); hPtr = Tcl_CreateHashEntry(®->byUid, (void *) (ptrdiff_t) uid, &isNew); Tcl_SetHashValue(hPtr, framePtr); return uid; } /* -------------------------------------------------------------------- * Un nom canonique décomposé * -------------------------------------------------------------------- */ typedef struct Ref { Tcl_Obj *part1; /* nom (qualifié, ou local) */ Tcl_Obj *part2; /* index de tableau, ou NULL */ CallFrame *frame; /* appel visé (variable locale), ou NULL */ } Ref; static void RefFree(Ref *r) { if (r->part1 != NULL) { Tcl_DecrRefCount(r->part1); } if (r->part2 != NULL) { Tcl_DecrRefCount(r->part2); } } /* « nom(index) » -> nom, index. Le nom est le début jusqu'à la première * parenthèse ouvrante, l'index finit à la dernière parenthèse. */ static void SplitIndex(const char *s, Tcl_Size len, Tcl_Size *baseLen, const char **idx, Tcl_Size *idxLen) { const char *lp; *baseLen = len; *idx = NULL; *idxLen = 0; if (len > 2 && s[len - 1] == ')' && (lp = (const char *)memchr(s, '(', len)) != NULL && lp > s) { *baseLen = lp - s; *idx = lp + 1; *idxLen = len - (lp - s) - 2; } } static int RefParse(Tcl_Interp *interp, const char *canon, Ref *r) { Interp *iPtr = (Interp *) interp; Tcl_Size len = (Tcl_Size) strlen(canon), baseLen, idxLen; const char *base = canon, *idx; FrameReg *reg; (void) iPtr; memset(r, 0, sizeof(*r)); if (strncmp(canon, "frame:", 6) == 0) { const char *colon; Tcl_WideInt uid; Tcl_HashEntry *hPtr; char *end; uid = (Tcl_WideInt) strtoll(canon + 6, &end, 10); colon = end; if (*colon != ':') { goto bad; } base = colon + 1; len = (Tcl_Size) strlen(base); reg = GetReg(interp, 0); hPtr = (reg == NULL) ? NULL : Tcl_FindHashEntry(®->byUid, (void *) (ptrdiff_t) uid); if (hPtr == NULL) { Tcl_SetObjResult(interp, Tcl_ObjPrintf( "can't access \"%s\": the procedure call that owned this " "variable has returned", canon)); Tcl_SetErrorCode(interp, "TCL", "TYPE", "STALE", (char *) NULL); return TCL_ERROR; } r->frame = (CallFrame *) Tcl_GetHashValue(hPtr); } SplitIndex(base, len, &baseLen, &idx, &idxLen); r->part1 = Tcl_NewStringObj(base, baseLen); Tcl_IncrRefCount(r->part1); if (idx != NULL) { r->part2 = Tcl_NewStringObj(idx, idxLen); Tcl_IncrRefCount(r->part2); } return TCL_OK; bad: Tcl_SetObjResult(interp, Tcl_ObjPrintf("bad variable link \"%s\"", canon)); Tcl_SetErrorCode(interp, "TCL", "TYPE", "LINK", (char *) NULL); return TCL_ERROR; } /* Exécute le corps avec varFramePtr = l'appel visé, comme uplevel. */ #define WITH_FRAME(interp, r, body) \ do { \ Interp *iPtr_ = (Interp *) (interp); \ CallFrame *saved_ = iPtr_->varFramePtr; \ if ((r)->frame != NULL) { \ iPtr_->varFramePtr = (r)->frame; \ } \ body; \ iPtr_->varFramePtr = saved_; \ } while (0) /* -------------------------------------------------------------------- * Le contrat de tclTypes.h * -------------------------------------------------------------------- */ static void FullName(Tcl_Interp *interp, Var *varPtr, Tcl_Obj *out) { Tcl_GetVariableFullName(interp, (Tcl_Var) varPtr, out); /* l'espace de noms global rend un nom sans « :: » */ if (Tcl_GetString(out)[0] != ':') { Tcl_Obj *tmp = Tcl_NewStringObj("::", 2); Tcl_AppendObjToObj(tmp, out); Tcl_SetStringObj(out, Tcl_GetString(tmp), TCL_AUTO_LENGTH); Tcl_DecrRefCount(tmp); } } int TclVarRefCanon(Tcl_Interp *interp, Tcl_Obj *nameObj, Tcl_Obj **canonPtr) { Interp *iPtr = (Interp *) interp; const char *name = Tcl_GetString(nameObj); Tcl_Size len = (Tcl_Size) strlen(name), baseLen, idxLen; const char *idx; Tcl_Obj *baseObj, *out; Var *varPtr, *arrayPtr = NULL; if (strncmp(name, "frame:", 6) == 0) { /* déjà canonique */ *canonPtr = Tcl_NewStringObj(name, len); return TCL_OK; } SplitIndex(name, len, &baseLen, &idx, &idxLen); if (baseLen == 0) { Tcl_SetObjResult(interp, Tcl_NewStringObj( "a link needs a variable name", TCL_AUTO_LENGTH)); Tcl_SetErrorCode(interp, "TCL", "TYPE", "LINK", (char *) NULL); return TCL_ERROR; } baseObj = Tcl_NewStringObj(name, baseLen); Tcl_IncrRefCount(baseObj); out = Tcl_NewObj(); varPtr = TclObjLookupVarEx(interp, baseObj, NULL, 0, NULL, 0, 0, &arrayPtr); while (varPtr != NULL && TclIsVarLink(varPtr)) { varPtr = varPtr->value.linkPtr; /* upvar, global, variable */ } if (varPtr != NULL && TclGetVarNsPtr(varPtr) != NULL) { FullName(interp, varPtr, out); /* une variable d'espace de noms */ } else if (strstr(name, "::") != NULL) { /* qualifié mais absente : relatif à l'espace de noms courant */ if (name[0] == ':') { Tcl_AppendToObj(out, name, baseLen); } else { Namespace *nsPtr = iPtr->varFramePtr->nsPtr; Tcl_AppendToObj(out, nsPtr->fullName, TCL_AUTO_LENGTH); if (nsPtr != iPtr->globalNsPtr) { Tcl_AppendToObj(out, "::", 2); } Tcl_AppendToObj(out, name, baseLen); if (Tcl_GetString(out)[0] != ':') { Tcl_Obj *tmp = Tcl_NewStringObj("::", 2); Tcl_AppendObjToObj(tmp, out); Tcl_SetStringObj(out, Tcl_GetString(tmp), TCL_AUTO_LENGTH); Tcl_DecrRefCount(tmp); } } } else if (iPtr->varFramePtr->isProcCallFrame) { /* variable locale d'un appel : on le numérote */ Tcl_WideInt uid = FrameUid(interp, iPtr->varFramePtr); Tcl_AppendPrintfToObj(out, "frame:%" TCL_LL_MODIFIER "d:", uid); Tcl_AppendToObj(out, name, baseLen); } else { /* absente, hors procédure : variable de l'espace de noms courant */ Namespace *nsPtr = iPtr->varFramePtr->nsPtr; Tcl_AppendToObj(out, nsPtr->fullName, TCL_AUTO_LENGTH); if (nsPtr != iPtr->globalNsPtr) { Tcl_AppendToObj(out, "::", 2); } if (Tcl_GetString(out)[0] != ':') { Tcl_Obj *tmp = Tcl_NewStringObj("::", 2); Tcl_AppendObjToObj(tmp, out); Tcl_SetStringObj(out, Tcl_GetString(tmp), TCL_AUTO_LENGTH); Tcl_DecrRefCount(tmp); } Tcl_AppendToObj(out, name, baseLen); } if (idx != NULL) { Tcl_AppendToObj(out, "(", 1); Tcl_AppendToObj(out, idx, idxLen); Tcl_AppendToObj(out, ")", 1); } Tcl_DecrRefCount(baseObj); *canonPtr = out; return TCL_OK; } int TclVarRefGet(Tcl_Interp *interp, const char *canon, Tcl_Obj **valuePtr) { Ref r; Tcl_Obj *v = NULL; if (RefParse(interp, canon, &r) != TCL_OK) { return TCL_ERROR; } WITH_FRAME(interp, &r, v = Tcl_ObjGetVar2(interp, r.part1, r.part2, TCL_LEAVE_ERR_MSG)); RefFree(&r); if (v == NULL) { return TCL_ERROR; } *valuePtr = v; return TCL_OK; } int TclVarRefSet(Tcl_Interp *interp, const char *canon, Tcl_Obj *value) { Ref r; Tcl_Obj *res = NULL; if (RefParse(interp, canon, &r) != TCL_OK) { return TCL_ERROR; } WITH_FRAME(interp, &r, res = Tcl_ObjSetVar2(interp, r.part1, r.part2, value, TCL_LEAVE_ERR_MSG)); RefFree(&r); return (res == NULL) ? TCL_ERROR : TCL_OK; } TclType * TclVarRefType(Tcl_Interp *interp, const char *canon) { Ref r; Var *varPtr = NULL, *arrayPtr = NULL; TclType *tp = NULL; if (RefParse(interp, canon, &r) != TCL_OK) { Tcl_ResetResult(interp); return NULL; } WITH_FRAME(interp, &r, varPtr = TclObjLookupVarEx(interp, r.part1, r.part2, 0, NULL, 0, 0, &arrayPtr)); while (varPtr != NULL && TclIsVarLink(varPtr)) { varPtr = varPtr->value.linkPtr; } if (varPtr != NULL && (varPtr->flags & VAR_TYPED) && varPtr->value.objPtr != NULL) { TclAnnotInfo(varPtr->value.objPtr, NULL, &tp, NULL, NULL); } RefFree(&r); return tp; }