Skip to content

Add PyDict_AsFrozenDictAndClear public API #109

Description

@sobolevn

Discussion: https://discuss.python.org/t/provide-a-c-api-to-efficently-convert-dict-into-frozendict/107754/10
Issue: python/cpython#153410
PR: python/cpython#153413

Discussion summary:

  • Currently, there's no clean way to create a frozendict instances from C code. We faced this in msgspec: Add frozendict support for 3.15+ msgspec/msgspec#1052 Since this is a project focused on performance, we don't quite like to use PyFrozenDict_New for this. Because it copies the dict we already have once again. It works for O(n) and uses more memory than it actually needs to
  • New proposed API solves this by coping existing keys and values from any dict with O(1) complexity. Which is much faster
  • All people in the discussion agreed that such API would be nice to have
  • Most people in the discussion agreed that the taking an existing dict and transforming it to frozendict seems like a way to go

So, here it is :)
Looking forward to hearing your feedback!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions