🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.tcl
Article #81

Re: Announce bitarray 1.0 - packed integer arrays for Tcl 9 (C extension)

#81
From: et99
Date: Fri, 23 Oct 2026 08:28
85 lines
6924 bytes
Hi Olivier:

The speed comes from being done in C instead of a Tcl loop, 64 bits at a time (and with SSE2 for some of the 8-, 16- and 32-bit operators). And yes, once the samples are integers, a threshold test gives a 1-bit array that you can combine with other tests and counting.

The answer to your question is that they aren't mnemonics, they are the operator words of bitarray op. 'land' is a logical 'and' along with two other logicals: 'lor' and 'lnot'.

My list in the announcement is wrong: it shows 'land', 'lor' and 'lnot' on the bitwise line as well. They belong only to the logical family, and the bitwise line should read 'and', 'or', 'xor', 'andnot' and 'not', or the symbols & | ^ &~ ~. The README's table is correct, so at least I only got it wrong in one place.

The measurement that looks incredible is the first row of the table, a bitwise 'and' of two 1-bit arrays of one million elements (11,000x), and the next row is the same for 8-bit arrays (1,450x). The plain-list time, 87,347 microseconds, is for this ordinary Tcl loop, where $l and $other are lists of a million 0s and 1s:

     set X [lmap x $X y $Z {expr {$x & $y}}]

and the bitarray times, 7.9 microseconds for 1-bit arrays and 60 for 8-bit ones, are for the single command:

     bitarray op X = X and $Z     ;# updates X in place: X = X and Z

Writing the result into a new array instead of updating X in place takes about twice as long, 13 to 16 microseconds, so the speed-up for 1-bit arrays is then about 5,000x to 6,000x. Looking at the code where we write to a new variable reveals we zero'd the new array first, which likely is the cost, not so much the allocation of a new tcl object. We'll fix that in a later release.

This is the loop in C that runs inside the extension (BitOpWords in bitarray.c), where x, y and dst are the arrays of 64-bit words and m is the number of words:

     for (k = 0; k < m; k++) {
         uint64_t xv = x->words[k], yv = y->words[k];
         dst->words[k] = xv & yv;
     }

A million 1-bit elements are only 15,625 words, so m is 15,625 and the C loop makes that many passes where the Tcl loop makes a million, and each Tcl pass goes through the interpreter. With 8-bit elements there are 125,000 words, which is why that row is lower.

There is no hand-written vector code in this loop (the SSE2 code is in the logical and relational operators, not here). I asked Claude to show me the machine code that gcc 13 (the GNU C compiler) produced for it on Linux (Intel syntax, as shown by objdump). Built with -O2 it is the plain loop, one 64-bit word per pass:

     mov   r9, [rsi+rcx*8+0x40]    ; load a word from one array
     and   r9, [rdi+rcx*8+0x40]    ; AND it with the word from the other array
     mov   [rax+rcx*8+0x40], r9    ; store the result
     add   rcx, 1                  ; next word
     cmp   r8, rcx                 ; reached m?
     jne   top                     ; no, go round again

Built with -O3 the compiler vectorized it by itself, two words (128 bits) per pass:

     movdqu xmm0, [rsi+rcx]        ; load 16 bytes (two words) from one array
     movdqu xmm2, [rdi+rcx]        ; load 16 bytes from the other array
     add    r9, 1                  ; loop counter
     pand   xmm0, xmm2             ; one AND of all 128 bits
     movups [rax+rcx], xmm0        ; store 16 bytes
     add    rcx, 0x10              ; advance 16 bytes
     cmp    r10, r9                ; reached m/2?
     jne    top                    ; no, go round again

On Linux the vectorized build was about 1.7 times faster for the 1-bit case (about 4 microseconds against 7 for a million elements). The table's numbers are from Windows with Visual C++ /O2, and there the time was 7.9 microseconds, which is about what the plain loop gave on Linux and not what the vectorized one gave.

So my guess is that the Visual C++ build ran the plain loop, but I have not looked at its machine code.

The benchmark that produced the table is extras/listbench.tcl in the repository, and I would be glad to hear what numbers you get on your own machine.

Thanks for your interest,

Eric


On 10/22/2026 2:16 AM, Olivier wrote:
> et99 <et99@rocketship1.me> posted:
> 
>>
>>
>> The bitarray C extension stores arrays of small integers (1, 8, 16, 32 or 64 bits, signed or unsigned) packed in memory that looks and behaves like an ordinary Tcl list. To the rest of Tcl a bitarray IS a list: lindex, lset, lrange, lreplace, linsert, foreach, "in", lsort, lsearch, join, {*}$a all work on a bitarrary.
>>
>> .....
>>
>> They work a 64-bit word at a time, and the logical and relational operators use SSE2 for the 8-, 16- and 32-bit widths on x86-64 (on by default; one build switch turns it off). The table below is one million elements, microseconds per call, measured on Windows with Visual C++ /O2 against the same job done on a plain list with an ordinary Tcl loop. The numbers are from running extras/listbench.tcl on a Windows run.
>>
>>       operation                         plain list     bitarray    speed-up-approx
>>       a AND b (1-bit arrays)                87,347          7.9       11,000x
>> ....
>>       "> 1000" on a 16-bit array           250,769          315          800x
>>
>> Questions, bug reports and criticism are welcome. Mit Licence.
>>
>> -Eric
>>   
> 
> It seems astonishing, almost incredible, especially for the use on digital signals or
> analog signals (after being converted to integer). What are land, lnot ...?
> Are they just mnemonics you used for your explanations ?
> 
> Olivier.
Technical Details
Message-ID: <11bf5sa$1ju6e$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11bb56j$8k4k$1@dont-email.me> <1792660578-1108@newsgrouper.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: et99 <et99@rocketship1.me>
Newsgroups: comp.lang.tcl
Subject: Re: Announce bitarray 1.0 - packed integer arrays for Tcl 9 (C
 extension)
Date: Fri, 23 Oct 2026 01:28:57 -0700
Organization: A noiseless patient Spider
Lines: 85
Message-ID: <11bf5sa$1ju6e$1@dont-email.me>
References: <11bb56j$8k4k$1@dont-email.me> <1792660578-1108@newsgrouper.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 23 Oct 2026 08:28:59 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1702094"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+CNZZx2H88DJcsDyZPTl1Q"; posting-host="69e81acc20d4d43c58ecbffdbd3be501"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:lVo8t3+dhesMz9bD6Zu+mlu9J40= sha256:7JJ27J3WDMjJ7g0tS0V2UvfOhPQb0bJm63W/CyrqdT0=
	sha1:DFIrtk4SEdlENyTpWpiBSGfDOSE= sha256:5MmbeoVxhFTJLPHVuJVaJss9yCAO2HcPTqozbNG+g5g=
In-Reply-To: <1792660578-1108@newsgrouper.org>
Content-Language: en-US