Skip to content

Commit c968469

Browse files
committed
chore: update to Zig 0.15.1
1 parent 4ff4cb8 commit c968469

File tree

7 files changed

+102
-99
lines changed

7 files changed

+102
-99
lines changed

.github/workflows/cd.yaml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ on:
55
workflows: [CI]
66
types: [completed]
77

8-
workflow_dispatch:
8+
concurrency:
9+
group: cd
10+
cancel-in-progress: true
911

1012
jobs:
1113
emit:
12-
if: ${{ github.event.workflow_run.conclusion == 'success' }}
14+
if: ${{ github.event.workflow_run.event == 'push' && github.event.workflow_run.conclusion == 'success' }}
1315

1416
runs-on: ubuntu-latest
1517

1618
steps:
1719
- name: Check out repository
18-
uses: actions/checkout@v4
20+
uses: actions/checkout@v5
1921

2022
- name: Set up Zig
21-
uses: mlugg/setup-zig@v1
22-
with:
23-
version: master
23+
uses: mlugg/setup-zig@v2
2424

25-
- name: Run doc step
25+
- name: Run `doc` step
2626
run: zig build doc
2727

2828
- name: Upload artifact for GitHub Pages
29-
uses: actions/upload-pages-artifact@v3
29+
uses: actions/upload-pages-artifact@v4
3030
with:
3131
path: zig-out/docs/
3232

.github/workflows/ci.yaml

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,33 @@ on:
77
pull_request:
88
branches: [main]
99

10-
workflow_dispatch:
10+
concurrency:
11+
group: ci
12+
cancel-in-progress: true
1113

1214
jobs:
1315
test:
1416
runs-on: ubuntu-latest
1517

1618
steps:
1719
- name: Check out repository
18-
uses: actions/checkout@v4
20+
uses: actions/checkout@v5
1921

2022
- name: Set up Zig
21-
uses: mlugg/setup-zig@v1
22-
with:
23-
version: master
23+
uses: mlugg/setup-zig@v2
2424

25-
- name: Run test step
25+
- name: Run `test` step
2626
run: zig build test --summary all
2727

2828
fmt:
2929
runs-on: ubuntu-latest
3030

3131
steps:
3232
- name: Check out repository
33-
uses: actions/checkout@v4
33+
uses: actions/checkout@v5
3434

3535
- name: Set up Zig
36-
uses: mlugg/setup-zig@v1
37-
with:
38-
version: master
36+
uses: mlugg/setup-zig@v2
3937

40-
- name: Run fmt step
38+
- name: Run `fmt` step
4139
run: zig build fmt

README.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
### Usage
66

7-
- Add `sieve` dependency to `build.zig.zon`.
7+
1. Add `sieve` dependency to `build.zig.zon`:
88

99
```sh
10-
zig fetch --save git+https://github.com/tensorush/zig-sieve
10+
zig fetch --save git+https://github.com/tensorush/zig-sieve.git
1111
```
1212

13-
- Use `sieve` dependency in `build.zig`.
13+
2. Use `sieve` dependency in `build.zig`:
1414

1515
```zig
1616
const sieve_dep = b.dependency("sieve", .{
@@ -23,23 +23,23 @@ const sieve_mod = sieve_dep.module("sieve");
2323

2424
### Benchmarks (MacBook M1 Pro)
2525

26-
- Sequence: the time to cache and retrieve integer values.
26+
- Sequence - the time to cache and retrieve integer values:
2727

28-
```sh
29-
$ zig build bench -- -s
30-
Sequence: 23.042us
31-
```
28+
```sh
29+
$ zig build bench -- s
30+
Sequence: 22.958us
31+
```
3232

33-
- Composite: the time to cache and retrieve composite values.
33+
- Composite - the time to cache and retrieve composite values:
3434

35-
```sh
36-
$ zig build bench -- -c
37-
Composite: 33.417us
38-
```
35+
```sh
36+
$ zig build bench -- c
37+
Composite: 37.668us
38+
```
3939

40-
- Composite (normal): the time to cache and retrieve normally-distributed composite values.
40+
- Composite (normal) - the time to cache and retrieve normally-distributed composite values:
4141

42-
```sh
43-
$ zig build bench -- -n
44-
Composite Normal: 99.708us
45-
```
42+
```sh
43+
$ zig build bench -- n
44+
Composite Normal: 108.001us
45+
```

build.zig

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
11
const std = @import("std");
22

3-
pub fn build(b: *std.Build) void {
3+
const manifest = @import("build.zig.zon");
4+
5+
pub fn build(b: *std.Build) !void {
46
const install_step = b.getInstallStep();
57
const target = b.standardTargetOptions(.{});
68
const optimize = b.standardOptimizeOption(.{});
79
const root_source_file = b.path("src/root.zig");
8-
const version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 1 };
10+
const version: std.SemanticVersion = try .parse(manifest.version);
911

10-
// Module
11-
const mod = b.addModule("sieve", .{
12+
// Public root module
13+
const root_mod = b.addModule("sieve", .{
1214
.target = target,
1315
.optimize = optimize,
1416
.root_source_file = root_source_file,
17+
.strip = b.option(bool, "strip", "Strip the binary"),
1518
});
1619

1720
// Library
18-
const lib_step = b.step("lib", "Install library");
19-
2021
const lib = b.addLibrary(.{
2122
.name = "sieve",
2223
.version = version,
23-
.root_module = mod,
24+
.root_module = root_mod,
2425
});
25-
26-
const lib_install = b.addInstallArtifact(lib, .{});
27-
lib_step.dependOn(&lib_install.step);
28-
install_step.dependOn(lib_step);
26+
b.installArtifact(lib);
2927

3028
// Documentation
3129
const docs_step = b.step("doc", "Emit documentation");
30+
3231
const docs_install = b.addInstallDirectory(.{
3332
.install_dir = .prefix,
3433
.install_subdir = "docs",
3534
.source_dir = lib.getEmittedDocs(),
3635
});
3736
docs_step.dependOn(&docs_install.step);
38-
install_step.dependOn(docs_step);
3937

4038
// Benchmarks
4139
const benchs_step = b.step("bench", "Run benchmarks");
@@ -60,11 +58,7 @@ pub fn build(b: *std.Build) void {
6058
const tests_step = b.step("test", "Run test suite");
6159

6260
const tests = b.addTest(.{
63-
.version = version,
64-
.root_module = b.createModule(.{
65-
.target = target,
66-
.root_source_file = root_source_file,
67-
}),
61+
.root_module = root_mod,
6862
});
6963

7064
const tests_run = b.addRunArtifact(tests);
@@ -84,4 +78,14 @@ pub fn build(b: *std.Build) void {
8478
});
8579
fmt_step.dependOn(&fmt.step);
8680
install_step.dependOn(fmt_step);
81+
82+
// Compilation check for ZLS Build-On-Save
83+
// See: https://zigtools.org/zls/guides/build-on-save/
84+
const check_step = b.step("check", "Check compilation");
85+
const check_exe = b.addExecutable(.{
86+
.name = "sieve",
87+
.version = version,
88+
.root_module = root_mod,
89+
});
90+
check_step.dependOn(&check_exe.step);
8791
}

build.zig.zon

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.{
22
.name = .sieve,
3-
.fingerprint = 0x34df97e7ea21fbe6,
4-
.version = "0.1.1",
5-
.minimum_zig_version = "0.14.0",
3+
.version = "0.2.0",
4+
.fingerprint = 0x34df97e7ea21fbe6, // Changing this has security and trust implications.
5+
.minimum_zig_version = "0.15.1",
66
.paths = .{
77
"src/",
88
"build.zig",

src/bench.zig

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
11
const std = @import("std");
2+
23
const sieve = @import("sieve.zig");
34

4-
const MEAN: f64 = 50.0;
5-
const ARRAY_SIZE: u64 = 12;
6-
const NUM_ITERS: u64 = 1000;
7-
const CACHE_CAPACITY: u64 = 68;
8-
const STD_DEV: f64 = MEAN / 3.0;
5+
const MEAN = 50.0;
6+
const ARRAY_SIZE = 12;
7+
const NUM_ITERS = 1000;
8+
const CACHE_CAPACITY = 68;
9+
const STD_DEV = MEAN / 3.0;
10+
const MAX_BUF_SIZE = 1 << 12;
911

1012
pub fn main() !void {
11-
var gpa_state = std.heap.DebugAllocator(.{}){};
13+
var gpa_state: std.heap.DebugAllocator(.{}) = .init;
1214
const gpa = gpa_state.allocator();
13-
defer if (gpa_state.deinit() == .leak) {
14-
@panic("Memory leak has occurred!");
15-
};
15+
defer if (gpa_state.deinit() == .leak) @panic("Memory leaked!");
1616

17-
const std_out = std.io.getStdOut();
18-
var buf_writer = std.io.bufferedWriter(std_out.writer());
19-
const writer = buf_writer.writer();
17+
var stdout_buf: [MAX_BUF_SIZE]u8 = undefined;
18+
var stdout_writer = std.fs.File.stdout().writer(&stdout_buf);
19+
const writer = &stdout_writer.interface;
2020

21-
var prng = std.Random.DefaultPrng.init(blk: {
21+
var prng: std.Random.DefaultPrng = .init(blk: {
2222
var seed: u64 = undefined;
2323
try std.posix.getrandom(std.mem.asBytes(&seed));
2424
break :blk seed;
2525
});
2626
const random = prng.random();
2727

28-
var buf: [512]u8 = undefined;
29-
var fixed_buf = std.heap.FixedBufferAllocator.init(buf[0..]);
28+
var buf: [MAX_BUF_SIZE]u8 = undefined;
29+
var fixed_buf: std.heap.FixedBufferAllocator = .init(&buf);
3030
const args = try std.process.argsAlloc(fixed_buf.allocator());
3131

32-
switch (args[1][1]) {
32+
switch (args[1][0]) {
3333
's' => try benchmarkSequence(gpa, writer),
3434
'c' => try benchmarkComposite(gpa, random, writer),
3535
'n' => try benchmarkCompositeNormal(gpa, random, writer),
3636
else => @panic("Unknown benchmark!"),
3737
}
3838

39-
try buf_writer.flush();
39+
try writer.flush();
4040
}
4141

4242
fn benchmarkSequence(gpa: std.mem.Allocator, writer: anytype) !void {
4343
const Cache = sieve.Cache(u64, u64);
44-
var cache = try Cache.init(gpa, CACHE_CAPACITY);
44+
var cache: Cache = try .init(gpa, CACHE_CAPACITY);
4545
defer cache.deinit(gpa);
4646

4747
var timer = try std.time.Timer.start();
@@ -62,12 +62,12 @@ fn benchmarkSequence(gpa: std.mem.Allocator, writer: anytype) !void {
6262
_ = cache.get(num);
6363
}
6464

65-
try writer.print("Sequence: {}\n", .{std.fmt.fmtDuration(timer.read() - start)});
65+
try writer.print("Sequence: {D}\n", .{timer.read() - start});
6666
}
6767

6868
fn benchmarkComposite(gpa: std.mem.Allocator, random: std.Random, writer: anytype) !void {
6969
const Cache = sieve.Cache(u64, struct { [ARRAY_SIZE]u8, u64 });
70-
var cache = try Cache.init(gpa, CACHE_CAPACITY);
70+
var cache: Cache = try .init(gpa, CACHE_CAPACITY);
7171
defer cache.deinit(gpa);
7272

7373
var timer = try std.time.Timer.start();
@@ -79,7 +79,7 @@ fn benchmarkComposite(gpa: std.mem.Allocator, random: std.Random, writer: anytyp
7979
while (i < NUM_ITERS) : (i += 1) {
8080
num = random.uintLessThan(u64, 100);
8181
node = try gpa.create(Cache.Node);
82-
node.* = .{ .key = num, .value = .{ [1]u8{0} ** ARRAY_SIZE, num } };
82+
node.* = .{ .key = num, .value = .{ @splat(0), num } };
8383
_ = try cache.put(node);
8484
}
8585

@@ -88,12 +88,12 @@ fn benchmarkComposite(gpa: std.mem.Allocator, random: std.Random, writer: anytyp
8888
_ = cache.get(num);
8989
}
9090

91-
try writer.print("Composite: {}\n", .{std.fmt.fmtDuration(timer.read() - start)});
91+
try writer.print("Composite: {D}\n", .{timer.read() - start});
9292
}
9393

9494
fn benchmarkCompositeNormal(gpa: std.mem.Allocator, random: std.Random, writer: anytype) !void {
9595
const Cache = sieve.Cache(u64, struct { [ARRAY_SIZE]u8, u64 });
96-
var cache = try Cache.init(gpa, @intFromFloat(STD_DEV));
96+
var cache: Cache = try .init(gpa, @intFromFloat(STD_DEV));
9797
defer cache.deinit(gpa);
9898

9999
var timer = try std.time.Timer.start();
@@ -106,7 +106,7 @@ fn benchmarkCompositeNormal(gpa: std.mem.Allocator, random: std.Random, writer:
106106
num = @intFromFloat(random.floatNorm(f64) * STD_DEV + MEAN);
107107
num %= 100;
108108
node = try gpa.create(Cache.Node);
109-
node.* = .{ .key = num, .value = .{ [1]u8{0} ** ARRAY_SIZE, num } };
109+
node.* = .{ .key = num, .value = .{ @splat(0), num } };
110110
_ = try cache.put(node);
111111
}
112112

@@ -116,5 +116,5 @@ fn benchmarkCompositeNormal(gpa: std.mem.Allocator, random: std.Random, writer:
116116
_ = cache.get(num);
117117
}
118118

119-
try writer.print("Composite Normal: {}\n", .{std.fmt.fmtDuration(timer.read() - start)});
119+
try writer.print("Composite Normal: {D}\n", .{timer.read() - start});
120120
}

0 commit comments

Comments
 (0)