Commit graph

37226 commits

Author SHA1 Message Date
Justus Klausecker
2fa2300ba4 std.heap.ArenaAllocator: Get rid of cmpxchg loop in hot path
This is achieved by bumping `end_index` by a large enough amount so that
a suitably aligned region of memory can always be provided. The potential
wasted space this creates is then recovered by a single cmpxchg. This is
always successful for single-threaded arenas which means that this version
still behaves exactly the same as the old single-threaded implementation
when only being accessed by one thread at a time. It can however fail when
another thread bumps `end_index` in the meantime. The observerd failure
rates under extreme load are:

2 Threads: 4-5%
3 Threads: 13-15%
4 Threads: 15-17%
5 Threads: 17-18%
6 Threads: 19-20%
7 Threads: 18-21%

This version offers ~25% faster performance under extreme load from 7 threads,
with diminishing speedups for less threads. The performance for 1 and 2
threads is nearly identical.
2026-02-26 15:30:55 +01:00
Motiejus Jakštys
56253d9e31 x86_64 codegen: fix RoundMode vroundss immediate value
Quoting Vol. 2B 4-590 of [Intel SSA manual][1]:

> Bit 3 of the immediate byte controls processor behavior for a
precision exception <...>

The Direction struct is incorrectly sized to 4 bits, which pushes the
precision exception bit to the reserved bits, which helpfully crashes
under valgrind (but works on real hardware, since CPU ignores it):

``
const std = @import("std");

noinline fn ceil(x: f32) f32 {
    return @ceil(x);
}

test "ceil" {
    try std.testing.expectEqual(@as(f32, 2.0), ceil(1.5));
}
```

With Zig 0.15.1:

```
$ zig test -fno-llvm -mcpu x86_64_v3  -fvalgrind  ceil_test.zig  --test-cmd valgrind --test-cmd --quiet --test-cmd-bin
Illegal instruction at address 0x102d14d
/home/motiejus/code/ceil_test.zig:4:5: 0x102d14d in ceil (ceil_test.zig)
    return @ceil(x);
    ^
/home/motiejus/code/ceil_test.zig:8:52: 0x102d097 in test.ceil (ceil_test.zig)
    try std.testing.expectEqual(@as(f32, 2.0), ceil(1.5));
                                                   ^
/nix/store/n81qdpd96d86ngfv5bqymy26b8kqm654-zig-0.15.1/lib/compiler/test_runner.zig:218:25: 0x1167e80 in mainTerminal (test_runner.zig)
        if (test_fn.func()) |_| {
                        ^
/nix/store/n81qdpd96d86ngfv5bqymy26b8kqm654-zig-0.15.1/lib/compiler/test_runner.zig:66:28: 0x11610a1 in main (test_runner.zig)
        return mainTerminal();
                           ^
/nix/store/n81qdpd96d86ngfv5bqymy26b8kqm654-zig-0.15.1/lib/std/start.zig:618:22: 0x115ae3d in posixCallMainAndExit (std.zig)
            root.main();
                     ^
/nix/store/n81qdpd96d86ngfv5bqymy26b8kqm654-zig-0.15.1/lib/std/start.zig:232:5: 0x115a6d1 in _start (std.zig)
    asm volatile (switch (native_arch) {
    ^
???:?:?: 0x0 in ??? (???)
error: the following test command crashed:
valgrind --quiet /home/motiejus/.cache/zig/o/7cc564b5410fc3facdb6e8768643074e/test
```

Zig 0.15.1 with this patch:

```
zig/zig3 test -fno-llvm -mcpu x86_64_v3  -fvalgrind  ceil_test.zig  --test-cmd valgrind --test-cmd --quiet --test-cmd-bin
All 1 tests passed.
```

Looking through `CodeGen.zig` it _seems_ like the same RoundMode struct
is used for vroundss/vroundps/vcvtps2ph, so update the comment while
we're at it. But I am only 90% sure it's true.

[1]: https://cdrdv2.intel.com/v1/dl/getContent/671200
2026-02-25 20:34:08 +01:00
Andrew Kelley
e0173c2ce0 Merge pull request 'rework fuzz testing to be smith based' (#31205) from gooncreeper/zig:integrated-smith into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31205
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-02-25 20:23:36 +01:00
Kendall Condon
bb304796f4 optimize flate decompression
Matches now use memcpy and memset when possible.

Block loops have been rewritten to be more optimizer friendly.

Reworks Symbol and HuffmanDecoder
* Symbol now only includes the value and number of code bits.
  decodeSymbol returns only the value.
* HuffmanDecoder now takes the regular bits instead of the reversed.
* Code table construction now uses buckets instead of sorting.
* For linked codes, the value field of Symbol is now used as the next
  index. The actual value is the element index.
* InvalidCode is now detected only once with a special linked index.

Performance is 39.7% faster than before and 1.1% faster than gzip using
a sample created from compressing a tar of the src directory.
2026-02-25 20:05:48 +01:00
Ivan Agafonov
333055ced7 langref: specific expectEqual* functions instead of expect 2026-02-25 19:34:30 +01:00
Justus Klausecker
a3a9dc111d std.heap.ArenaAllocator: make it threadsafe
Modifies the `Allocator` implementation provided by `ArenaAllocator` to be
threadsafe using only atomics and no synchronization primitives locked
behind an `Io` implementation.

At its core this is a lock-free singly linked list which uses CAS loops to
exchange the head node. A nice property of `ArenaAllocator` is that the
only functions that can ever remove nodes from its linked list are `reset`
and `deinit`, both of which are not part of the `Allocator` interface and
thus aren't threadsafe, so node-related ABA problems are impossible.

There *are* some trade-offs: end index tracking is now per node instead of
per allocator instance. It's not possible to publish a head node and its
end index at the same time if the latter isn't part of the former.

Another compromise had to be made in regards to resizing existing nodes.
Annoyingly, `rawResize` of an arbitrary thread-safe child allocator can
of course never be guaranteed to be an atomic operation, so only one
`alloc` call can ever resize at the same time, other threads have to
consider any resizes they attempt during that time failed. This causes
slightly less optimal behavior than what could be achieved with a mutex.
The LSB of `Node.size` is used to signal that a node is being resized.
This means that all nodes have to have an even size.

Calls to `alloc` have to allocate new nodes optimistically as they can
only know whether any CAS on a head node will succeed after attempting it,
and to attempt the CAS they of course already need to know the address of
the freshly allocated node they are trying to make the new head.
The simplest solution to this would be to just free the new node again if
a CAS fails, however this can be expensive and would mean that in practice
arenas could only really be used with a GPA as their child allocator. To
work around this, this implementation keeps its own free list of nodes
which didn't make their CAS to be reused by a later `alloc` invocation.
To keep things simple and avoid ABA problems the free list is only ever
be accessed beyond its head by 'stealing' the head node (and thus the
entire list) with an atomic swap. This makes iteration and removal trivial
since there's only ever one thread doing it at a time which also owns all
nodes it's holding. When the thread is done it can just push its list onto
the free list again.

This implementation offers comparable performance to the previous one when
only being accessed by a single thread and a slight speedup compared to
the previous implementation wrapped into a `ThreadSafeAllocator` up to ~7
threads performing operations on it concurrently.
(measured on a base model MacBook Pro M1)
2026-02-25 19:12:35 +01:00
hemisputnik
2f8e660805 std.math.log10: handle comptime_int inputs correctly
also add a few tests for comptime types

fixes #31333
2026-02-25 18:57:58 +01:00
Andrew Kelley
608b07a3d7 Merge pull request 'fix std.heap.PageAllocator to not intrude on stacks + re-enable LoongArch CI' (#31271) from alexrp/zig:page-allocator-fixes into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31271
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-02-24 21:24:50 +01:00
Toufiq Shishir
784e89fd4b fix: remove null argument from std.debug.lockStderr call inside std.json.Value.dump 2026-02-24 04:23:25 +01:00
Justus Klausecker
360bc28c96 fix cmpxchg behavior test
This has to be a `@cmpxchgStrong` instead of a `@cmpxchgWeak` otherwise
this test will fail spuriously on LL/SC architectures like PowerPC.
2026-02-23 21:13:26 +01:00
Frank Denis
ee82d926a6 Merge pull request 'Expose the elligator map for Curve25519' (#31318) from jedisct1/zig:elligator-curve25519 into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31318
2026-02-23 19:53:09 +01:00
Frank Denis
e8ca9229c8 Expose the elligator map for Curve25519
This is the same as for Edwards25519 without the y coordinate,
since it returns Montgomery coordinates, but it can be confusing
to call the Edwards25519 function while working on the
Curve25519 representation.

New protocols such as CPACE requires the map over Curve25519.
2026-02-23 13:04:58 +01:00
George Huebner
27ef6e9a9b bpf: add missing helpers 2026-02-23 02:27:13 +01:00
Yusuf Bham
de6c0d500f uefi(guid): don't use @byteSwap in fmt, adjust fmt test
@byteSwap shouldn't be called since we're formatting
ints again, and UEFI only officially supports LE.
2026-02-22 23:13:33 +01:00
inge4pres
29e52aa040 lib: add cgroup BPF helpers
Add bpf_cgrp_storage_get and bpf_cgrp_storage_delete from
libbpf helpers.

Signed-off-by: inge4pres <fgualazzi@gmail.com>
2026-02-22 20:24:51 +01:00
Jacob Young
d7f90722f7 x86_64: fix parsing of sib operands 2026-02-21 18:10:50 -05:00
Alex Rønne Petersen
bd80ad4647
Revert "ci: disable loongarch64-linux"
This reverts commit f061c0dc28.
2026-02-21 23:39:34 +01:00
Alex Rønne Petersen
b5bcbf2a62
std.heap.DebugAllocator: make BucketHeader.fromPage() use wrapping arithmetic
If we've allocated the very last page in the address space then these operations
will overflow and underflow respectively - which is fine.
2026-02-21 23:39:34 +01:00
Matthew Lugg
a9d18c4a0c
std.heap.PageAllocator: avoid mremaps which may reserve potential stack space
Linux's approach to mapping the main thread's stack is quite odd: it essentially
tries to select an mmap address (assuming unhinted mmap calls) which do not
cover the region of virtual address space into which the stack *would* grow
(based on the stack rlimit), but it doesn't actually *prevent* those pages from
being mapped. It also doesn't try particularly hard: it's been observed that the
first (unhinted) mmap call in a simple application is usually put at an address
which is within a gigabyte or two of the stack, which is close enough to make
issues somewhat likely. In particular, if we get an address which is close-ish
to the stack, and then `mremap` it without the MAY_MOVE flag, we are *very*
likely to map pages in this "theoretical stack region". This is particularly a
problem on loongarch64, where the initial mmap address is empirically only
around 200 megabytes from the stack (whereas on most other 64-bit targets it's
closer to a gigabyte).

To work around this, we just need to avoid mremap in some cases. Unfortunately,
this system call isn't used too heavily by musl or glibc, so design issues like
this can and do exist without being caught. So, when `PageAllocator.resize` is
called, let's not try to `mremap` to grow the pages. We can still call `mremap`
in the `PageAllocator.remap` path, because in that case we can set the
`MAY_MOVE` flag, which empirically appears to make the Linux kernel avoid the
problematic "theoretical stack region".
2026-02-21 23:39:34 +01:00
Alex Rønne Petersen
c8dd050305
std.heap.PageAllocator: hint mmaps in the same direction as stack growth
The old logic was fine for targets where the stack grows up (so, literally just
hppa), but problematic on targets where it grows down, because we could hint
that we wanted an allocation to happen in an area of the address space that the
kernel expects to be able to expand the stack into. The kernel is happy to
satisfy such a hint despite the obvious problems this leads to later down the
road.

Co-authored-by: rpkak <rpkak@noreply.codeberg.org>
2026-02-21 23:39:20 +01:00
Andrew Kelley
cd02b1703b Merge pull request 'std.Io.Select: add awaitMany, documentation, and unit test; remove outstanding field' (#31296) from select-enhancement into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31296
2026-02-21 05:22:20 +01:00
Andrew Kelley
311bba4af0 std.Io.Select: add awaitMany function and unit test
and fix documentation. these functions are in fact threadsafe.
2026-02-20 17:28:33 -08:00
Andrew Kelley
f9053f38e5 std.Io.Select: add documentation 2026-02-20 17:08:13 -08:00
Andrew Kelley
54eb03cbf6 std.Io.Select: remove "outstanding" field
it is not fundamentally part of this abstraction
2026-02-20 16:42:37 -08:00
Andrew Kelley
5ac6ff43d4 Merge pull request 'Io.Select: cancelation and concurrent' (#30836) from blblack/zig:select-stuff into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30836
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-02-21 01:29:06 +01:00
Andrew Kelley
6a9510c0eb Merge pull request 'Make std.PriorityDequeue an unmanaged container' (#31273) from saurabh/zig:unmanaged-priority-dequeue into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31273
2026-02-20 07:11:57 +01:00
John Benediktsson
14d06330e0 std.c: fix SO_LINGER on darwin and define SO_LINGER_SEC 2026-02-20 05:36:46 +01:00
David Rubin
8259d8d631 minimal 2026-02-20 05:34:54 +01:00
Alex Rønne Petersen
e95132476d std.os.linux: fix the mmap2 unit for various architectures
closes https://codeberg.org/ziglang/zig/issues/31033
2026-02-20 04:56:07 +01:00
Lukas Lalinsky
7dddcd043f Add lr and sp clobbers for arm/thumb 2026-02-20 01:16:33 +01:00
Yusuf Bham
1e26d885d3 uefi(guid): don't use std.mem.asBytes in format
Using std.mem.asBytes causes a panic when format
is called as slices can no longer have hex options
when formatting, so instead just print the ints.
2026-02-18 20:19:50 -05:00
Saurabh Mishra
5140455264 min heap is formed with less than comparison 2026-02-18 13:24:03 -08:00
Saurabh Mishra
293710c1cd use .empty for default field values intead of init() and introduce initContext() 2026-02-18 12:38:26 -08:00
Saurabh Mishra
23252c27be merge popMinOrNull into popMin and popMaxOrNull into popMax, and update tests 2026-02-18 11:58:13 -08:00
Saurabh Mishra
535722b374 rename add to push and remove to pop in methods and tests 2026-02-18 11:43:57 -08:00
Saurabh Mishra
d297f7b8f2 make priority dequeue an unmanaged container and ensure tests pass 2026-02-18 11:20:32 -08:00
Marcel W. Wysocki
0e3c6514a4 link: recognize thin archives in ld script detection
Needed for linking the Linux kernel.
2026-02-17 23:15:32 +01:00
Alex Rønne Petersen
0fd8dc8ea6 ci: update to qemu 10.2.1 2026-02-17 19:16:25 +01:00
Marcel W. Wysocki
0e45a44991 cc: disable implicit runtimes with -nostdlib, matching clang 2026-02-16 19:42:13 +08:00
Andrew Kelley
27eec9bd62 Revert "Allow overriding std.Io at a namespace level."
This reverts commit e314dadb01.

This idea requires more consideration before committing to it.
At the very least let's not regress autodocs.

Closes #31230
2026-02-15 20:26:12 -08:00
vitalii
1838134200 fix segfault when /etc/hosts missing last newline 2026-02-15 23:12:13 +01:00
rpkak
17dd2f0a85 spirv: implement AIR tag struct_field_ptr 2026-02-15 23:09:24 +01:00
Iku Iwasa
8c83acd3d4 Add missing EV flags for NetBSD
- Add FLAG1, ERROR and EOF.
2026-02-15 23:08:11 +01:00
Andrew Kelley
c8f54a2f07 Merge pull request 'std.Io: remove select function' (#31223) from remove-select into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31223
2026-02-15 19:29:21 +01:00
Jacob Young
5763f7dbcc std.Io.Evented: clean up supporting code for select 2026-02-15 05:52:07 -05:00
Andrew Kelley
d9fc7fa04d std.Io: remove select function
This function works with a slice of futures and returns the index of a
completed one. This doesn't work very well in practice because it's
either too high level or too low level.

At the lower level we have Io.Batch for doing this kind of thing at the
Operation API layer.

At the higher level we have Io.Select which is a convenience wrapper
around an Io.Group and an Io.Queue.
2026-02-14 18:31:48 -08:00
Andrew Kelley
c6eeae8a8c Merge pull request 'std.math.acos/asin: Add @Vector(?,f32) and @Vector(?,f64) support' (#31209) from lzm-build/zig:acos_asin_simd into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/31209
Reviewed-by: Andrew Kelley <andrew@ziglang.org>
2026-02-14 23:35:03 +01:00
Jacob Young
771047ae93 Io.Dispatch.sleep: add cancelation support 2026-02-14 16:26:49 -05:00
Jacob Young
b7f93695f9 Io.Dispatch.Mutex: fix deadlock conditions 2026-02-14 05:52:59 -05:00
lzm-build
51509fe63b std.math.acos/asin: Add @Vector(?,f32) and @Vector(?,f64) support 2026-02-14 16:57:05 +08:00