128 Commits

Author SHA1 Message Date
Michael Lynch
2a7cedfeb9 Refactor ArrayList replaceRange tests 2024-04-02 13:46:51 -07:00
Ryan Liptak
16b3d1004e Remove redundant test name prefixes now that test names are fully qualified
Follow up to #19079, which made test names fully qualified.

This fixes tests that now-redundant information in their test names. For example here's a fully qualified test name before the changes in this commit:

"priority_queue.test.std.PriorityQueue: shrinkAndFree"

and the same test's name after the changes in this commit:

"priority_queue.test.shrinkAndFree"
2024-02-26 15:18:31 -08:00
Andrew Kelley
17291e072b std.ArrayList: fixedWriter
A writer that appends to the list, returning error.OutOfMemory rather
than attempting to increase capacity.
2024-02-23 02:37:11 -07:00
Andrew Kelley
9129fb28dc std.ArrayList: add writerAssumeCapacity
Useful when you want to use an ArrayList to operate on a static buffer.
2024-02-23 02:37:11 -07:00
Andrew Kelley
827e30634f std.ArrayList: pedantic fixups to previous commit
* fix and clarify incorrect doc comments
* unify the pattern of calling unmanaged methods
2024-01-19 16:24:51 -07:00
Gordon Cassie
4ddd0b1a1b std.ArrayList: add replaceRangeAssumeCapacity method 2024-01-19 16:24:51 -07:00
Erik Arvstedt
c50ba2d101 std.ArrayList.replaceRange: remove unneded overflow checks
The code asserted that the range to be replaced is within bounds of
`self.items`.
This is now reflected in the doc comment.
The old, wrong doc comment was copied from the `insert*` fns.

With this assertion holding true, `start + len` is always within the
address space and `start + new_items.len` is, at this point, always
strictly within bounds of `self.items`.
2024-01-19 00:55:17 +01:00
Erik Arvstedt
0bb6967d14 std.ArrayList: remove + 1 overflow checks 2024-01-19 00:55:17 +01:00
Andrew Kelley
f2721a4cbc std.ArrayList: pedantic rewordings of documentation and unit tests 2024-01-15 23:44:36 -07:00
notcancername
69461bcae4 std.array_list: Document and reduce illegal behavior in ArrayLists 2024-01-15 23:44:36 -07:00
Ryan Liptak
4dd3505e43 std.ArrayList: Clarify that ensureTotalCapacity/ensureTotalCapacityPrecise will never shrink the array
Closes #18499
2024-01-13 19:58:22 -08:00
Andrew Kelley
f6549a956d std.ArrayList: add initBuffer to the unmanaged array list
This is useful when you want to have an array list backed by a fixed
slice of memory and no Allocator will be used.

It's an alternative to BoundedArray as you will see in the following
commit.
2023-11-22 11:32:14 -07:00
mlugg
51595d6b75
lib: correct unnecessary uses of 'var' 2023-11-19 09:55:07 +00:00
Jacob Young
fe93332ba2 x86_64: implement enough to pass unicode tests
* implement vector comparison
 * implement reduce for bool vectors
 * fix `@memcpy` bug
 * enable passing std tests
2023-10-23 22:42:18 -04:00
Jacob Young
27fe945a00 Revert "Revert "Merge pull request #17637 from jacobly0/x86_64-test-std""
This reverts commit 6f0198cadbe29294f2bf3153a27beebd64377566.
2023-10-22 15:46:43 -04:00
Andrew Kelley
6f0198cadb Revert "Merge pull request #17637 from jacobly0/x86_64-test-std"
This reverts commit 0c99ba1eab63865592bb084feb271cd4e4b0357e, reversing
changes made to 5f92b070bf284f1493b1b5d433dd3adde2f46727.

This caused a CI failure when it landed in master branch due to a
128-bit `@byteSwap` in std.mem.
2023-10-22 12:16:35 -07:00
Jacob Young
32e85d44eb x86_64: disable failing tests, enable test-std testing 2023-10-21 10:55:41 -04:00
Lucas Santos
303181901b Improve (Unmanaged)ArrayList.insert
(Unmanaged)ArrayList.insert has the same inefficiency as the old insertSlice. With the new addManyAt, the solution is trivial.
Also improves the test "growing memory preserves contents". In the previous implementation, if any changes were made to the ArrayList memory growth policy (function growMemory), the list could end up with enough capacity to not trigger a memory growth, defeating the purpose of the test. The new implementation more robustly triggers a memory growth.
2023-09-30 16:17:22 -07:00
Andrew Kelley
9013970861 std.ArrayList: fixups for the previous commit
* Move `computeBetterCapacity` to the bottom so that `pub` stuff shows
   up first.
 * Rename `computeBetterCapacity` to `growCapacity`. Every function
   implicitly computes something; that word is always redundant in a
   function name. "better" is vague. Better in what way? Instead we
   describe what is actually happening. "grow".
 * Improve doc comments to be very explicit about when element pointers
   are invalidated or not.
 * Rename `addManyAtIndex` to `addManyAt`. The parameter is named
   `index`; that is enough.
 * Extract some duplicated code into `addManyAtAssumeCapacity` and make
   it `pub`.
 * Since I audited every line of code for correctness, I changed the
   style to my personal preference.
 * Avoid a redundant `@memset` to `undefined` - memory allocation does
   that already.
 * Fixed comment giving the wrong reason for not calling
   `ensureTotalCapacity`.
2023-09-29 13:42:38 -07:00
Lucas Santos
9d765b5ab5 std.ArrayList: insertSlice avoids extra memcpy
Includes a more robust implementation of replaceRange, which updates the
ArrayListUnmanaged if state changes in the managed part of the code
before returning an error.

Co-authored-by: Andrew Kelley <andrew@ziglang.org>
2023-09-29 12:52:40 -07:00
Gregory Anders
cab9da35bd std: enable FailingAllocator to fail on resize
Now that allocator.resize() is allowed to fail, programs may wish to
test code paths that handle resize() failure. The simplest way to do
this now is to replace the vtable of the testing allocator with one
that uses Allocator.noResize for the 'resize' function pointer.

An alternative way to support this testing capability is to augment the
FailingAllocator (which is already useful for testing allocation failure
scenarios) to intentionally fail on calls to resize(). To do this, add a
'resize_fail_index' parameter to the FailingAllocator that causes
resize() to fail after the given number of calls.
2023-09-06 19:06:32 +03:00
Andrew Kelley
a576082170 std: reword some comments 2023-07-16 18:32:52 -07:00
Ryan Liptak
2896266a03 docs: Fix outdated doc comments about allocating 'at least' the requested size
The 'at least' behavior of the Allocator interface was removed in #13666, so anything that used reallocAtLeast or the .at_least Exact behavior could still have doc comments that reference no-longer-true behavior.

Funnily enough, ArrayList is the only place that used this functionality (outside of allocator test cases), so its doc comments are the only things that need to be fixed. This was checked by resetting to deda6b514691c3a7ffc7931469886d0e7be2f67e and searching for all instances of `reallocAtLeast` and `.at_least` (one of which would need to be used to get the `.at_least` behavior)
2023-07-12 21:54:30 -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
Jacob Young
6e0de1d116 InternPool: port most of value tags 2023-06-10 20:47:54 -07:00
dweiller
bd3360e03d convert s[start..start+len] to s[start..][0..len] 2023-05-07 15:55:21 +10:00
Andrew Kelley
6261c13731 update codebase to use @memset and @memcpy 2023-04-28 13:24:43 -07:00
Andrew Kelley
57ea6207d3 std.ArrayList: mark the appendNTimes methods inline
The previous commit introduced an optimization to the LLVM backend that
makes `@memset` lower more optimally when the element is comptime-known
and has a repeating byte pattern.

By making these functions inline, if the element parameter is
comptime-known at the callsite, it will be comptime-known in the
`@memset` call, causing more use of the LLVM `memset` intrinsic rather
than an inline for loop when using the LLVM backend.

This affects, for example, std.crypto.argon2, which calls
appendNTimesAssumeCapacity with a `[128]u64` as the element. This is now
lowered with a single `memset` call.
2023-04-28 13:24:43 -07:00
Andrew Kelley
58fabbc903 x86_64 backend: support @memset with slices 2023-04-25 11:23:41 -07:00
Andrew Kelley
7c56145a76 add behavior test for @memset on slices
and avoid new language feature in std.ArrayList for now, until x86_64
self-hosted backend can implement it.
2023-04-25 11:23:41 -07:00
Andrew Kelley
a5c910adb6 change semantics of @memcpy and @memset
Now they use slices or array pointers with any element type instead of
requiring byte pointers.

This is a breaking enhancement to the language.

The safety check for overlapping pointers will be implemented in a
future commit.

closes #14040
2023-04-25 11:23:40 -07:00
zooster
bc8e1e1de4
Improvements to docs and text
* docs(std.math): elaborate on difference between absCast and absInt

* docs(std.rand.Random.weightedIndex): elaborate on likelihood

I think this makes it easier to understand.

* langref: add small reminder

* docs(std.fs.path.extension): brevity

* docs(std.bit_set.StaticBitSet): mention the specific types

* std.debug.TTY: explain what purpose this struct serves

This should also make it clearer that this struct is not supposed to provide unrelated terminal manipulation functionality such as setting the cursor position or something because terminals are complicated and we should keep this struct simple and focused on debugging.

* langref(package listing): brevity

* langref: explain what exactly `threadlocal` causes to happen

* std.array_list: link between swapRemove and orderedRemove

Maybe this can serve as a TLDR and make it easier to decide.

* PrefetchOptions.locality: clarify docs that this is a range

This confused me previously and I thought I can only use either 0 or 3.

* fix typos and more

* std.builtin.CallingConvention: document some CCs

* langref: explain possibly cryptic names

I think it helps knowing what exactly these acronyms (@clz and @ctz) and
abbreviations (@popCount) mean.

* variadic function error: add missing preposition

* std.fmt.format docs: nicely hyphenate

* help menu: say what to optimize for

I think this is slightly more specific than just calling it
"optimizations". These are speed optimizations. I used the word
"performance" here.
2023-04-23 21:06:21 +03:00
-k
0282c2a924 doc: fix minor grammar issues 2023-04-22 13:09:15 +03:00
Isaac Freund
a097779b61 std: Add ArrayList.insertAssumeCapacity()
Also test and document that inserting at list.items.len is allowed.
2023-03-12 11:02:53 +00:00
Andrew Kelley
aeaef8c0ff update std lib and compiler sources to new for loop syntax 2023-02-18 19:17:21 -07:00
Nicolas Goy
b7c96c3bbd Allow const for ArrayList.getLast, fix #14522 2023-02-04 15:24:45 -05:00
Michael Bartnett
99febb54d3 Add fromOwnedSliceSentinel to ArrayList ArrayList and ArrayListUnmanaged, add fromOwnedSlice to ArrayListUnmanaged 2023-01-16 14:22:38 +02:00
Chris Boesch
1de96a2cc4 Add the two functions 'getLast' and 'getLastOrNull' to ArrayListAligned/ArrayListAlignedUnmanaged. 2023-01-03 12:48:20 +02:00
Veikka Tuominen
9a0c593a54 add tests for fixed stage1 bugs
Closes #1957
Closes #1994
Closes #2140
Closes #2746
Closes #2802
Closes #2855
Closes #2895
Closes #2981
Closes #3054
Closes #3158
Closes #3234
Closes #3259
Closes #3371
Closes #3376
Closes #3387
Closes #3529
Closes #3653
Closes #3750
Closes #3778
Closes #3882
Closes #3915
Closes #3929
Closes #3961
Closes #3988
Closes #4123
Closes #7448
2022-12-29 12:42:44 +02:00
Ryan Liptak
83e0e23f8a ArrayList.toOwnedSlice: Fix potential for leaks when using errdefer
#13666 introduced a footgun when using `toOwnedSlice` with `errdefer array_list.deinit()`, since `toOwnedSlice` could retain capacity if `resize` failed, meaning it would leak without `deinit` being called. This meant that the only correct way to use `toOwnedSlice` was with `defer` instead of `errdefer` to ensure that the ArrayList would get cleaned up.

Now, toOwnedSlice will now behave similarly to how it did before #13666, in that it will always clear the ArrayList's capacity if the resize/realloc succeeds.

This also reverts commit 05890a12f532ba9d58904a14381ec174b9efe473, which was contingent on the modified toOwnedSlice behavior.

Closes #13946
2022-12-15 02:56:24 -05:00
Mikael Berthe
05890a12f5 std: Update ArrayList documentation
Calling `deinit` might still be required after using `toOwnedSlice` now.
2022-12-11 17:42:26 +02:00
Martin Wickham
b1c4227763 Allow const ArrayLists to be cloned 2022-12-05 04:36:10 -05:00
Andrew Kelley
16caea38d1 std.ArrayList: fix shrinkAndFree
Fixes a regression introduced in
e35f297aeb993ec956ae80379ddf7f86069e109b.

Now there is test coverage for ArrayList.shrinkAndFree in the case when
resizing fails.
2022-11-30 15:42:59 -07:00
Andrew Kelley
ceb0a632cf std.mem.Allocator: allow shrink to fail
closes #13535
2022-11-29 23:30:38 -07:00
remeh
e7424d5d2a std.array_list: add a comment on every methods invalidating pointers.
While it is already mentioned on the `items` attributes of the structs, it is
interesting to comment in every method potentially invalidating pointers to items
that they may do so.
2022-11-18 14:49:31 +02:00
Andrew Kelley
d3d24874c9 std: remove deprecated API for the upcoming release
See #3811
2022-09-16 14:46:53 -04:00
alichraghi
8c9f468fdd std.ArrayList.ensureTotalCapacity: optimize and fix integer overflow
Fixes #12099
2022-09-14 20:05:15 -04:00
Andrew Kelley
fa620ef710 link.MachO: use accurate alignment attribute on pointers
Also adds a new method to ArrayList: appendUnalignedSlice
2022-08-10 22:28:05 -07:00
Andrew Kelley
0f3e849719 std.ArrayList: use variable for local mutable state
stage1 has the wrong semantics here
2022-03-11 22:47:24 -07:00
Motiejus Jakštys
e620b692c6 [std.ArrayList] return explicit errors
All errors from std.ArrayList are allocation errors. Mark them as such.
This is helpful when one wants to obtain a function pointer.
2022-02-22 13:57:51 -05:00