Skip to content

Commit 692e095

Browse files
alexmalyshevmeta-codesync[bot]
authored andcommitted
Move AnnotationIndex implementation to .cpp
Summary: Just a refactor, setting up for next diff. Reviewed By: yoney Differential Revision: D114023459 fbshipit-source-id: afa2c02876e44ce0f1fc150599f913ccb1b12eee
1 parent bb2ff91 commit 692e095

2 files changed

Lines changed: 50 additions & 44 deletions

File tree

cinderx/Jit/hir/annotation_index.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
#include "cinderx/Jit/hir/annotation_index.h"
44

5+
#include "cinderx/Common/log.h"
56
#include "cinderx/Jit/config.h"
67

8+
#include <algorithm>
9+
710
namespace cinderx::jit::hir {
811

912
std::unique_ptr<AnnotationIndex> AnnotationIndex::fromFunction(
@@ -30,4 +33,45 @@ std::unique_ptr<AnnotationIndex> AnnotationIndex::fromFunction(
3033
return nullptr;
3134
}
3235

36+
BorrowedRef<> AnnotationIndex::find(BorrowedRef<> name) const {
37+
JIT_DCHECK(
38+
reinterpret_cast<PyASCIIObject*>(name.getObj())->state.interned != 0,
39+
"should be interned");
40+
// annotations_ is sorted by key pointer (via std::less<Ref<>>), so this is a
41+
// binary search by pointer identity. It deliberately does not call
42+
// SortedVecMap::find, which would require constructing an owning Ref<> and
43+
// thus touch the Python C-API.
44+
auto it = std::lower_bound(
45+
annotations_.begin(),
46+
annotations_.end(),
47+
name,
48+
[](const auto& entry, PyObject* rhs) { return entry.first.get() < rhs; });
49+
if (it != annotations_.end() && it->first.get() == name) {
50+
return it->second.get();
51+
}
52+
return nullptr;
53+
}
54+
55+
// Built from the flattened (name, annotation, ...) tuple used before 3.14.
56+
AnnotationIndex::AnnotationIndex(BorrowedRef<PyTupleObject> annotations)
57+
: owner_(Ref<>::create(annotations.getObj())) {
58+
Py_ssize_t size = PyTuple_GET_SIZE(annotations.get());
59+
for (Py_ssize_t index = 0; index + 1 < size; index += 2) {
60+
BorrowedRef<> key = PyTuple_GET_ITEM(annotations.get(), index);
61+
BorrowedRef<> value = PyTuple_GET_ITEM(annotations.get(), index + 1);
62+
annotations_.emplace(Ref<>::create(key), Ref<>::create(value));
63+
}
64+
}
65+
66+
// Built from the __annotations__ dict used on 3.14+.
67+
AnnotationIndex::AnnotationIndex(BorrowedRef<PyDictObject> dict)
68+
: owner_(Ref<>::create(dict.getObj())) {
69+
PyObject* key = nullptr;
70+
PyObject* value = nullptr;
71+
Py_ssize_t pos = 0;
72+
while (PyDict_Next(owner_, &pos, &key, &value)) {
73+
annotations_.emplace(Ref<>::create(key), Ref<>::create(value));
74+
}
75+
}
76+
3377
} // namespace cinderx::jit::hir

cinderx/Jit/hir/annotation_index.h

Lines changed: 6 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
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

1210
namespace cinderx::jit::hir {
@@ -26,55 +24,19 @@ namespace cinderx::jit::hir {
2624
// for correctness).
2725
class 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

Comments
 (0)