Skip to content

Commit

Permalink
ptrhash-paper: bucket-fn construction results
Browse files Browse the repository at this point in the history
  • Loading branch information
RagnarGrootKoerkamp committed Dec 5, 2024
1 parent 50c69e4 commit c6c9055
Show file tree
Hide file tree
Showing 7 changed files with 6,212 additions and 834 deletions.
18 changes: 12 additions & 6 deletions posts/ptrhash-paper/plot.py → posts/ptrhash-paper/bucket-fn.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,15 @@ def invert(f, y):
return l


lmbda = 4


def bucket_sz(f, y):
y1 = y
y2 = y + 0.001
x1 = invert(f, y1)
x2 = invert(f, y2)
return (x2 - x1) * 1000
return lmbda * (x2 - x1) * 1000


xs = [x / 1000 for x in range(1000)]
Expand All @@ -73,7 +76,7 @@ def bucket_sz(f, y):
# Add horizontal grid lines
plt.grid(axis="y", lw=0.5)

plt.savefig("bucket-fn.svg", bbox_inches="tight")
plt.savefig("plots/bucket-fn.svg", bbox_inches="tight")
plt.close()

## PLOT 2
Expand All @@ -93,11 +96,14 @@ def bucket_sz(f, y):
plt.gca().spines["right"].set_visible(False)
# x and y from 0 to 1
plt.xlim(0, 1)
plt.ylim(0, 3)
plt.ylim(0, 11.5)
plt.xlabel("Normalized bucket index")
plt.ylabel("Relative expected bucket size")
plt.ylabel("Expected bucket size for $\lambda = 4$")
# Add horizontal grid lines
plt.grid(axis="y", lw=0.5)
plt.grid(axis="y", lw=0.5, which="major")
plt.grid(axis="y", lw=0.5, which="minor", alpha=0.4)
# Add minor tickes lines every 1
plt.yticks(range(0, 12, 1), minor=True)

plt.savefig("bucket-size.svg", bbox_inches="tight")
plt.savefig("plots/bucket-size.svg", bbox_inches="tight")
plt.close()
118 changes: 118 additions & 0 deletions posts/ptrhash-paper/evals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python3

import json
from math import log, floor, ceil
import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator


def build_stats(f, out):
alpha = 0.98

with open(f) as f:
all_data = json.load(f)

cols = len(all_data)

pcts = range(100)
xs = [x / 100 for x in range(0, 101)]

# Figure with 3 subplots
fig, axs = plt.subplots(
1, cols, figsize=(cols * 3 + 0.75, 2.5), layout="constrained"
)
if cols == 1:
axs = [axs]
keys = ["linear", "skewed", "optimal", "square", "cubic"]
keys = [k for k in keys if k in all_data]

for ax1, name in zip(axs, keys):
data = all_data[name]["by_pct"]
elem = 0
elems = [0]
for pct in pcts:
elem += data[pct]["elements"]
elems.append(elem)
slots = elems[-1] / alpha
load = [x / slots for x in elems]

# Set plot size
ax3 = ax1.twinx()
ax2 = ax1.twinx()

ax1.set_ylim(0, 1.0)
ax3.set_ylim(0, 1)
ax2.set_ylim(0, 10)

ax1.set_title(name.capitalize())

ax1.set_xlabel("Normalized bucket index")
ax1.set_ylabel("Evictions per bucket")
ax2.set_ylabel("Bucket size")
ax2.yaxis.label.set_color("red")
ax1.xaxis.set_minor_locator(MultipleLocator(0.1))

p1 = ax1.plot(
xs[1:],
[data[pct]["evictions"] / data[pct]["buckets"] for pct in pcts],
label="Evictions per bucket",
color="black",
)
# Plot load factor
p3 = ax3.fill(xs + [1], load + [0], color="red", alpha=0.1, label="Load factor")
ax3.yaxis.set_visible(False)

# Plot bucket size on secondary axis
p2 = ax2.plot(
xs[1:],
[data[pct]["elements"] / data[pct]["buckets"] for pct in pcts],
color="red",
label="Bucket size",
)

ax1.set_zorder(ax3.get_zorder() + 1)
ax1.set_frame_on(False)

# Only show x and y ax
ax1.spines["top"].set_visible(False)
ax1.spines["bottom"].set_visible(False)
ax1.spines["left"].set_visible(False)
ax1.spines["right"].set_visible(False)
ax2.spines["top"].set_visible(False)
ax2.spines["bottom"].set_visible(False)
ax2.spines["left"].set_visible(False)
ax2.spines["right"].set_visible(False)
ax3.spines["top"].set_visible(False)
ax3.spines["bottom"].set_visible(False)
ax3.spines["left"].set_visible(False)
ax3.spines["right"].set_visible(False)
# x and y from 0 to 1
ax1.set_xlim(0, 1)
# plt.ylim(0, 1)
# plt.xlabel("Normalized hash")
# plt.ylabel("Normalized bucket index")
# Add horizontal grid lines
ax1.grid(axis="y", lw=0.5)

# Keep only leftmost and rightmost y-axis
# First
if name == keys[0]:
ax1.yaxis.set_visible(True)
else:
ax1.set_yticklabels([])
ax1.yaxis.set_ticks_position("none")
ax1.set_ylabel(None)
# Last
if name == keys[-1]:
ax2.yaxis.set_visible(True)
if len(keys) > 1:
ax1.legend(handles=p1 + p2 + p3, loc="best")
else:
ax2.yaxis.set_visible(False)

plt.savefig(out, bbox_inches="tight")
plt.close()


build_stats("data/build_stats_l35.json", "plots/build_stats_l35.svg")
build_stats("data/build_stats_l40.json", "plots/build_stats_l40.svg")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit c6c9055

Please sign in to comment.