202404 | Zig 0.12.0 正式释出

发布: 2024-04-18   上次更新: 2024-05-03

文章目录

重大事件

千呼万唤的 0.12.0 版本终于 2024-04-20 正式释出了!这次版本历时 8 个月,有 268 位贡献者,一共进行了 3688 次提交!社区内的一些讨论:Hacker NewsLobsters。 这是它的 Release notes。ZigCC 对这个文档进行了翻译、整理,方便大家阅读:

并且还在 2024-04-27 举行了一次线上的 meetup 来庆祝这次发布,这是会议的总结:0.12.0 Release Party 回顾

0.12.0 这个版本,对用户来说,最重大的变更就是构建系统的稳定了,这对于 Zig 生态的发展是十分关键的一步,试想一个项目用到的依赖之间版本不兼容, 这是十分痛苦的事情,毫无疑问这是阻碍 Zig 生态发生的绊脚石,没有之一。好在这一切都在 0.12 这个版本解决了,用户可以基于 Step 构成的有向无环图来编译自己的项目,不需要再折腾 CMake、Makefile、Vcpkg、Git submodule 等工具,所有的依赖使用 zon 来管理即可。 读者如果对 Zig 构建系统还不熟悉,可以参考:

期待一年后 Zig 的生态!

观点/教程

Zig 中任意精度整数用途与实现

由于 CPU 在访问内存时,一般都会有对齐的要求,对于这种非常规的数字,在内存中的地址会是怎样的呢?可以做一个简单的实验:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
const std = @import("std");

const Foo = packed struct {
  a: u3,
  b: u2,
};

pub fn main() !void {
  const vs = [_]u3{ 1, 2, 3 };
  for (&vs) |*b| {
      std.debug.print("{p}-{b}\n", .{ b, b.* });
  }

  std.debug.print("U3 size: {d}\n", .{@sizeOf(u3)});
  std.debug.print("Foo size: {d}\n", .{@sizeOf(Foo)});

  const foos = [_]Foo{
      .{ .a = 1, .b = 3 },
  };

  std.debug.print("foo as bytes: {b}\n", .{std.mem.sliceAsBytes(&foos)});

  for (foos) |b| {
      std.debug.print("{any}-{any}\n", .{ &b.a, &b.b });
  }
}

输出:

1
2
3
4
5
6
7
u3@104d11a2c-1
u3@104d11a2d-10
u3@104d11a2e-11
U3 size: 1
Foo size: 1
foo as bytes: { 11001 }
u3@16b196367-u2@16b196367

通过前三个输出可以知道,每个 u3 实际占用一个字节,但当用在 packed 结构中,就会变成 3 个 bit。其中的 11001 就是字段 a b 混合后的值,且 a 是三位,b 是高两位。

Learnings From Building a DB in Zig
作者分享了在一次 3 天的 Hackthon 中,使用 Zig 开发一个数据库的经历。
build.zig.zon dependency hashes
讲解了 zon 中依赖的 hash 是怎么计算出来的
play with new comptime var rule of zig 0.12.0
To SIMD and beyond: Optimizing a simple comparison routine
作者在这里循序渐进的介绍了几种数字比较的技巧,从基本的方案,到 Vector,到最后利用 bit 的特点,来逐步优化,并用 godbolt 查看生成的汇编代码,是一篇不错的文章。
Documentation takes another step backwards : r/Zig
一个 Reddit 用户对文档的抱怨

项目/工具

rofrol/zig-companies
A list of companies using Zig in production.
akarpovskii/tuile
A Text UI library for Zig
mntnmntn/zenith
A very minimal text editor in Zig,支持 0.12.0 版本
chung-leong/zigar
Enable the use of Zig code in JavaScript project
jnordwick/zig-string
Zig string library that includes small string optimization on the stack
FalsePattern/ZigBrains
Yet another zig language plugin for intellij

Zig 语言更新

建议/反馈✉️

  1. 关注微信公众号,加微信群与更多人一起畅聊 Zig
  2. 发现内容错误或链接失效?欢迎提交 PR
  3. 想要分享 Zig 使用经验,欢迎给我们供稿