Data-TopK-Shared

 view release on metacpan or  search on metacpan

Shared.xs  view on Meta::CPAN

    EXTRACT(self);
    STRLEN n;
    const char *s;
    int has_ts; double ts = 0.0, g = 1.0; uint64_t raw;
  CODE:
    /* Resolve the optional timestamp's magic FIRST: a tied/overloaded timestamp
     * runs arbitrary Perl in FETCH/numify that could realloc or free item's SV
     * buffer. Capture item's bytes LAST so nothing runs between the capture and
     * the locked use (add_many guards the same hazard by copying to a mortal). */
    has_ts = (SvGETMAGIC(timestamp), SvOK(timestamp));   /* optional timestamp for decayed mode */
    if (has_ts) { ts = (double)SvNV(timestamp); if (!isfinite(ts)) has_ts = 0; }   /* ignore Inf/NaN -> per-add tick */
    s = SvPVbyte(item, n);                 /* may croak (wide char) -- BEFORE the lock */
    REEXTRACT(self);
    tk_rwlock_wrlock(h);
    raw = tk_observe_locked(h, s, n, has_ts, ts);
    if (h->mode == TK_MODE_DECAYED) g = tk_g(h);
    h->hdr->stat_ops++;
    tk_rwlock_wrunlock(h);
    RETVAL = (h->mode == TK_MODE_DECAYED) ? newSVnv(tk_w_get(raw) / g) : newSVuv((UV)raw);
  OUTPUT:
    RETVAL

t/02-decayed.t  view on Meta::CPAN

        ok $r->is_decayed, 'reopened tree is still decayed';
        cmp_ok abs($r->half_life - 50), '<', 1e-9, 'reopen: half_life persisted';
        cmp_ok $r->estimate("p"), '>', 0, 'reopen: decayed value persisted';
    }
    unlink $path;
}

# ---- regression: a non-finite timestamp must not brick the summary ----
{
    my $t = Data::TopK::Shared->new_decayed(undef, 8, 16, 10);
    my $inf = "Inf" + 0; my $nan = "NaN" + 0;   # NOT 9e999 (finite on -Duselongdouble)
    $t->add("a", 5);
    $t->add("a", $inf);          # Inf timestamp -> ignored (per-add tick), not a poison
    $t->add("a", $nan);          # NaN timestamp -> ignored
    my $e = $t->estimate("a");
    ok $e == $e && $e < 1e9, 'Inf/NaN timestamp does not NaN-brick the estimate';
    $t->add("b", 10);
    ok $t->estimate("b") == $t->estimate("b"), 'summary still usable after a non-finite timestamp';
}

# ---- regression: a huge time gap fully decays old weight (no g=Inf stale weights) ----
{
    my $t = Data::TopK::Shared->new_decayed(undef, 8, 16, 10);   # half-life 10
    $t->add("old", $_) for 100, 101, 102;
    $t->add("new", 200000);      # gap far past the exp-overflow point -> old must decay to ~0
    cmp_ok $t->estimate("old"), '<', 1e-6, 'old weight fully decays across a huge gap';

topk.h  view on Meta::CPAN

    __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) */



( run in 2.045 seconds using v1.01-cache-2.11-cpan-60884a8df76 )