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

**URL:** <https://discuss.python.org/t/provide-a-c-api-to-efficently-convert-dict-into-frozendict/107754>\
**Category:** C API\
**Created:** [June 13, 2026, 10:27am UTC](https://discuss.python.org/t/provide-a-c-api-to-efficently-convert-dict-into-frozendict/107754 "2026-06-13T10:27:10Z")\
**Posts on this page:** 1\
**Showing post:** 27

<div class="post-metadata">

**Author:** ![methane](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/methane/32/43_2.png) [@methane](https://discuss.python.org/u/methane)\
**Post date:** [July 16, 2026, 8:20am UTC](https://discuss.python.org/t/provide-a-c-api-to-efficently-convert-dict-into-frozendict/107754/27 "2026-07-16T08:20:42Z")

</div>

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.

---

_[View the full topic](https://discuss.python.org/t/provide-a-c-api-to-efficently-convert-dict-into-frozendict/107754)._
