Commit Graph
16 Commits
Author SHA1 Message Date
Paul Anderson af24fd11a5 std.sort.pdq: push larger partition to stack to avoid stack overflow 2026-09-05 17:50:56 +02:00
Andrew Kelley ae4e835a66 std.lang: rename OptimizeMode to Optimize
and remove "release" from the enum tag names.

no functional change, however, despite the addition of
backwards-compatibile declarations in this patch, it is breaking because
expressions that use `==` or `!=` operators will not able to use the
deprecated names.

I predict these names to survive until the 1.0 tag without any more
breakage.
2026-07-20 17:26:50 -07:00
Ali Cheraghi 57ca3512e4 std.sort.pdq: fix stack overflow on 32-bit targets
our pdq implementation uses an explicit stack of `Range` entries to avoid recursion.
The size of this stack was set to `log2(maxInt(usize) + 1)`. on 32-bit
targets like `wasm32-freestanding`, this would hit the maximum size,
causing OOB. there were two issues:

- `max_limit`, which controls how many imbalanced partitions are allowed
  before the algorithm falls back to heapsort, was computed as
  `floorPowerOfTwo(n) + 1` instead of `log2(n)` as in the original c++
  reference implementation. e.g. for `n = 1_000_000`, this allowed 524289
  bad partitions before heapsort would kick in, instead of 19.
  this meant deeply imbalanced partitions could accumulate stack pushes
  essentially without limit. However, i didn't observe any meaningful
  difference in benchmarks.

- the worst-case stack depth is bounded by `log2(n) + max_limit`,
  which approaches `2 * log2(n)`. The reference c++ and go implementation
  doesn't have this problem because they don't use explicit stack buffer.
2026-06-11 05:40:50 +02:00
Frank Denis ef7f828338 std.sort.pdq: use block-based branchless partitioning
Implement BlockQuicksort (was a TODO item).

This avoids branch mispredictions on random data, yielding ~2x
speedup on large random arrays while leaving pre-sorted and
equal-element fast paths unaffected.
2026-05-29 05:48:58 +02:00
Frank Denis 0569f1f6a7 std.sort.pdq: use unguarded insertion for non-leftmost partitions
After partitioning, the pivot acts as a sentinel for the right
sub-partition, guaranteeing termination without a bounds check.

So we can skip the `j > a` check for all non-leftmost partitions.

Give a consistent ~10-50% speedup, with the biggest gains at smaller
sizes.
2026-05-29 05:47:35 +02:00
Frank Denis 6dd0270a19 std.sort.pdq: fix out-of-bounds access in partialInsertionSort (#25253)
* std.sort.pdq: fix out-of-bounds access in partialInsertionSort

When sorting a sub-range that doesn't start at index 0, the
partialInsertionSort function could access indices below the range
start. The loop condition `while (j >= 1)` didn't respect the
arbitrary range boundaries [a, b).

This changes the condition to `while (j > a)` to ensure indices
never go below the range start, fixing the issue where pdqContext
would access out-of-bounds indices.

Fixes #25250
2025-09-17 19:54:15 -07:00
mlugg 6808ce27bd compiler,lib,test,langref: migrate @setCold to @branchHint 2024-08-27 00:44:35 +01:00
Jora Troosh 13070448f5 std: fix typos (#20560) 2024-07-09 14:25:42 -07:00
mlugg 51595d6b75 lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
JustinWayland c45af2af61 Fix simple doc mistakes. (#17624)
* Add missing period in Stack's description

This looks fine in the source, but looks bad when seen on the documentation website.

* Correct documentation for attachSegfaultHandler()

The description for attachSegfaultHandler() looks pretty bad without indicating that the stuff at the end is code

* Added missing 'the's in Queue.put's documentation

* Fixed several errors in Stack's documentation

`push()` and `pop()` were not styled as code

There was no period after `pop()`, which looks bad on the documentation.

* Fix multiple problems in base64.zig

Both "invalid"s in Base64.decoder were not capitalized.

Missing period in documentation of Base64DecoderWithIgnore.calcSizeUpperBound.

* Fix capitalization typos in bit_set.zig

In DynamicBitSetUnmanaged.deinit's and DynamicBitSet.deinit's documentation, "deinitializes" was uncapitalized.

* Fix typos in fifo.zig's documentation

Added a previously missing period to the end of the first line of LinearFifo.writableSlice's documentation.
Added missing periods to both lines of LinearFifo.pump's documentation.

* Fix typos in fmt.bufPrint's documentation

The starts of both lines were not capitalized.

* Fix minor documentation problems in fs/file.zig

Missing periods in documentation for Permissions.setReadOnly, PermissionsWindows.setReadOnly, MetadataUnix.created, MetadataLinux.created, and MetadataWindows.created.

* Fix a glaring typo in enums.zig

* Correct errors in fs.zig

* Fixed documentation problems in hash_map.zig

The added empty line in verify_context's documentation is needed, otherwise autodoc for some reason assumes that the list hasn't been terminated and continues reading off the rest of the documentation as if it were part of the second list item.

* Added lines between consecutive URLs in http.zig

Makes the documentation conform closer to what was intended.

* Fix wrongfully ended sentence in Uri.zig

* Handle wrongly entered comma in valgrind.zig.

* Add missing periods in wasm.zig's documentation

* Fix odd spacing in event/loop.zig

* Add missing period in http/Headers.zig

* Added missing period in io/limited_reader.zig

This isn't in the documentation due to what I guess is a limitation of autodoc, but it's clearly supposed to be. If it was, it would look pretty bad.

* Correct documentation in math/big/int.zig

* Correct formatting in math/big/rational.zig

* Create an actual link to ZIGNOR's paper.

* Fixed grammatical issues in sort/block.zig

This will not show up in the documentation currently.

* Fix typo in hash_map.zig
2023-10-21 21:24:55 +00:00
yujiri8 dae516dbdf improve documentation of std.sort.*Context functions (#16145) 2023-06-27 00:51:06 -07:00
Ali Chraghi 6bd5479306 std.sort.block: add safety check for lessThan return value 2023-06-26 17:50:10 -07:00
mlugg f26dda2117 all: migrate code to new cast builtin syntax
Most of this migration was performed automatically with `zig fmt`. There
were a few exceptions which I had to manually fix:

* `@alignCast` and `@addrSpaceCast` cannot be automatically rewritten
* `@truncate`'s fixup is incorrect for vectors
* Test cases are not formatted, and their error locations change
2023-06-24 16:56:39 -07:00
r00ster91 2593156068 migration: std.math.{min, min3, max, max3} -> @min & @max 2023-06-16 13:44:09 -07:00
Niles Salter 700ea694b2 Fix pdqSort+heapSort for ranges besides 0..len (#15982) 2023-06-13 16:55:58 -04:00
Ali Chraghi 3db3cf7790 std.sort: add pdqsort and heapsort 2023-05-23 17:55:59 -07:00