Skip to content
Tcl Source Code Changes To Migrating C extensions to Tcl 9
Login

Changes To Migrating C extensions to Tcl 9

Changes to "Migrating C extensions to Tcl 9" between 2023-11-26 17:44:40 and 2023-11-27 15:38:51

︙
172
173
174
175
176
177
178
179

180












181






182
183
184
185
186
187
188
172
173
174
175
176
177
178

179
180
181
182
183
184
185
186
187
188
189
190
191
192

193
194
195
196
197
198
199
200
201
202
203
204
205







-
+

+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+







no existing references to it.

If the extension needs to maintain compatibility with Tcl 8.6, an alternative
approach is to perform a pair of `Tcl_IncrRefCount` and `Tcl_DecrRefCount` calls
on the element. While this achieves the same effect as the `Tcl_BounceRefCount`
function, it comes at the cost of a slight efficiency trade-off.

### Prefer Tcl_ListObjIndex to Tcl_ListObjGetElements
### Prefer Tcl_ListObjIndex to Tcl_ListObjGetElements for large lists

The introduction of abstract list implementations has an additional consequence
related to iteration over lists. In Tcl 8, it was common practice to iterate
over list elements by calling `Tcl_ListObjGetElements` which returned a pointer
to the array of `Tcl_Obj *` elements stored in the internal representation of a
list. In Tcl 9, the internal representation may take a different form and use of
this function forces generation of an array with potentially significantly
higher memory usage. For example, the `lseq` command which is used to create a
list of integers has an extremely compact internal representation. A list of a
million consecutive integers takes up under a hundred bytes of memory. However,
if `Tcl_ListObjGetElements` is called on this list, it is forced to allocate an
array of a million pointers each pointing to a `Tcl_Obj` holding an integer --
more than 50MB of memory.
TODO

It is therefore recommended that in cases where an extension may be dealing with
large lists, iteration be done using the `Tcl_ListObjIndex` function instead
of `Tcl_ListObjGetElements`. Note however that there is a performance tradeoff here
as iterating using `Tcl_ListObjGetElements` is faster as the returned array can
be directly accessed without a function call for each element.

## Tommath

Tcl 9 optionally allows the linking of an external `libtommath` library for multiple-precision
integer arithmetic in lieu of Tcl's internal version. This has some consequences for extensions:

* If including `tclTomMath.h`, Tcl's stubs interface to `libtommath` is used and only a subset of
︙