Provide a C-API to efficently convert `dict` into `frozendict`

It is important that the construction of an immutable object be observable atomically from other threads. This makes it easier to access immutable objects concurrently from multiple threads.
And PyDict_AsFrozenDictAndClear() satisfies this condition. The frozendict constructed by PyDict_AsFrozenDictAndClear() is not accessible from other threads during construction.

I agree that it is preferable for the dict passed to PyDict_AsFrozenDictAndClear() not to be observed by other threads through the GC, but that is not a necessary condition. It does not result in a TSan error, nor does it cause Python to segfault.

Regarding the Writer (or Builder) API, I am concerned about whether it can achieve performance at least as good as PyDict_AsFrozenDictAndClear(), and whether losing the ability to use APIs such as PyDict_ContainsString() and PyDict_SetDefault() would limit its use cases.

For example, a JSON parser might want to report an error immediately when it encounters a duplicate key, and create a frozendict only after successfully reaching the end of the object.
An HTTP header parser might want to concatenate the value strings when it encounters duplicate keys.
Also, when constructing a Python dict from a mapping type in a language other than Python, an API such as PyDict_FromItems() might provide better performance if calls to the Python/C API introduce overhead.

In any case, I still do not know enough about the actual use cases for frozendict. I would like to hear requests from multiple projects besides msgspec as well.

2 Likes