Skip to content

Commit 4ff4cb8

Browse files
committed
chore: update to Zig v0.14.0
1 parent 5260f6d commit 4ff4cb8

File tree

10 files changed

+81
-87
lines changed

10 files changed

+81
-87
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
# Zig recommends LF line endings
12
*.zig text eol=lf
23
*.zon text eol=lf

.github/workflows/cd.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ on:
99

1010
jobs:
1111
emit:
12+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
13+
1214
runs-on: ubuntu-latest
1315

1416
steps:
@@ -17,14 +19,16 @@ jobs:
1719

1820
- name: Set up Zig
1921
uses: mlugg/setup-zig@v1
22+
with:
23+
version: master
2024

21-
- name: Run `doc`
25+
- name: Run doc step
2226
run: zig build doc
2327

2428
- name: Upload artifact for GitHub Pages
2529
uses: actions/upload-pages-artifact@v3
2630
with:
27-
path: zig-out/doc/
31+
path: zig-out/docs/
2832

2933
deploy:
3034
needs: emit

.github/workflows/ci.yaml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ jobs:
1919

2020
- name: Set up Zig
2121
uses: mlugg/setup-zig@v1
22+
with:
23+
version: master
2224

23-
- name: Run `test`
25+
- name: Run test step
2426
run: zig build test --summary all
2527

2628
fmt:
@@ -32,6 +34,8 @@ jobs:
3234

3335
- name: Set up Zig
3436
uses: mlugg/setup-zig@v1
37+
with:
38+
version: master
3539

36-
- name: Run `fmt`
40+
- name: Run fmt step
3741
run: zig build fmt

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
# Zig artifacts
12
.zig-cache/
23
zig-out/

README.md

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
# zig-sieve
22

3-
[![CI][ci-shd]][ci-url]
4-
[![CD][cd-shd]][cd-url]
5-
[![DC][dc-shd]][dc-url]
6-
[![LC][lc-shd]][lc-url]
7-
83
## Zig implementation of [SIEVE cache eviction algorithm](https://cachemon.github.io/SIEVE-website/).
94

105
### Usage
116

127
- Add `sieve` dependency to `build.zig.zon`.
138

149
```sh
15-
zig fetch --save git+https://github.com/tensorush/zig-sieve#<git_tag_or_commit_hash>
10+
zig fetch --save git+https://github.com/tensorush/zig-sieve
1611
```
1712

1813
- Use `sieve` dependency in `build.zig`.
@@ -26,36 +21,25 @@ const sieve_mod = sieve_dep.module("sieve");
2621
<compile>.root_module.addImport("sieve", sieve_mod);
2722
```
2823

29-
### Benchmarks
24+
### Benchmarks (MacBook M1 Pro)
3025

3126
- Sequence: the time to cache and retrieve integer values.
3227

3328
```sh
3429
$ zig build bench -- -s
35-
Sequence: 28.833us
30+
Sequence: 23.042us
3631
```
3732

3833
- Composite: the time to cache and retrieve composite values.
3934

4035
```sh
4136
$ zig build bench -- -c
42-
Composite: 45.291us
37+
Composite: 33.417us
4338
```
4439

4540
- Composite (normal): the time to cache and retrieve normally-distributed composite values.
4641

4742
```sh
4843
$ zig build bench -- -n
49-
Composite Normal: 135.25us
44+
Composite Normal: 99.708us
5045
```
51-
52-
<!-- MARKDOWN LINKS -->
53-
54-
[ci-shd]: https://img.shields.io/github/actions/workflow/status/tensorush/zig-sieve/ci.yaml?branch=main&style=for-the-badge&logo=github&label=CI&labelColor=black
55-
[ci-url]: https://github.com/tensorush/zig-sieve/blob/main/.github/workflows/ci.yaml
56-
[cd-shd]: https://img.shields.io/github/actions/workflow/status/tensorush/zig-sieve/cd.yaml?branch=main&style=for-the-badge&logo=github&label=CD&labelColor=black
57-
[cd-url]: https://github.com/tensorush/zig-sieve/blob/main/.github/workflows/cd.yaml
58-
[dc-shd]: https://img.shields.io/badge/click-F6A516?style=for-the-badge&logo=zig&logoColor=F6A516&label=docs&labelColor=black
59-
[dc-url]: https://tensorush.github.io/zig-sieve
60-
[lc-shd]: https://img.shields.io/github/license/tensorush/zig-sieve.svg?style=for-the-badge&labelColor=black
61-
[lc-url]: https://github.com/tensorush/zig-sieve/blob/main/LICENSE

build.zig

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,53 @@
11
const std = @import("std");
22

33
pub fn build(b: *std.Build) void {
4+
const install_step = b.getInstallStep();
45
const target = b.standardTargetOptions(.{});
56
const optimize = b.standardOptimizeOption(.{});
6-
const root_source_file = b.path("src/sieve.zig");
7-
const version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 0 };
7+
const root_source_file = b.path("src/root.zig");
8+
const version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 1 };
89

910
// Module
10-
_ = b.addModule("sieve", .{ .root_source_file = root_source_file });
11+
const mod = b.addModule("sieve", .{
12+
.target = target,
13+
.optimize = optimize,
14+
.root_source_file = root_source_file,
15+
});
1116

1217
// Library
1318
const lib_step = b.step("lib", "Install library");
1419

15-
const lib = b.addStaticLibrary(.{
20+
const lib = b.addLibrary(.{
1621
.name = "sieve",
17-
.target = target,
1822
.version = version,
19-
.optimize = optimize,
20-
.root_source_file = root_source_file,
23+
.root_module = mod,
2124
});
2225

2326
const lib_install = b.addInstallArtifact(lib, .{});
2427
lib_step.dependOn(&lib_install.step);
25-
b.default_step.dependOn(lib_step);
28+
install_step.dependOn(lib_step);
2629

2730
// Documentation
28-
const doc_step = b.step("doc", "Emit documentation");
29-
30-
const doc_install = b.addInstallDirectory(.{
31+
const docs_step = b.step("doc", "Emit documentation");
32+
const docs_install = b.addInstallDirectory(.{
3133
.install_dir = .prefix,
32-
.install_subdir = "doc",
34+
.install_subdir = "docs",
3335
.source_dir = lib.getEmittedDocs(),
3436
});
35-
doc_step.dependOn(&doc_install.step);
36-
b.default_step.dependOn(doc_step);
37+
docs_step.dependOn(&docs_install.step);
38+
install_step.dependOn(docs_step);
3739

3840
// Benchmarks
3941
const benchs_step = b.step("bench", "Run benchmarks");
4042

4143
const benchs = b.addExecutable(.{
4244
.name = "bench",
43-
.target = target,
44-
.optimize = .ReleaseFast,
45-
.root_source_file = b.path("src/bench.zig"),
45+
.version = version,
46+
.root_module = b.createModule(.{
47+
.target = target,
48+
.optimize = .ReleaseFast,
49+
.root_source_file = b.path("src/bench.zig"),
50+
}),
4651
});
4752

4853
const benchs_run = b.addRunArtifact(benchs);
@@ -55,25 +60,28 @@ pub fn build(b: *std.Build) void {
5560
const tests_step = b.step("test", "Run test suite");
5661

5762
const tests = b.addTest(.{
58-
.target = target,
5963
.version = version,
60-
.root_source_file = root_source_file,
64+
.root_module = b.createModule(.{
65+
.target = target,
66+
.root_source_file = root_source_file,
67+
}),
6168
});
6269

6370
const tests_run = b.addRunArtifact(tests);
6471
tests_step.dependOn(&tests_run.step);
65-
b.default_step.dependOn(tests_step);
72+
install_step.dependOn(tests_step);
6673

67-
// Formatting checks
68-
const fmt_step = b.step("fmt", "Run formatting checks");
74+
// Formatting check
75+
const fmt_step = b.step("fmt", "Check formatting");
6976

7077
const fmt = b.addFmt(.{
7178
.paths = &.{
7279
"src/",
7380
"build.zig",
81+
"build.zig.zon",
7482
},
7583
.check = true,
7684
});
7785
fmt_step.dependOn(&fmt.step);
78-
b.default_step.dependOn(fmt_step);
86+
install_step.dependOn(fmt_step);
7987
}

build.zig.zon

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.{
2-
.name = "sieve",
3-
.version = "0.1.0",
2+
.name = .sieve,
3+
.fingerprint = 0x34df97e7ea21fbe6,
4+
.version = "0.1.1",
45
.minimum_zig_version = "0.14.0",
56
.paths = .{
67
"src/",

src/bench.zig

Lines changed: 19 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//! Root file that benchmarks SIEVE cache eviction algorithm's performance.
2-
31
const std = @import("std");
42
const sieve = @import("sieve.zig");
53

@@ -10,15 +8,12 @@ const CACHE_CAPACITY: u64 = 68;
108
const STD_DEV: f64 = MEAN / 3.0;
119

1210
pub fn main() !void {
13-
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
14-
defer if (gpa.deinit() == .leak) {
11+
var gpa_state = std.heap.DebugAllocator(.{}){};
12+
const gpa = gpa_state.allocator();
13+
defer if (gpa_state.deinit() == .leak) {
1514
@panic("Memory leak has occurred!");
1615
};
1716

18-
var arena = std.heap.ArenaAllocator.init(gpa.allocator());
19-
const allocator = arena.allocator();
20-
defer arena.deinit();
21-
2217
const std_out = std.io.getStdOut();
2318
var buf_writer = std.io.bufferedWriter(std_out.writer());
2419
const writer = buf_writer.writer();
@@ -30,27 +25,24 @@ pub fn main() !void {
3025
});
3126
const random = prng.random();
3227

33-
var buf: [1024]u8 = undefined;
28+
var buf: [512]u8 = undefined;
3429
var fixed_buf = std.heap.FixedBufferAllocator.init(buf[0..]);
3530
const args = try std.process.argsAlloc(fixed_buf.allocator());
3631

3732
switch (args[1][1]) {
38-
's' => try benchmarkSequence(allocator, writer),
39-
'c' => try benchmarkComposite(allocator, random, writer),
40-
'n' => try benchmarkCompositeNormal(allocator, random, writer),
33+
's' => try benchmarkSequence(gpa, writer),
34+
'c' => try benchmarkComposite(gpa, random, writer),
35+
'n' => try benchmarkCompositeNormal(gpa, random, writer),
4136
else => @panic("Unknown benchmark!"),
4237
}
4338

4439
try buf_writer.flush();
4540
}
4641

47-
fn benchmarkSequence(
48-
allocator: std.mem.Allocator,
49-
writer: anytype,
50-
) !void {
42+
fn benchmarkSequence(gpa: std.mem.Allocator, writer: anytype) !void {
5143
const Cache = sieve.Cache(u64, u64);
52-
var cache = try Cache.init(allocator, CACHE_CAPACITY);
53-
defer cache.deinit(allocator);
44+
var cache = try Cache.init(gpa, CACHE_CAPACITY);
45+
defer cache.deinit(gpa);
5446

5547
var timer = try std.time.Timer.start();
5648
const start = timer.lap();
@@ -60,7 +52,7 @@ fn benchmarkSequence(
6052
var i: u64 = 1;
6153
while (i < NUM_ITERS) : (i += 1) {
6254
num = i % 100;
63-
node = try allocator.create(Cache.Node);
55+
node = try gpa.create(Cache.Node);
6456
node.* = .{ .key = num, .value = num };
6557
_ = try cache.put(node);
6658
}
@@ -73,14 +65,10 @@ fn benchmarkSequence(
7365
try writer.print("Sequence: {}\n", .{std.fmt.fmtDuration(timer.read() - start)});
7466
}
7567

76-
fn benchmarkComposite(
77-
allocator: std.mem.Allocator,
78-
random: std.Random,
79-
writer: anytype,
80-
) !void {
68+
fn benchmarkComposite(gpa: std.mem.Allocator, random: std.Random, writer: anytype) !void {
8169
const Cache = sieve.Cache(u64, struct { [ARRAY_SIZE]u8, u64 });
82-
var cache = try Cache.init(allocator, CACHE_CAPACITY);
83-
defer cache.deinit(allocator);
70+
var cache = try Cache.init(gpa, CACHE_CAPACITY);
71+
defer cache.deinit(gpa);
8472

8573
var timer = try std.time.Timer.start();
8674
const start = timer.lap();
@@ -90,7 +78,7 @@ fn benchmarkComposite(
9078
var i: u64 = 1;
9179
while (i < NUM_ITERS) : (i += 1) {
9280
num = random.uintLessThan(u64, 100);
93-
node = try allocator.create(Cache.Node);
81+
node = try gpa.create(Cache.Node);
9482
node.* = .{ .key = num, .value = .{ [1]u8{0} ** ARRAY_SIZE, num } };
9583
_ = try cache.put(node);
9684
}
@@ -103,14 +91,10 @@ fn benchmarkComposite(
10391
try writer.print("Composite: {}\n", .{std.fmt.fmtDuration(timer.read() - start)});
10492
}
10593

106-
fn benchmarkCompositeNormal(
107-
allocator: std.mem.Allocator,
108-
random: std.Random,
109-
writer: anytype,
110-
) !void {
94+
fn benchmarkCompositeNormal(gpa: std.mem.Allocator, random: std.Random, writer: anytype) !void {
11195
const Cache = sieve.Cache(u64, struct { [ARRAY_SIZE]u8, u64 });
112-
var cache = try Cache.init(allocator, @intFromFloat(STD_DEV));
113-
defer cache.deinit(allocator);
96+
var cache = try Cache.init(gpa, @intFromFloat(STD_DEV));
97+
defer cache.deinit(gpa);
11498

11599
var timer = try std.time.Timer.start();
116100
const start = timer.lap();
@@ -121,7 +105,7 @@ fn benchmarkCompositeNormal(
121105
while (i < NUM_ITERS) : (i += 1) {
122106
num = @intFromFloat(random.floatNorm(f64) * STD_DEV + MEAN);
123107
num %= 100;
124-
node = try allocator.create(Cache.Node);
108+
node = try gpa.create(Cache.Node);
125109
node.* = .{ .key = num, .value = .{ [1]u8{0} ** ARRAY_SIZE, num } };
126110
_ = try cache.put(node);
127111
}

src/root.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//! Root source file that exposes the library's API to users and Autodoc.
2+
3+
const std = @import("std");
4+
5+
pub const Cache = @import("sieve.zig").Cache;
6+
7+
test {
8+
std.testing.refAllDecls(@This());
9+
}

src/sieve.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//! Root library file that exposes the public API.
2-
31
const std = @import("std");
42

53
/// Intrusive cache based on the SIEVE eviction algorithm.

0 commit comments

Comments
 (0)