22
33#pragma once
44
5- #include " cinderx/Common/log.h"
65#include " cinderx/Common/ref.h"
76#include " cinderx/Common/sorted_vec_map.h"
87
9- #include < algorithm>
108#include < memory>
119
1210namespace cinderx ::jit::hir {
@@ -26,55 +24,19 @@ namespace cinderx::jit::hir {
2624// for correctness).
2725class AnnotationIndex {
2826 public:
29- // Retrieve the annotation for the given name, or return nullptr. Pure C++;
30- // safe to call with the GIL released.
31- //
32- // annotations_ is sorted by key pointer (via std::less<Ref<>>), so this is a
33- // binary search by pointer identity. It deliberately does not call
34- // SortedVecMap::find, which would require constructing an owning Ref<> and
35- // thus touch the Python C-API.
36- BorrowedRef<> find (PyObject* name) const {
37- JIT_DCHECK (
38- reinterpret_cast <PyASCIIObject*>(name)->state .interned != 0 ,
39- " should be interned" );
40- auto it = std::lower_bound (
41- annotations_.begin (),
42- annotations_.end (),
43- name,
44- [](const auto & entry, PyObject* rhs) {
45- return entry.first .get () < rhs;
46- });
47- if (it != annotations_.end () && it->first .get () == name) {
48- return it->second .get ();
49- }
50- return nullptr ;
51- }
52-
5327 static std::unique_ptr<AnnotationIndex> fromFunction (
5428 BorrowedRef<PyFunctionObject> func);
5529
30+ // Retrieve the annotation for the given name, or return nullptr. Pure C++;
31+ // safe to call with the GIL released.
32+ BorrowedRef<> find (BorrowedRef<> name) const ;
33+
5634 private:
5735 // Built from the flattened (name, annotation, ...) tuple used before 3.14.
58- explicit AnnotationIndex (BorrowedRef<PyTupleObject> annotations)
59- : owner_(Ref<>::create(annotations.getObj())) {
60- Py_ssize_t size = PyTuple_GET_SIZE (annotations.get ());
61- for (Py_ssize_t index = 0 ; index + 1 < size; index += 2 ) {
62- BorrowedRef<> key = PyTuple_GET_ITEM (annotations.get (), index);
63- BorrowedRef<> value = PyTuple_GET_ITEM (annotations.get (), index + 1 );
64- annotations_.emplace (Ref<>::create (key), Ref<>::create (value));
65- }
66- }
36+ explicit AnnotationIndex (BorrowedRef<PyTupleObject> annotations);
6737
6838 // Built from the __annotations__ dict used on 3.14+.
69- explicit AnnotationIndex (BorrowedRef<PyDictObject> dict)
70- : owner_(Ref<>::create(dict.getObj())) {
71- PyObject* key = nullptr ;
72- PyObject* value = nullptr ;
73- Py_ssize_t pos = 0 ;
74- while (PyDict_Next (owner_, &pos, &key, &value)) {
75- annotations_.emplace (Ref<>::create (key), Ref<>::create (value));
76- }
77- }
39+ explicit AnnotationIndex (BorrowedRef<PyDictObject> dict);
7840
7941 Ref<> owner_;
8042 SortedVecMap<Ref<>, Ref<>> annotations_;
0 commit comments