Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

Playground

countminsketchdemo.gno

// Package countminsketchdemo is a small gnoweb demo of the frequency sketch
// provided by the
// [p/moul/x/daily/countminsketch](/p/nym-moultest123/x/daily/countminsketch/v0) library:
// fixed storage, one-sided error, and what a collision looks like.
//
// It contains no sketch logic of its own. Stateless, so Render is
// deterministic — which is precisely what the library is for.
package countminsketchdemo

import (
	"strconv"
	"strings"

	"gno.land/p/nym-moultest123/x/daily/countminsketch/v0"
)

// stream is the workload: a few heavy hitters and a long tail.
var stream = buildStream()

func buildStream() []string {
	var out []string
	for i := 0; i < 40; i++ {
		out = append(out, "alice")
	}
	for i := 0; i < 25; i++ {
		out = append(out, "bob")
	}
	for i := 0; i < 10; i++ {
		out = append(out, "carol")
	}
	for i := 0; i < 120; i++ { // the tail: 120 distinct one-offs
		out = append(out, "user"+strconv.Itoa(i))
	}
	return out
}

// Render renders the demo for gnoweb.
func Render(path string) string {
	var b strings.Builder
	b.WriteString("# Count–Min Sketch\n\n")
	b.WriteString("Frequency estimates in fixed space, demoing the ")
	b.WriteString("[`p/moul/x/daily/countminsketch`](/p/nym-moultest123/x/daily/countminsketch/v0) library.\n\n")

	b.WriteString("## The workload\n\n")
	b.WriteString("`" + strconv.Itoa(len(stream)) + "` events over `123` distinct keys: ")
	b.WriteString("three heavy hitters and a long tail of one-offs.\n\n")

	big, _ := countminsketch.New(1024, 4)
	fill(big)

	b.WriteString("## A generously sized sketch (1024 × 4)\n\n")
	b.WriteString("| key | true | estimate |\n|---|---|---|\n")
	for _, k := range []string{"alice", "bob", "carol", "user0", "never-seen"} {
		b.WriteString("| `" + k + "` | " + strconv.Itoa(trueCount(k)) + " | " +
			strconv.FormatInt(big.Estimate(k), 10) + " |\n")
	}
	b.WriteString("\nExact here, and `never-seen` reads `0` — a **zero estimate is ")
	b.WriteString("definitive**: that key was never added.\n\n")

	tiny, _ := countminsketch.New(8, 2)
	fill(tiny)

	b.WriteString("## A deliberately tiny one (8 × 2)\n\n")
	b.WriteString("Only `" + strconv.Itoa(tiny.Counters()) + "` counters for `123` distinct ")
	b.WriteString("keys, so collisions are guaranteed:\n\n")
	b.WriteString("| key | true | estimate | error |\n|---|---|---|---|\n")
	for _, k := range []string{"alice", "bob", "carol", "user0"} {
		est := tiny.Estimate(k)
		b.WriteString("| `" + k + "` | " + strconv.Itoa(trueCount(k)) + " | " +
			strconv.FormatInt(est, 10) + " | +" +
			strconv.FormatInt(est-int64(trueCount(k)), 10) + " |\n")
	}
	b.WriteString("\nEvery error is **positive**. That is the guarantee: the sketch may ")
	b.WriteString("overcount, never undercount, so an estimate is an upper bound — ")
	b.WriteString("\"at most this often\", never \"exactly this often\".\n\n")

	b.WriteString("## Where the collisions are\n\n")
	b.WriteString("Row 0 of the tiny sketch, and the column each key lands in:\n\n")
	b.WriteString("| column | counter |\n|---|---|\n")
	for c, v := range tiny.Row(0) {
		who := []string{}
		for _, k := range []string{"alice", "bob", "carol"} {
			if tiny.Index(k, 0) == c {
				who = append(who, "`"+k+"`")
			}
		}
		line := "| " + strconv.Itoa(c) + " | " + strconv.FormatInt(v, 10)
		if len(who) > 0 {
			line += " ← " + strings.Join(who, ", ")
		}
		b.WriteString(line + " |\n")
	}
	b.WriteString("\nTaking the **minimum** across rows is what limits the damage: an ")
	b.WriteString("overestimate needs a collision in *every* row at once.\n\n")

	b.WriteString("## Why bother\n\n")
	b.WriteString("| | exact map | sketch (1024 × 4) |\n|---|---|---|\n")
	b.WriteString("| storage | one entry per distinct key, unbounded | ")
	b.WriteString(strconv.Itoa(big.Counters()) + " counters, fixed |\n")
	b.WriteString("| answer | exact | upper bound |\n")
	b.WriteString("\nOn chain the second row is the point: storage driven by user input ")
	b.WriteString("is a liability, and a sketch sized once cannot be made to grow.\n")
	return b.String()
}

func fill(s *countminsketch.Sketch) {
	for _, e := range stream {
		s.Add(e)
	}
}

func trueCount(k string) int {
	n := 0
	for _, e := range stream {
		if e == k {
			n++
		}
	}
	return n
}

gnomod.toml

module = "gno.land/r/nym-moultest123/x/daily/countminsketchdemo/v0"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186410
  max_deposit = "5000000ugnot"

render_example_test.gno

package countminsketchdemo

// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
	print(Render(""))
	// Output:
	// # Count–Min Sketch
	//
	// Frequency estimates in fixed space, demoing the [`p/moul/x/daily/countminsketch`](/p/nym-moultest123/x/daily/countminsketch/v0) library.
	//
	// ## The workload
	//
	// `195` events over `123` distinct keys: three heavy hitters and a long tail of one-offs.
	//
	// ## A generously sized sketch (1024 × 4)
	//
	// | key | true | estimate |
	// |---|---|---|
	// | `alice` | 40 | 40 |
	// | `bob` | 25 | 25 |
	// | `carol` | 10 | 10 |
	// | `user0` | 1 | 1 |
	// | `never-seen` | 0 | 0 |
	//
	// Exact here, and `never-seen` reads `0` — a **zero estimate is definitive**: that key was never added.
	//
	// ## A deliberately tiny one (8 × 2)
	//
	// Only `16` counters for `123` distinct keys, so collisions are guaranteed:
	//
	// | key | true | estimate | error |
	// |---|---|---|---|
	// | `alice` | 40 | 54 | +14 |
	// | `bob` | 25 | 39 | +14 |
	// | `carol` | 10 | 24 | +14 |
	// | `user0` | 1 | 16 | +15 |
	//
	// Every error is **positive**. That is the guarantee: the sketch may overcount, never undercount, so an estimate is an upper bound — "at most this often", never "exactly this often".
	//
	// ## Where the collisions are
	//
	// Row 0 of the tiny sketch, and the column each key lands in:
	//
	// | column | counter |
	// |---|---|
	// | 0 | 15 |
	// | 1 | 25 ← `carol` |
	// | 2 | 16 |
	// | 3 | 15 |
	// | 4 | 54 ← `alice` |
	// | 5 | 16 |
	// | 6 | 15 |
	// | 7 | 39 ← `bob` |
	//
	// Taking the **minimum** across rows is what limits the damage: an overestimate needs a collision in *every* row at once.
	//
	// ## Why bother
	//
	// | | exact map | sketch (1024 × 4) |
	// |---|---|---|
	// | storage | one entry per distinct key, unbounded | 4096 counters, fixed |
	// | answer | exact | upper bound |
	//
	// On chain the second row is the point: storage driven by user input is a liability, and a sketch sized once cannot be made to grow.
}