kaspa-notify 0.15.0

Kaspa notification subsystem
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use crate::{
    address::tracker::Tracker,
    listener::ListenerId,
    subscription::{
        single::{UtxosChangedState, UtxosChangedSubscription},
        DynSubscription,
    },
};
use std::{ops::Deref, sync::Arc};

#[cfg(test)]
use kaspa_addresses::Address;

#[derive(Debug)]
pub struct SubscriptionContextInner {
    pub address_tracker: Tracker,
    pub utxos_changed_subscription_to_all: DynSubscription,
}

impl SubscriptionContextInner {
    const CONTEXT_LISTENER_ID: ListenerId = ListenerId::MAX;

    pub fn new() -> Self {
        Self::with_options(None)
    }

    pub fn with_options(max_addresses: Option<usize>) -> Self {
        let address_tracker = Tracker::new(max_addresses);
        let utxos_changed_subscription_all =
            Arc::new(UtxosChangedSubscription::new(UtxosChangedState::All, Self::CONTEXT_LISTENER_ID));
        Self { address_tracker, utxos_changed_subscription_to_all: utxos_changed_subscription_all }
    }

    #[cfg(test)]
    pub fn with_addresses(addresses: &[Address]) -> Self {
        let address_tracker = Tracker::with_addresses(addresses);
        let utxos_changed_subscription_all =
            Arc::new(UtxosChangedSubscription::new(UtxosChangedState::All, Self::CONTEXT_LISTENER_ID));
        Self { address_tracker, utxos_changed_subscription_to_all: utxos_changed_subscription_all }
    }
}

impl Default for SubscriptionContextInner {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Clone, Debug, Default)]
pub struct SubscriptionContext {
    inner: Arc<SubscriptionContextInner>,
}

impl SubscriptionContext {
    pub fn new() -> Self {
        Self::with_options(None)
    }

    pub fn with_options(max_addresses: Option<usize>) -> Self {
        let inner = Arc::new(SubscriptionContextInner::with_options(max_addresses));
        Self { inner }
    }

    #[cfg(test)]
    pub fn with_addresses(addresses: &[Address]) -> Self {
        let inner = Arc::new(SubscriptionContextInner::with_addresses(addresses));
        Self { inner }
    }
}

impl Deref for SubscriptionContext {
    type Target = SubscriptionContextInner;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        address::tracker::{CounterMap, Index, IndexSet, Indexer, RefCount},
        subscription::SubscriptionContext,
    };
    use itertools::Itertools;
    use kaspa_addresses::{Address, Prefix};
    use kaspa_alloc::init_allocator_with_default_settings;
    use kaspa_core::trace;
    use kaspa_math::Uint256;
    use std::collections::{HashMap, HashSet};
    use workflow_perf_monitor::mem::get_process_memory_info;

    fn create_addresses(count: usize) -> Vec<Address> {
        (0..count)
            .map(|i| Address::new(Prefix::Mainnet, kaspa_addresses::Version::PubKey, &Uint256::from_u64(i as u64).to_le_bytes()))
            .collect()
    }

    fn measure_consumed_memory<T, F: FnOnce() -> Vec<T>, F2: FnOnce(&T) -> (usize, usize)>(
        item_len: usize,
        num_items: usize,
        ctor: F,
        length_and_capacity: F2,
    ) -> Vec<T> {
        let before = get_process_memory_info().unwrap();

        trace!("Creating items...");
        let items = ctor();

        let after = get_process_memory_info().unwrap();

        trace!("Required item length: {}", item_len);
        trace!("Memory consumed: {}", (after.resident_set_size - before.resident_set_size) / num_items as u64);
        trace!(
            "Memory/idx: {}",
            ((after.resident_set_size - before.resident_set_size) as f64 / num_items as f64 / item_len as f64 * 10.0).round() / 10.0
        );

        let (len, capacity) = length_and_capacity(&items[0]);
        match len > 0 {
            true => trace!(
                "Actual item: len = {}, capacity = {}, free space = +{:.1}%",
                len,
                capacity,
                (capacity - len) as f64 * 100.0 / len as f64
            ),
            false => trace!("Actual item: len = {}, capacity = {}", len, capacity),
        }

        items
    }

    fn init_and_measure_consumed_memory<T, F: FnOnce() -> Vec<T>, F2: FnOnce(&T) -> (usize, usize)>(
        item_len: usize,
        num_items: usize,
        ctor: F,
        length_and_capacity: F2,
    ) -> Vec<T> {
        init_allocator_with_default_settings();
        kaspa_core::log::try_init_logger("INFO,kaspa_notify::subscription::context=trace");
        measure_consumed_memory(item_len, num_items, ctor, length_and_capacity)
    }

    #[test]
    #[ignore = "measuring consumed memory"]
    // ITEM = SubscriptionContext
    // (measuring IndexMap<ScriptPublicKey, u16>)
    //
    //   ITEM_LEN    NUM_ITEMS     MEMORY/ITEM   MEM/ADDR
    // --------------------------------------------------
    // 10_000_000            5   1_098_744_627      109.9
    //  1_000_000           50     103_581_696      104.0
    //    100_000          100       9_157_836       91.6
    //     10_000        1_000         977_666       97.8
    //      1_000       10_000          94_633       94.6
    //        100      100_000           9_617       96.2
    //         10    1_000_000           1_325      132.5
    //          1   10_000_000             410      410.0
    fn test_subscription_context_size() {
        const ITEM_LEN: usize = 10_000_000;
        const NUM_ITEMS: usize = 5;

        init_allocator_with_default_settings();
        kaspa_core::log::try_init_logger("INFO,kaspa_notify::subscription::context=trace");

        trace!("Creating addresses...");
        let addresses = create_addresses(ITEM_LEN);

        let _ = measure_consumed_memory(
            ITEM_LEN,
            NUM_ITEMS,
            || (0..NUM_ITEMS).map(|_| SubscriptionContext::with_addresses(&addresses)).collect_vec(),
            |x| (x.address_tracker.len(), x.address_tracker.capacity()),
        );
    }

    #[test]
    #[ignore = "measuring consumed memory"]
    // ITEM = HashMap<u32, u16>
    //
    //   ITEM_LEN    NUM_ITEMS     MEMORY/ITEM    MEM/IDX
    // --------------------------------------------------
    // 10_000_000           10     151_214_489       15.1
    //  1_000_000          100      18_926_059       18.9
    //    100_000        1_000       1_187_864       11.9
    //     10_000       10_000         152_063       15.2
    //      1_000      100_000          20_576       20.6
    //        100    1_000_000           1_336       13.4
    //         10   10_000_000             241       24.1
    //          1   10_000_000             128      128.4
    fn test_hash_map_u32_u16_size() {
        const ITEM_LEN: usize = 1;
        const NUM_ITEMS: usize = 10_000_000;

        let _ = init_and_measure_consumed_memory(
            ITEM_LEN,
            NUM_ITEMS,
            || {
                (0..NUM_ITEMS)
                    .map(|_| (0..ITEM_LEN as Index).map(|i| (i, (ITEM_LEN as Index - i) as RefCount)).rev().collect::<HashMap<_, _>>())
                    .collect_vec()
            },
            |x| (x.len(), x.capacity()),
        );
    }

    #[test]
    #[ignore = "measuring consumed memory"]
    // ITEM = CounterMap
    // (measuring HashMap<u32, u16>)
    //
    //   ITEM_LEN    NUM_ITEMS     MEMORY/ITEM    MEM/IDX
    // --------------------------------------------------
    // 10_000_000           10     151_239_065       15.1
    //  1_000_000          100      18_927_534       18.9
    //    100_000        1_000       1_188_024       11.9
    //     10_000       10_000         152_077       15.2
    //      1_000      100_000          20_587       20.6
    //        100    1_000_000           1_344       13.4
    //         10   10_000_000             249       24.9
    //          1   10_000_000             136      136.5
    fn test_counter_map_size() {
        const ITEM_LEN: usize = 10;
        const NUM_ITEMS: usize = 10_000_000;

        let _ = init_and_measure_consumed_memory(
            ITEM_LEN,
            NUM_ITEMS,
            || {
                (0..NUM_ITEMS)
                    .map(|_| {
                        // Reserve the required capacity
                        // Note: the resulting allocated HashMap bucket count is (capacity * 8 / 7).next_power_of_two()
                        let mut item = CounterMap::with_capacity(ITEM_LEN);

                        (0..ITEM_LEN as Index).for_each(|x| {
                            item.insert(x);
                        });
                        item
                    })
                    .collect_vec()
            },
            |x| (x.len(), x.capacity()),
        );
    }

    #[test]
    #[ignore = "measuring consumed memory"]
    // ITEM = HashSet<u32>
    //
    //   ITEM_LEN    NUM_ITEMS     MEMORY/ITEM    MEM/IDX
    // --------------------------------------------------
    // 10_000_000           10      84'094'976        8.4
    //  1_000_000          100      10'524'508       10.5
    //    100_000        1_000         662_720        6.6
    //     10_000       10_000          86_369        8.6
    //      1_000      100_000          12_372       12.4
    //        100    1_000_000             821        8.2
    //         10   10_000_000             144       14.4
    //          1   10_000_000             112      112.0
    fn test_hash_set_u32_size() {
        const ITEM_LEN: usize = 1_000_000;
        const NUM_ITEMS: usize = 100;

        let _ = init_and_measure_consumed_memory(
            ITEM_LEN,
            NUM_ITEMS,
            || (0..NUM_ITEMS).map(|_| (0..ITEM_LEN as Index).rev().collect::<HashSet<_>>()).collect_vec(),
            |x| (x.len(), x.capacity()),
        );
    }

    #[test]
    #[ignore = "measuring consumed memory"]
    // ITEM = HashSet<u32> emptied
    //
    //   ITEM_LEN    NUM_ITEMS     MEMORY/ITEM    MEM/IDX
    // --------------------------------------------------
    // 10_000_000           10      84'094'976        8.4
    //  1_000_000          100      10'524'508       10.5
    //    100_000        1_000         662_720        6.6
    //     10_000       10_000          86_369        8.6
    //      1_000      100_000          12_372       12.4
    //        100    1_000_000             821        8.2
    //         10   10_000_000             144       14.4
    //          1   10_000_000             112      112.0
    fn test_emptied_hash_set_u32_size() {
        const ITEM_LEN: usize = 1_000_000;
        const NUM_ITEMS: usize = 100;

        let _ = init_and_measure_consumed_memory(
            ITEM_LEN,
            NUM_ITEMS,
            || {
                (0..NUM_ITEMS)
                    .map(|_| {
                        let mut set = (0..ITEM_LEN as Index).rev().collect::<HashSet<_>>();
                        let original_capacity = set.capacity();
                        let _ = set.drain();
                        assert!(set.is_empty());
                        assert_eq!(original_capacity, set.capacity());
                        set
                    })
                    .collect_vec()
            },
            |x| (x.len(), x.capacity()),
        );
    }

    #[test]
    #[ignore = "measuring consumed memory"]
    // ITEM = IndexSet
    // (measuring HashSet<u32>)
    //
    //   ITEM_LEN    NUM_ITEMS     MEMORY/ITEM    MEM/IDX
    // --------------------------------------------------
    // 10_000_000           10      84_119_961        8.4
    //  1_000_000          100      10_526_720       10.5
    //    100_000        1_000         662_974        6.6
    //     10_000       10_000          86_424        8.6
    //      1_000      100_000          12_381       12.4
    //        100    1_000_000             830        8.3
    //         10   10_000_000             152       15.2
    //          1   10_000_000             120      120.0
    fn test_index_set_size() {
        const ITEM_LEN: usize = 10_000_000;
        const NUM_ITEMS: usize = 10;

        let _ = init_and_measure_consumed_memory(
            ITEM_LEN,
            NUM_ITEMS,
            || {
                (0..NUM_ITEMS)
                    .map(|_| {
                        // Reserve the required capacity
                        // Note: the resulting allocated HashSet bucket count is (capacity * 8 / 7).next_power_of_two()
                        let mut item = IndexSet::with_capacity(ITEM_LEN);

                        (0..ITEM_LEN as Index).for_each(|x| {
                            item.insert(x);
                        });
                        item
                    })
                    .collect_vec()
            },
            |x| (x.len(), x.capacity()),
        );
    }

    #[test]
    #[ignore = "measuring consumed memory"]
    // ITEM = Vec<u32>
    //
    //   ITEM_LEN    NUM_ITEMS     MEMORY/ITEM    MEM/IDX
    // --------------------------------------------------
    // 10_000_000           10      40_208_384        4.0
    //  1_000_000          100       4_026_245        4.0
    //    100_000        1_000         403_791        4.0
    //     10_000       10_000          41_235        4.1
    //      1_000      100_000           4_141        4.1
    //        100    1_000_000             478        4.8
    //         10   10_000_000              72        7.2
    //          1   10_000_000              32       32.0
    fn test_vec_u32_size() {
        const ITEM_LEN: usize = 10_000_000;
        const NUM_ITEMS: usize = 10;

        let _ = init_and_measure_consumed_memory(
            ITEM_LEN,
            NUM_ITEMS,
            || (0..NUM_ITEMS).map(|_| (0..ITEM_LEN as Index).collect::<Vec<_>>()).collect_vec(),
            |x| (x.len(), x.capacity()),
        );
    }
    // #[test]
    // #[ignore = "measuring consumed memory"]
    // // ITEM = DashSet
    // // (measuring DashSet<u32>)
    // //
    // //   ITEM_LEN    NUM_ITEMS     MEMORY/ITEM    MEM/IDX
    // // --------------------------------------------------
    // // 10_000_000           10      96_439_500        9.6
    // //  1_000_000          100      11_942_010       11.9
    // //    100_000        1_000         826_400        8.3
    // //     10_000       10_000         107_060       10.7
    // //      1_000      100_000          19_114       19.1
    // //        100    1_000_000          12_717      127.2
    // //         10    1_000_000           8_865      886.5
    // //          1    1_000_000           8_309     8309.0
    // fn test_dash_set_size() {
    //     const ITEM_LEN: usize = 1;
    //     const NUM_ITEMS: usize = 1_000_000;

    //     init_allocator_with_default_settings();
    //     kaspa_core::log::try_init_logger("INFO,kaspa_notify::subscription::context=trace");

    //     let before = get_process_memory_info().unwrap();
    //     trace!("Creating sets...");
    //     let sets = (0..NUM_ITEMS)
    //         .map(|_| {
    //             // Rely on organic growth rather than pre-defined capacity
    //             let item = DashSet::new();
    //             (0..ITEM_LEN as Index).for_each(|x| {
    //                 item.insert(x);
    //             });
    //             item
    //         })
    //         .collect_vec();

    //     let after = get_process_memory_info().unwrap();
    //     trace!("Set length: {}", sets[0].len());
    //     trace!("Memory consumed: {}", (after.resident_set_size - before.resident_set_size) / NUM_ITEMS as u64);
    // }
}