File: 0023-blocks-annotator_raw-use-set-instead-of-vector-to-av.patch

package info (click to toggle)
gnuradio 3.10.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 41,196 kB
  • sloc: cpp: 191,540; python: 91,856; ansic: 2,292; xml: 999; fortran: 927; sh: 477; makefile: 50
file content (252 lines) | stat: -rw-r--r-- 8,400 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
From f9babbcbc0c67f2fbdda8aed76ffb13bb664a973 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marcus=20M=C3=BCller?= <mmueller@gnuradio.org>
Date: Sun, 23 Feb 2025 00:36:12 +0100
Subject: [PATCH 23/41] blocks/annotator_raw: use set instead of vector to
 avoid sorting
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Signed-off-by: Marcus Müller <mmueller@gnuradio.org>
---
 gr-blocks/lib/annotator_raw_impl.cc         | 45 +++++-----
 gr-blocks/lib/annotator_raw_impl.h          | 27 +++++-
 gr-blocks/python/blocks/qa_annotator_raw.py | 94 +++++++++++++++++++++
 3 files changed, 141 insertions(+), 25 deletions(-)
 create mode 100644 gr-blocks/python/blocks/qa_annotator_raw.py

diff --git a/gr-blocks/lib/annotator_raw_impl.cc b/gr-blocks/lib/annotator_raw_impl.cc
index 4939946d51..d9da9264e3 100644
--- a/gr-blocks/lib/annotator_raw_impl.cc
+++ b/gr-blocks/lib/annotator_raw_impl.cc
@@ -8,12 +8,10 @@
  *
  */
 
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
 
 #include "annotator_raw_impl.h"
 #include <gnuradio/io_signature.h>
+#include <cstdint>
 #include <cstring>
 #include <stdexcept>
 
@@ -42,20 +40,20 @@ void annotator_raw_impl::add_tag(uint64_t offset, pmt::pmt_t key, pmt::pmt_t val
     gr::thread::scoped_lock l(d_mutex);
 
     tag_t tag;
-    tag.srcid = pmt::intern(name());
+    tag.offset = offset;
     tag.key = key;
     tag.value = val;
-    tag.offset = offset;
+    tag.srcid = pmt::intern(name());
 
-    // add our new tag
-    d_queued_tags.push_back(tag);
-    // make sure our tags are in offset order
-    std::sort(d_queued_tags.begin(), d_queued_tags.end(), tag_t::offset_compare);
     // make sure we are not adding an item in the past!
-    if (tag.offset > nitems_read(0)) {
-        throw std::runtime_error(
-            "annotator_raw::add_tag: item added too far in the past.");
+    if (tag.offset < nitems_read(0)) {
+        throw std::runtime_error(fmt::format("annotator_raw::add_tag: item added too far "
+                                             "in the past at {}; we're already at {}.",
+                                             tag.offset,
+                                             nitems_read(0)));
     }
+    // add our new tag
+    d_queued_tags.insert(tag);
 }
 
 int annotator_raw_impl::work(int noutput_items,
@@ -64,25 +62,26 @@ int annotator_raw_impl::work(int noutput_items,
 {
     gr::thread::scoped_lock l(d_mutex);
 
-    const char* in = (const char*)input_items[0];
-    char* out = (char*)output_items[0];
-
     uint64_t start_N = nitems_read(0);
     uint64_t end_N = start_N + (uint64_t)(noutput_items);
 
     // locate queued tags that fall in this range and insert them when appropriate
-    std::vector<tag_t>::iterator i = d_queued_tags.begin();
-    while (i != d_queued_tags.end()) {
-        if ((*i).offset >= start_N && (*i).offset < end_N) {
-            add_item_tag(0, (*i).offset, (*i).key, (*i).value, (*i).srcid);
-            i = d_queued_tags.erase(i);
-        } else {
-            break;
+    const auto lower_bound = d_queued_tags.lower_bound(start_N);
+    if (lower_bound != d_queued_tags.end()) {
+        // at least one element in range
+        const auto upper_bound = d_queued_tags.upper_bound(end_N);
+        for (auto iterator = lower_bound; iterator != upper_bound; ++iterator) {
+            add_item_tag(0, *iterator);
         }
+        if (lower_bound != d_queued_tags.begin()) {
+            d_logger->error("dropping in-past tags");
+        }
+        d_queued_tags.erase(d_queued_tags.begin(), upper_bound);
     }
 
+
     // copy data across
-    memcpy(out, in, noutput_items * d_itemsize);
+    memcpy(output_items[0], input_items[0], noutput_items * d_itemsize);
     return noutput_items;
 }
 
diff --git a/gr-blocks/lib/annotator_raw_impl.h b/gr-blocks/lib/annotator_raw_impl.h
index a17b6ae27c..ac84187ee3 100644
--- a/gr-blocks/lib/annotator_raw_impl.h
+++ b/gr-blocks/lib/annotator_raw_impl.h
@@ -13,6 +13,9 @@
 
 #include <gnuradio/blocks/annotator_raw.h>
 #include <gnuradio/thread/thread.h>
+#include <type_traits>
+#include <set>
+#include <tuple>
 
 namespace gr {
 namespace blocks {
@@ -20,9 +23,29 @@ namespace blocks {
 class annotator_raw_impl : public annotator_raw
 {
 private:
-    const size_t d_itemsize;
-    std::vector<tag_t> d_queued_tags;
+    struct tag_comparator {
+        using is_transparent = std::true_type;
+        //!\brief comparator over all fields in a tag, not just the offset
+        constexpr bool operator()(const tag_t& l, const tag_t& r) const
+        {
+            return std::tie(l.offset, l.key, l.value, l.srcid) <
+                   std::tie(r.offset, r.key, r.value, r.srcid);
+        }
+        constexpr bool operator()(const tag_t& l,
+                                  const decltype(tag_t::offset)& r_offset) const
+        {
+            return l.offset < r_offset;
+        }
+        constexpr bool operator()(const decltype(tag_t::offset)& l_offset,
+                                  const tag_t& r) const
+        {
+            return l_offset < r.offset;
+        }
+    };
+    using tag_container = std::set<tag_t, tag_comparator>;
+    tag_container d_queued_tags;
     gr::thread::mutex d_mutex;
+    const size_t d_itemsize;
 
 public:
     annotator_raw_impl(size_t sizeof_stream_item);
diff --git a/gr-blocks/python/blocks/qa_annotator_raw.py b/gr-blocks/python/blocks/qa_annotator_raw.py
new file mode 100644
index 0000000000..708ee98262
--- /dev/null
+++ b/gr-blocks/python/blocks/qa_annotator_raw.py
@@ -0,0 +1,94 @@
+#!/usr/bin/env python
+#
+# Copyright 2025 Marcus Müller
+#
+# This file is part of GNU Radio
+#
+# SPDX-License-Identifier: GPL-3.0-or-later
+#
+#
+
+
+from gnuradio import gr, gr_unittest
+from gnuradio.blocks import vector_sink_b, vector_source_b, annotator_raw, throttle
+from time import sleep
+import pmt
+
+
+class test_annotator_raw(gr_unittest.TestCase):
+
+    def compare_tag_iterables(self, tags_out: list, tags_in: list, name: str):
+        for tag, ref in zip(tags_out, tags_in):
+            self.assertTupleEqual(
+                (tag.offset, tag.key, tag.value),
+                ref,
+                "tag contents differ",
+            )
+            self.assertEqual(pmt.to_python(tag.srcid), name)
+
+    def setUp(self):
+        self.tb = gr.top_block()
+
+    def tearDown(self):
+        self.tb = None
+
+    def test_001_instantiation(self):
+        blk = annotator_raw(1)
+        self.assertTrue(blk)
+
+    def test_002_preseed(self):
+        N = 1000
+        tags_in = [(n * N, pmt.mp(f"key_{n}"), pmt.from_long(n * 10)) for n in range(N)]
+
+        source = vector_source_b([i % 256 for i in range(N * N)], repeat=False)
+        blk = annotator_raw(gr.sizeof_char)
+        sink = vector_sink_b(reserve_items=N * N)
+        self.tb.connect(source, blk, sink)
+
+        for tag_tuple in tags_in:
+            blk.add_tag(*tag_tuple)
+
+        self.tb.run()
+
+        self.assertEqual(
+            N * N, len(sink.data()), "did not get correct number of samples"
+        )
+
+        tags_out = sink.tags()
+        self.compare_tag_iterables(tags_out, tags_in, blk.name())
+
+    def test_003_late_insertion(self):
+        N = 1000
+        total_time = 0.5
+        tags_in = [
+            (n * N, pmt.mp(f"key_{n}"), pmt.from_long(n * 10)) for n in range(N // 2, N)
+        ]
+
+        source = vector_source_b([i % 256 for i in range(N * N)], repeat=False)
+        slower = throttle(
+            gr.sizeof_char, N * N / total_time, maximum_items_per_chunk=16
+        )
+        blk = annotator_raw(gr.sizeof_char)
+        sink = vector_sink_b(reserve_items=N * N)
+        self.tb.connect(source, slower, blk, sink)
+
+        self.tb.start()
+        sleep(0.4 * total_time)
+        # we should be a fair bit into the input, but not yet halfway through
+        for tag_tuple in tags_in:
+            blk.add_tag(*tag_tuple)
+        self.assertRaises(
+            RuntimeError, lambda: blk.add_tag(0, pmt.PMT_NIL, pmt.PMT_NIL)
+        )
+        self.tb.wait()
+
+        self.assertEqual(
+            N * N, len(sink.data()), "did not get correct number of samples"
+        )
+
+        tags_out = sink.tags()
+        self.compare_tag_iterables(tags_out, tags_in, blk.name())
+
+
+if __name__ == "__main__":
+    gr_unittest.run(test_annotator_raw)
-- 
2.47.3