Progressing seems popular This topic is on improvements to the engine and asking people for suggestions and testing.
I’ve pushed a number of commits that introduce thread local atoms/blobs. It started with the observation that, while blobs are a neat and safe way to encapsulate references to foreign resources, they tend to stress the global garbage collector and the conservative nature of SWI-Prolog’s AGC (Atom Garbage Collector) can cause foreign resources to leak. So, I started discussing with Claude with the idea to get a cheaper thread-local alternative that can be promoted to a shared blob for the case we want to share a resource between blobs. After some rounds, and trials, this resulted in thread-local atoms. The idea is
The atom array is split in chunks of 64 atoms that are reserved for a specific thread. That alone is an advantage that was discovered while timing multi-threaded tabling. Before, subsequent atoms (trie blobs) had the tendency to get interleaved over multiple threads, causing a lot of contention.
An atom/blob starts as thread-local. It is part of the shared atom table, but has a flag and using the above chunks we can figure out the owner thread. The thread itself runs synchronous (and thus precise) AGC using similar rules as AGC. It it stays local, only a simple atomic synchronization is needed to discard it (also used by shared AGC).
If the atom is added to a global resource (e.g, a clause), PL_register_atom() makes it global. This merely clears the local flag and hands the atom from the threads local AGC to the global AGC. This mechanism also ensures that an atom that is passed to another thread using a clause, record or message is made global.
Unique atoms (most of them, i.e., where the atom handle has a one-to-one mapping to the represented value) are made global at the moment a second thread tries to create the atom: it finds the local atom (which is also in the global atom table) and makes it global.
This mechanism now covers streams, clause and record references (clause/3, asserta/2), tries (tables), undo/1 terms and … text atoms for e.g., read/1, atom_concat/3 and any foreign predicate using PL_unify_chars() or one of its variations (as opposed to PL_new_atom() && PL_unify_atom(), i.e., PL_new_atom() creates a shared atom). This extents into thread_local/1 predicates, so reading terms and asserting them into a thread local dynamic predicate keeps all the atoms owned by the thread. Thread-local predicates now also use thread-local clause garbage collection.
In addition, the xpce, jpl and Janus packages now use this mechanism to reference foreign objects.
My timing shows mild improvements on single threaded workloads and serious improvements on multi-threaded workloads (up to 4 times better for JPL). Far fewer calls to global AGC and much better reclamation of foreign resources because (a) local AGC is much cheaper and thus can run more frequently and (b) it is precise.
All this has few user visible consequences. If you use blobs in your foreign code you can use the blob registration flag PL_BLOB_LOCAL and you should read the updated manual sections. C/C++ code needs no changes except that PL_get_atom()can give you a local atom. All is fine if you just communicate the atom_t to the same thread or store it and use PL_register_atom() (as required also before this). You cannot pass this atom_t to another thread or engine though. If you need to do that, you must use PL_register_atom(), PL_put_atom() (or a similar function to store it in a term_t) and PL_unregister_atom(). Predicates such as current_atom/1 or current_blob/2 return only shared atoms/blobs and blobs owned by the calling thread.
All the coding and testing is done by Claude. It found several pre-existing race conditions and other bugs that are fixed as well. Design was cooperative. Claude came with the idea of local atoms but got stuck in the design several times where I could come up with solutions.
The second step is there. This cleans up the data tags. That was quite a mess with remains of 32-bit limits (Prolog cells are since quite a while 64 bits, also on 32-bit hardware) and using stack offsets rather than direct addressing. I had planned to do this for years, expecting both simplification and speedup. With AI, this was an easy refactor process. It got the simplification and room for new tags, but the expected speedup is a meagre 1-2% It brought some pros and cons in the process
cons
tagged integers (i.e., cheap ones) are now limited to \pm2^{55} (was 2^{56})
The number of atoms on 32 bit hardware is now limited to 16M (was 32M). This can probably be extended to pretty much unlimited as it is on 64 bit hardware.
pros
A lot of code simplifications. Notably making the distinction between small integers, big integers and rational numbers was a mess.
Performance 1-2% better
Pointers exchanged using PL_unify_pointer() now reach Prolog as an integer (as it was) with additional type info that it is an address. PL_get_pointer() only accepts such “address integers”. Using arithmetic on such an integer looses the address property. This makes using pointers the classical way (as opposed to using the safe but more expensive blobs) safer. There is a new type test is_address(@Term) for these things.
term_hash/2 needed changes, which was the right moment to fix it for once and forever: it now produces consistent hashes across all platforms (big/little endian, 32/64 bits, WASM and GMP/LibBF).
Please test. I’ve build and tested it on Linux, MacOS, Windows, WASM, single threaded and Alpine on s390x (emulated).
Questions
Should we try to raise the 16M atom limit on 32-bit (mostly used for WASM and small single board computers)?
The trick used with addresses could be repeated for character codes, i.e., atom_codes/2 could produce such extra-tagged integers. Of course, for compatibility, normal integers must be accepted as input for all predicates that accept integers. Such an approach however would cost nothing and disambiguate many integer lists from code lists, allowing for better printing. Is that worth considering?