Skip to content

Commit 4585354

Browse files
authored
Merge pull request #7 from zigcc/202308
draft 202308
2 parents 822a9c1 + d96db77 commit 4585354

File tree

2 files changed

+151
-2
lines changed

2 files changed

+151
-2
lines changed

content/monthly/202304.org

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
#+TITLE: 202304 | 首次闯入 Tiobe 前 50
22
#+DATE: 2023-05-03T10:31:04+0800
3-
#+LASTMOD: 2023-05-07T07:35:43+0800
3+
#+LASTMOD: 2023-09-03T20:10:11+0800
44

55
* 重大事件
66
在 2023 四月份的 [[https://www.tiobe.com/tiobe-index/][Tiobe]] 指数上,Zig [[https://www.techrepublic.com/article/tiobe-index-language-rankings/][排名 46]],尽管 Loris 发推表示这个数字对 Zig 来说没什么实际意义,但对于多数吃瓜群众来说,这还是十分让人鼓舞的。
77

8-
{{< tweet user="croloris" id="1646555550358831131" >}}
8+
#+begin_quote
9+
For people who heard about Zig just recently:
10+
11+
- Zig is not 2x faster than Rust, despite what recent benchmarks might lead you to believe.
12+
13+
- You won't find many Zig jobs for a few years still, despite the Tiobe stuff.
14+
15+
- Don't join to the Zig community just to rant about Rust.
16+
17+
— Loris Cro ⚡ (@croloris) [[https://twitter.com/croloris/status/1646555550358831131][April 13, 2023]]
18+
#+end_quote>
919
* 观点/教程
1020
- [[https://zig.news/kristoff/when-should-i-use-an-untagged-union-56ek][When should I use an UNTAGGED Union?]] :: Loris 的文章,作者利用访问 untagged union 的未赋值字段是一种 safety-checked UB 的行为,来解决数组成员被重新赋值过的情况。
1121
- [[https://zig.news/rutenkolk/data-driven-polymorphism-45bk][Data driven polymorphism]] :: 作者用 Zig 来实现 Clojure 语言中的 [[https://clojuredocs.org/clojure.core/defmulti][defmulti]],以达到『动态派发』的效果

content/monthly/202308.org

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#+TITLE: 202308 | 0.11 正式发布
2+
#+DATE: 2023-09-03T19:38:04+0800
3+
#+LASTMOD: 2023-09-04T22:05:21+0800
4+
* [[https://ziglang.org/download/0.11.0/release-notes.html#x86-Backend][0.11 正式发布]]
5+
0.11 终于在 8 月 4 号释出了,下面来看看它的一些重要改进吧。[[https://news.ycombinator.com/item?id=36995735][HN 讨论]]
6+
** Peer Type Resolution Improvements
7+
对等类型解析算法得到改进,下面是一些在 0.10 中不能解析,但在 0.11 中可以解析的例子:
8+
| Peer Types | Resolved Type |
9+
| [:s]const T, []T | []const T |
10+
| E!*T, ?*T | E!?*T |
11+
| [*c]T, @TypeOf(null) | [*c]T |
12+
| ?u32, u8 | ?u32 |
13+
| [2]u32, struct { u32, u32 } | [2]u32 |
14+
| *const @TypeOf(.{}), []const u8 | []const u8 |
15+
16+
而且现在使用 =@intCast= 这类 builtin 都只接受一个参数,目前类型根据上下文自动推断出来。
17+
** Multi-Object For Loops
18+
可以同时对多个对象进行遍历:
19+
#+begin_src zig
20+
// 之前
21+
for (input) |x, i| {
22+
output[i] = x * 2;
23+
}
24+
25+
// 现在
26+
for (input, 0..) |x, i| {
27+
output[i] = x * 2;
28+
}
29+
#+end_src
30+
** @min and @max
31+
主要有两个改动:
32+
1. 这两个 builtin 现在支持任意多个参数
33+
2. 返回的类型,会尽可能的紧凑
34+
#+begin_src zig
35+
test "@min/@max refines result type" {
36+
const x: u8 = 20; // comptime-known
37+
var y: u64 = 12345;
38+
// Since an exact bound is comptime-known, the result must fit in a u5
39+
comptime assert(@TypeOf(@min(x, y)) == u5);
40+
41+
var x_rt: u8 = x; // runtime-known
42+
// Since one argument to @min is a u8, the result must fit in a u8
43+
comptime assert(@TypeOf(@min(x_rt, y)) == u8);
44+
}
45+
#+end_src
46+
** @inComptime
47+
新加的 builtin,用于判断执行是否在 comptime 环境下执行:
48+
#+begin_src zig
49+
const global_val = blk: {
50+
assert(@inComptime());
51+
break :blk 123;
52+
};
53+
54+
comptime {
55+
assert(@inComptime());
56+
}
57+
58+
fn f() u32 {
59+
if (@inComptime()) {
60+
return 1;
61+
} else {
62+
return 2;
63+
}
64+
}
65+
66+
test "@inComptime" {
67+
try expectEqual(true, comptime @inComptime());
68+
try expectEqual(false, @inComptime());
69+
try expectEqual(@as(u32, 1), comptime f());
70+
try expectEqual(@as(u32, 2), f());
71+
}
72+
#+end_src
73+
** 类型转化相关 builtin 的重命名
74+
之前 =@xToY= 形式的 builtin 现在已经改成了 =@yFromX= ,这样的主要好处是便于阅读(从右向左),这是[[https://github.com/ziglang/zig/issues/6128][草案]]。
75+
** Tuple 类型声明
76+
现在可以直接用无 field 名字的 struct 来声明 tuple 类型:
77+
#+begin_src zig
78+
test "tuple declarations" {
79+
const T = struct { u32, []const u8 };
80+
var t: T = .{ 1, "foo" };
81+
try expect(t[0] == 1);
82+
try expectEqualStrings(t[1], "foo");
83+
84+
var mul = t ** 3;
85+
try expect(@TypeOf(mul) != T);
86+
try expect(mul.len == 6);
87+
try expect(mul[2] == 1);
88+
try expectEqualStrings(mul[3], "foo");
89+
90+
var t2: T = .{ 2, "bar" };
91+
var cat = t ++ t2;
92+
try expect(@TypeOf(cat) != T);
93+
try expect(cat.len == 4);
94+
try expect(cat[2] == 2);
95+
try expectEqualStrings(cat[3], "bar");
96+
}
97+
#+end_src
98+
99+
之前只能用 =std.meta.Tuple= 函数来定义:
100+
#+begin_src diff
101+
- const testcases = [_]std.meta.Tuple(&[_]type{ []const u8, []const u8, bool }){
102+
+ const testcases = [_]struct { []const u8, []const u8, bool }{
103+
#+end_src
104+
** 排序
105+
现在排序算法分布两类:
106+
- 稳定,blocksort 算法
107+
- 不稳定,[[https://github.com/ziglang/zig/pull/15412][pdqsort]] 算法,它结合了随机快速排序的快速平均情况和堆排序的快速最坏情况。
108+
与堆排的快速最差情况相结合,同时在具有特定模式的输入上达到线性时间。
109+
** Stack Unwinding
110+
Zig 之前依赖 [[https://en.wikipedia.org/wiki/Call_stack#Stack_and_frame_pointers][frame pointer]] 来做堆栈回卷,但它本身有些代价,因此线上环境可能会通过 =-fomit-frame-pointer= 将其禁用掉。
111+
112+
为了在这种情况下依然能够获取 panic 是的堆栈信息,Zig 现在支持了通过 DWARF unwind tables 和 MachO compact unwind information 来会恢复堆栈,详见:[[https://github.com/ziglang/zig/pull/15823][#15823]]。
113+
** 包管理
114+
0.11 首次正式引入了包管理器,具体解释可以参考:[[https://en.liujiacai.net/2023/04/13/zig-build-system/][Zig Build System]],而且很重要一点,step 之间可以[[https://ziglang.org/download/0.11.0/release-notes.html#Steps-Run-In-Parallel][并发执行]],
115+
** Bootstrapping
116+
C++ 实现的 Zig 编译器已经被彻底移除,这意味着 =-fstage1= 不再生效,Zig 现在只需要一个 2.4M 的 WebAssembly 文件和一个 C 编辑器即可,工作细节可以参考:[[https://ziglang.org/news/goodbye-cpp/][Goodbye to the C++ Implementation of Zig]]。
117+
** 代码生成
118+
虽然 Zig 编译器现在还是主要使用 LLVM 来进行代码生成,但在这次发布中,其他几个后端也有了非常大的进步:
119+
1. C 后端,行为测试通过 98%,而且生成的 C 代码兼容微软的 MSVC,用在了 bootstrapping 中
120+
2. x86 后端,行为测试通过 88%
121+
3. aarch64 后端,刚开始
122+
4. WebAssembly 后端,行为测试通过 86%,
123+
5. SPIR-V 后端,SPIR-V 是在 GPU 上运行的着色器(shader)和内核的字节码表示法。目前,Zig 的 SPIR-V 后端专注于为 OpenCL 内核生成代码,不过未来可能也会支持兼容 Vulkan 的着色器。
124+
** 增量编译
125+
虽然这仍是一个高度 WIP 的功能,但这一版本周期中的许多改进为编译器的增量编译功能铺平了道路。其中最重要的是 [[https://github.com/ziglang/zig/pull/15569][InternPool]]。Zig 用户大多看不到这一改动,但它为编译器带来了许多好处,其中之一就是我们现在更接近增量编译了。增量编译将是 0.12.0 发布周期的重点。
126+
* 观点/教程
127+
- [[https://www.aolium.com/karlseguin/4013ac14-2457-479b-e59b-e603c04673c8][Error Handling In Zig]] :: 又一篇讨论错误处理的文章
128+
- [[https://www.1a-insec.net/blog/10-type-magic-in-zig/][Commiting Type Crimes in Zig]] :: 对 Zig 类型系统的另一种用法,有些和[[https://zh.wikipedia.org/wiki/%E9%82%B1%E5%A5%87%E6%95%B0][邱奇数]]类似。
129+
- [[https://www.youtube.com/watch?v=kxT8-C1vmd4][Zig in 100 Seconds]] :: Zig 宣传视频
130+
- [[https://www.youtube.com/watch?v=vKKTMBoxpS8][Zig Build System & How to Build Software From Source • Andrew Kelley • GOTO 2023]] :: Andrew 关于构建系统的视频,[[https://www.bilibili.com/video/BV1Mh4y1K7yc/][B 站链接]]、[[https://www.youtube.com/watch?v=vKKTMBoxpS8][Youbute]]
131+
- [[https://rbino.com/posts/wrap-your-nif-with-zig/][Wrap your NIF with Zig]] :: NIF 是 Elixir 中进行 FFI 调用的方式,如果用原生 C 接口来用,会需要写很多胶水代码,
132+
作者这里用 comptime 特性来定义了一个 =make_nif_wrapper= 来简化 NIF 的实现,这个技巧在与 C 项目交互时十分有用。
133+
- [[https://matklad.github.io/2023/08/09/types-and-zig.html][Types and the Zig Programming Language]] :: matklad 对 Zig 类型系统的总结
134+
- [[https://andrewkelley.me/post/goodbye-twitter-reddit.html][So Long, Twitter and Reddit]] :: Andrew 的最新文章,远离社交平台!
135+
- [[https://zig.news/edyu/wtf-is-zig-comptime-and-inline-257b][WTF is Zig Comptime (and Inline)]] ::
136+
- [[https://double-trouble.dev/post/zbench/][Taking off with Zig: Putting the Z in Benchmark — Double Trouble]] ::
137+
* 项目/工具
138+
- [[https://devlog.hexops.com/2023/mach-v0.2-released/][Mach v0.2 released]]
139+
* [[https://github.com/ziglang/zig/pulls?page=1&q=+is%3Aclosed+is%3Apr+closed%3A2023-07-01..2023-08-01][Zig 语言更新]]

0 commit comments

Comments
 (0)