Data-TopK-Shared

 view release on metacpan or  search on metacpan

topk.h  view on Meta::CPAN

    uint64_t b = hv & h->hash_mask;
    uint32_t cur = buckets[b];
    if (cur == s) { buckets[b] = tk_slot(h, s)->hnext; return; }
    uint64_t guard = 0;
    while (cur != TK_NIL && TK_SLOT_OK(h, cur) && guard++ <= (uint64_t)h->capacity) {
        TkSlot *c = tk_slot(h, cur);
        if (c->hnext == s) { c->hnext = tk_slot(h, s)->hnext; return; }
        cur = c->hnext;
    }
}

/* store (truncated) key bytes into slot s; returns the stored length */
static inline uint32_t tk_store_key(TkHandle *h, uint32_t s, const void *key, size_t len) {
    uint32_t klen = (len > h->key_size) ? h->key_size : (uint32_t)len;
    TkSlot *sl = tk_slot(h, s);
    memcpy(tk_slot_key(sl), key, klen);
    sl->key_len = klen;
    return klen;
}

#define TK_RS_PEND (1ULL << 63)

/* Rescale the slots from hdr->rs_next on by hdr->rs_inv, then advance the landmark.
 * Each slot's new values go to the header first, so a rescale killed part-way is
 * resumed without scaling any slot twice. */
static void tk_rescale_run(TkHandle *h) {
    TkHeader *hdr = h->hdr;
    uint64_t used = tk_heap_size(h);
    uint64_t smax = tk_slots_max(h);
    if (used > smax) used = smax;                     /* Layer B */
    uint64_t i = hdr->rs_next;
    if (i & TK_RS_PEND) {
        i &= ~TK_RS_PEND;
        if (i < used) { tk_slot(h, i)->count = hdr->rs_count; tk_slot(h, i)->error = hdr->rs_error; }
        __atomic_signal_fence(__ATOMIC_SEQ_CST);
        hdr->rs_next = ++i;
    }
    double inv = hdr->rs_inv;
    for (; i < used; i++) {
        TkSlot *sl = tk_slot(h, i);
        hdr->rs_count = tk_w_bits(tk_w_get(sl->count) * inv);
        hdr->rs_error = tk_w_bits(tk_w_get(sl->error) * inv);
        __atomic_signal_fence(__ATOMIC_SEQ_CST);
        hdr->rs_next = i | TK_RS_PEND;
        __atomic_signal_fence(__ATOMIC_SEQ_CST);
        sl->count = hdr->rs_count;
        sl->error = hdr->rs_error;
        __atomic_signal_fence(__ATOMIC_SEQ_CST);
        hdr->rs_next = i + 1;
    }
    __atomic_signal_fence(__ATOMIC_SEQ_CST);
    hdr->landmark = hdr->now;                         /* g(now) == 1 again */
    __atomic_signal_fence(__ATOMIC_SEQ_CST);
    hdr->rs_on = 0;
}

/* Rescale all weights by 1/g and advance the landmark to now, keeping the
 * forward-decay weights bounded (decayed mode; caller holds the write lock). */
static void tk_rescale(TkHandle *h) {
    double g = tk_g(h);
    if (!(g > 1.0)) { h->hdr->landmark = h->hdr->now; return; }   /* g <= 1 or NaN: nothing to rescale */
    h->hdr->rs_inv  = isfinite(g) ? 1.0 / g : 0.0;   /* g == +Inf: a huge time gap fully decayed all old weight -> 0 */
    h->hdr->rs_next = 0;
    __atomic_signal_fence(__ATOMIC_SEQ_CST);
    h->hdr->rs_on = 1;
    __atomic_signal_fence(__ATOMIC_SEQ_CST);
    tk_rescale_run(h);
}

/* Observe one item; returns the item's estimated count after this observation.
 * (caller holds the write lock) */
static uint64_t tk_observe_locked(TkHandle *h, const void *item, size_t len, int has_ts, double ts) {
    if (h->capacity == 0) return 0;                        /* Layer B: unusable mapping */
    uint32_t klen = (len > h->key_size) ? h->key_size : (uint32_t)len;
    uint64_t hv = tk_hash(item, klen);
    h->hdr->seen++;

    double g = 1.0;                                        /* increment weight (1 in plain mode) */
    if (h->mode == TK_MODE_DECAYED) {
        /* advance the monotonic clock: to ts if given and larger, else one tick */
        if (has_ts) { if (ts > h->hdr->now) h->hdr->now = ts; }
        else h->hdr->now += 1.0;
        if (h->alpha * (h->hdr->now - h->hdr->landmark) > TK_RESCALE_LN)
            tk_rescale(h);                                 /* advance the landmark to keep g bounded */
        g = tk_g(h);
    }

    uint32_t s = tk_hash_find(h, item, klen, hv);
    uint32_t *heap = tk_heap(h);
    if (s != TK_NIL) {                                     /* already monitored: bump */
        TkSlot *sl = tk_slot(h, s);
        uint64_t nc = (h->mode == TK_MODE_DECAYED) ? tk_w_bits(tk_w_get(sl->count) + g) : sl->count + 1;
        uint64_t size = tk_heap_size(h);
        uint64_t pos = sl->heap_pos;
        int sink = pos < size && tk_must_sink(h, heap, size, pos, nc);
        uint32_t at = sink ? heap[pos] : TK_NIL;
        if (sink) tk_journal_begin(h, at, TK_J_DOWN, pos);
        sl->count = nc;
        if (sink) tk_sift_down(h, heap, size, pos, at);   /* count rose -> sink it */
        return sl->count;
    }

    uint64_t used_now = h->hdr->used;                     /* Layer B: read the peer-writable count ONCE */
    if (used_now < h->capacity) {                         /* warm-up: fill a fresh slot */
        uint32_t ns = (uint32_t)used_now;                 /* index by the same value the guard validated */
        TkSlot *sl = tk_slot(h, ns);
        tk_store_key(h, ns, item, len);
        sl->count = (h->mode == TK_MODE_DECAYED) ? tk_w_bits(g)   : 1;
        sl->error = (h->mode == TK_MODE_DECAYED) ? tk_w_bits(0.0) : 0;
        sl->hnext = TK_NIL;
        sl->heap_pos = ns;
        heap[ns] = ns;
        uint32_t sp = ns ? heap[(ns - 1) / 2] : TK_NIL;
        int rise = TK_SLOT_OK(h, sp) && tk_slot_lt(h, sl, tk_slot(h, sp));
        if (rise) tk_journal_begin(h, ns, TK_J_UP, ns);
        /* Claim the slot before indexing it: a writer killed in between leaves an unindexed slot
         * that eviction reclaims, not one the next key reuses while it is still chained. */
        __atomic_signal_fence(__ATOMIC_SEQ_CST);
        h->hdr->used++;
        __atomic_signal_fence(__ATOMIC_SEQ_CST);
        tk_hash_insert(h, ns, hv);



( run in 0.803 second using v1.01-cache-2.11-cpan-60884a8df76 )