Skip to content

Commit

Permalink
fix: update build
Browse files Browse the repository at this point in the history
  • Loading branch information
tensorush committed Jul 18, 2024
1 parent ec37093 commit 9951c8e
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 164 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
*.zig text eol=lf
*.zon text eol=lf
38 changes: 13 additions & 25 deletions .github/workflows/cd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,42 @@ name: Continuous Delivery
on:
push:
branches: [main]
paths: ["**.zig"]

pull_request:
branches: [main]
paths: ["**.zig"]

workflow_dispatch:

permissions:
pages: write
contents: read
id-token: write

concurrency:
group: pages
cancel-in-progress: false

jobs:
build:
emit:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Zig
uses: goto-bus-stop/setup-zig@v2
uses: mlugg/setup-zig@v1

- name: Emit docs
run: |
zig build docs
rm -rf zig-out/docs/src/std/
- name: Run `doc`
run: zig build doc

- name: Upload artifact for GitHub Pages
uses: actions/upload-pages-artifact@v1
uses: actions/upload-pages-artifact@v3
with:
path: zig-out/docs/
path: zig-out/doc/

deploy:
needs: build
needs: emit

runs-on: ubuntu-latest

permissions:
pages: write
id-token: write

environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}

steps:
- name: Deploy artifact to GitHub Pages
id: deployment
uses: actions/deploy-pages@v2
uses: actions/deploy-pages@v4
35 changes: 9 additions & 26 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ name: Continuous Integration
on:
push:
branches: [main]
paths: ["**.zig"]

pull_request:
branches: [main]
paths: ["**.zig"]

workflow_dispatch:

Expand All @@ -17,38 +15,23 @@ jobs:

steps:
- name: Check out repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Zig
uses: goto-bus-stop/setup-zig@v2
uses: mlugg/setup-zig@v1

- name: Run tests
run: |
zig env
zig build test
- name: Run `test`
run: zig build test --summary all

- name: Set up kcov
run: sudo apt install kcov

- name: Generate code coverage report
run: zig build cov

- name: Upload code coverage report to Codecov
uses: codecov/codecov-action@v3
with:
directory: kcov-output
fail_ci_if_error: true
verbose: true

lint:
fmt:
runs-on: ubuntu-latest

steps:
- name: Check out repository
uses: actions/checkout@v3
uses: actions/checkout@v4

- name: Set up Zig
uses: goto-bus-stop/setup-zig@v2
uses: mlugg/setup-zig@v1

- name: Run lints
run: zig build lint
- name: Run `fmt`
run: zig build fmt
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
kcov-output/
zig-cache/
.zig-cache/
zig-out/
File renamed without changes.
62 changes: 15 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,29 @@
## :lizard: :moneybag: **zig sieve**
# zig-sieve

[![CI][ci-shd]][ci-url]
[![CD][cd-shd]][cd-url]
[![DC][dc-shd]][dc-url]
[![CC][cc-shd]][cc-url]
[![LC][lc-shd]][lc-url]

### Zig implementation of the [SIEVE cache eviction algorithm](https://cachemon.github.io/SIEVE-website/) created by [Yazhuo Zhang](https://github.com/yazhuo) and [Juncheng Yang](https://github.com/1a1a11a).
## Zig implementation of [SIEVE cache eviction algorithm](https://cachemon.github.io/SIEVE-website/).

### :rocket: Usage

1. Add `sieve` as a dependency in your `build.zig.zon`.

<details>

<summary><code>build.zig.zon</code> example</summary>

```zig
.{
.name = "<name_of_your_package>",
.version = "<version_of_your_package>",
.dependencies = .{
.sieve = .{
.url = "https://github.com/tensorush/zig-sieve/archive/<git_tag_or_commit_hash>.tar.gz",
.hash = "<package_hash>",
},
},
}
```

Set `<package_hash>` to `12200000000000000000000000000000000000000000000000000000000000000000`, and Zig will provide the correct found value in an error message.

</details>

2. Add `sieve` as a module in your `build.zig`.

<details>

<summary><code>build.zig</code> example</summary>

```zig
const sieve = b.dependency("sieve", .{});
exe.addModule("sieve", sieve.module("sieve"));
```

</details>
- Add `sieve` dependency to `build.zig.zon`.

```sh
zig fetch --save https://github.com/tensorush/zig-sieve/archive/<git_tag_or_commit_hash>.tar.gz
```

#### :bar_chart: Benchmarks
- Use `sieve` dependency in `build.zig`.

```bash
$ zig build bench
Sequence: 27.666us
Composite: 38.5us
Composite Normal: 141.458us
```zig
const sieve_dep = b.dependency("sieve", .{
.target = target,
.optimize = optimize,
});
const sieve_mod = sieve_dep.module("sieve");
<compile>.root_module.addImport("sieve", sieve_mod);
```

<!-- MARKDOWN LINKS -->
Expand All @@ -64,7 +34,5 @@ Composite Normal: 141.458us
[cd-url]: https://github.com/tensorush/zig-sieve/blob/main/.github/workflows/cd.yaml
[dc-shd]: https://img.shields.io/badge/click-F6A516?style=for-the-badge&logo=zig&logoColor=F6A516&label=docs&labelColor=black
[dc-url]: https://tensorush.github.io/zig-sieve
[cc-shd]: https://img.shields.io/codecov/c/github/tensorush/zig-sieve?style=for-the-badge&labelColor=black
[cc-url]: https://app.codecov.io/gh/tensorush/zig-sieve
[lc-shd]: https://img.shields.io/github/license/tensorush/zig-sieve.svg?style=for-the-badge&labelColor=black
[lc-url]: https://github.com/tensorush/zig-sieve/blob/main/LICENSE.md
[lc-url]: https://github.com/tensorush/zig-sieve/blob/main/LICENSE
70 changes: 33 additions & 37 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const root_source_file = b.path("src/sieve.zig");
const version = std.SemanticVersion{ .major = 0, .minor = 1, .patch = 0 };

// Module
_ = b.addModule("sieve", .{ .root_source_file = root_source_file });
Expand All @@ -12,70 +14,64 @@ pub fn build(b: *std.Build) void {

const lib = b.addStaticLibrary(.{
.name = "sieve",
.root_source_file = root_source_file,
.target = target,
.optimize = b.standardOptimizeOption(.{}),
.version = .{ .major = 0, .minor = 1, .patch = 0 },
.version = version,
.optimize = optimize,
.root_source_file = root_source_file,
});

const lib_install = b.addInstallArtifact(lib, .{});
lib_step.dependOn(&lib_install.step);
b.default_step.dependOn(lib_step);

// Docs
const docs_step = b.step("docs", "Emit docs");
// Documentation
const doc_step = b.step("doc", "Emit documentation");

const docs_install = b.addInstallDirectory(.{
.source_dir = lib.getEmittedDocs(),
const doc_install = b.addInstallDirectory(.{
.install_dir = .prefix,
.install_subdir = "docs",
.install_subdir = "doc",
.source_dir = lib.getEmittedDocs(),
});
doc_step.dependOn(&doc_install.step);
b.default_step.dependOn(doc_step);

docs_step.dependOn(&docs_install.step);
b.default_step.dependOn(docs_step);

// Benchmark
const bench_step = b.step("bench", "Run benchmarks");
// Benchmarks
const benchs_step = b.step("bench", "Run benchmarks");

const bench = b.addExecutable(.{
.name = "sieve_bench",
.root_source_file = b.path("src/bench.zig"),
const benchs = b.addExecutable(.{
.name = "bench",
.target = target,
.optimize = .ReleaseFast,
.root_source_file = b.path("src/bench.zig"),
});

const bench_run = b.addRunArtifact(bench);
bench_step.dependOn(&bench_run.step);
b.default_step.dependOn(bench_step);
const benchs_run = b.addRunArtifact(benchs);
benchs_step.dependOn(&benchs_run.step);
b.default_step.dependOn(benchs_step);

// Tests
const tests_step = b.step("test", "Run tests");
// Test suite
const tests_step = b.step("test", "Run test suite");

const tests = b.addTest(.{
.target = target,
.version = version,
.root_source_file = root_source_file,
});

const tests_run = b.addRunArtifact(tests);
tests_step.dependOn(&tests_run.step);
b.default_step.dependOn(tests_step);

// Code coverage
const cov_step = b.step("cov", "Generate code coverage report");

const cov_run = b.addSystemCommand(&.{ "kcov", "--clean", "--include-pattern=src/", "kcov-output" });
cov_run.addArtifactArg(tests);
// Formatting checks
const fmt_step = b.step("fmt", "Run formatting checks");

cov_step.dependOn(&cov_run.step);
b.default_step.dependOn(cov_step);

// Lints
const lints_step = b.step("lint", "Run lints");

const lints = b.addFmt(.{
.paths = &.{ "src", "build.zig" },
const fmt = b.addFmt(.{
.paths = &.{
"src/",
"build.zig",
},
.check = true,
});

lints_step.dependOn(&lints.step);
b.default_step.dependOn(lints_step);
fmt_step.dependOn(&fmt.step);
b.default_step.dependOn(fmt_step);
}
9 changes: 8 additions & 1 deletion build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
.{
.name = "sieve",
.version = "0.1.0",
.paths = .{""},
.minimum_zig_version = "0.13.0",
.paths = .{
"src/",
"build.zig",
"build.zig.zon",
"LICENSE",
"README.md",
},
}
11 changes: 7 additions & 4 deletions src/bench.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,20 @@ pub fn main() !void {
};

var arena = std.heap.ArenaAllocator.init(gpa.allocator());
defer arena.deinit();

const allocator = arena.allocator();
defer arena.deinit();

// Standard output
const std_out = std.io.getStdOut();
var buf_writer = std.io.bufferedWriter(std_out.writer());
const writer = buf_writer.writer();

// Number generator
var prng = std.rand.DefaultPrng.init(0);
// PRNG
var prng = std.Random.DefaultPrng.init(blk: {
var seed: u64 = undefined;
try std.posix.getrandom(std.mem.asBytes(&seed));
break :blk seed;
});
const random = prng.random();

// "Sequence" benchmark
Expand Down
Loading

0 comments on commit 9951c8e

Please sign in to comment.