Commit graph

77 commits

Author SHA1 Message Date
aaronp64
4c3f2be16d Avoid repeated _copy_on_write() calls in Array::resize()
Updated Array::resize() to call ptrw() once before looping to initialize typed array elements, instead of accessing each through .write[].
2025-09-15 14:03:21 -04:00
Thaddeus Crews
0f67c3e992
Merge pull request #104522 from Ivorforce/localvector-force-trivial-resize
Core: Add `resize_initialized` and `resize_uninitialized` to `Vector` and `LocalVector`
2025-05-27 09:39:27 -05:00
Lukas Tenbrink
4cb8a0c77e Add resize_initialized and resize_uninitialized to Vector. These functions serve as replacements for resize, to make sure the caller understands whether elements need to be initialized 'by hand' after the call. 2025-05-26 18:35:41 +02:00
Pedro J. Estébanez
342266cfd9 Overhaul Variant::duplicate() for resources
This in the scope of a duplication triggered via any type in the `Variant` realm. that is, the following: `Variant` itself, `Array` and `Dictionary`. That includes invoking `duplicate()` from scripts.

A `duplicate_deep(deep_subresources_mode)` method is added to `Variant`, `Array` and `Dictionary` (for compatibility reasons, simply adding an extra parameter was not possible). The default value for it is `RESOURCE_DEEP_DUPLICATE_NONE`, which is like calling `duplicate(true)`.

Remarks:
- The results of copying resources via those `Variant` types are exactly the same as if the copy were initiated from the `Resource` type at C++.
- In order to keep some separation between `Variant` and the higher-level animal which is `Resource`, `Variant` still contains the original code for that, so it's self-sufficient unless there's a `Resource` involved. Once the deep copy finds a `Resource` that has to be copied according to the duplication parameters, the algorithm invokes the `Resource` duplication machinery. When the stack is unwind back to a nesting level `Variant` can handle, `Variant` duplication logic keeps functioning.

While that is good from a responsibility separation standpoint, that would have a caveat: `Variant` would not be aware of the mapping between original and duplicate subresources and so wouldn't be able to keep preventing multiple duplicates.

To avoid that, this commit also introduces a wormwhole, a sharing mechanism by which `Variant` and `Resource` can collaborate in managing the lifetime of the original-to-duplicates map. The user-visible benefit is that the overduplicate prevention works as broadly as the whole `Variant` entity being copied, including all nesting levels, regardless how disconnected the data members containing resources may be across al the nesting levels. In other words, despite the aforementioned division of duties between `Variant` and `Resource` duplication logic, the duplicates map is shared among them. It's created when first finding a `Resource` and, however how deep the copy was working at that point, the map kept alive unitl the stack is unwind to the root user call, until the first step of the recursion.

Thanks to that common map of duplicates, this commit is able to fix the issue that `Resource::duplicate_for_local_scene()` used to ignore overridden duplicate logic.
2025-05-26 10:06:40 +02:00
Yufeng Ying
3bf400ffae Move bisect to Span and deduplicate code.
Co-authored-by: Lukas Tenbrink <lukas.tenbrink@gmail.com>
2025-05-14 18:19:09 +08:00
mashumafi
b73346ef19 Avoid copy-on-write when reading vectors 2025-05-12 16:08:05 +02:00
aaronp64
d176ba045f Array performance improvements to reduce copying/copy_on_write calls
- Avoid temporary copy of p_array in Array::append_array when types match
- Call ptrw() once before looping in methods that return new Arrays, to avoid copy_on_write call for each item (recursive_duplicate, slice, filter, map)
2025-05-06 13:19:50 -04:00
Thaddeus Crews
b8950a4662
Merge pull request #105392 from aaronp64/array_min_max
Optimize `Array` `min`/`max` methods
2025-04-15 12:28:40 -05:00
aaronp64
ecf63dd079 Optimize Array min/max methods
Updated Array::min and Array::max to not copy Variants for comparison, and store index instead of copying each time new min/max is found.
2025-04-14 15:40:06 -04:00
Windy Darian
effd1b71ce allow inserting at end of array again 2025-04-12 22:16:19 -04:00
Rarysson Guilherme
fe39ffeb7d Add negative index to Array.remove_at and Array.insert 2025-04-08 14:55:39 -03:00
Yufeng Ying
4f4031a675 Replace size() == 0 with is_empty(). 2025-04-02 19:18:43 +08:00
kobewi
75881f8322 Use initializer list in Arrays 2025-03-26 18:38:15 +01:00
Rémi Verschelde
3f14e6a1b7
Merge pull request #104374 from aaronp64/array_swap_move
Optimize `Array` methods with `SWAP`/`std::move`
2025-03-21 13:04:04 +01:00
Thaddeus Crews
f6a35e2a6e
Merge pull request #86015 from kitbdev/array-initializer-list
Add list initialization to Array, Variant, and TypedArray
2025-03-19 17:46:06 -05:00
aaronp64
3eb4e6c6ff Optimize Array methods with SWAP/std::move
Updated Array::shuffle() to use SWAP instead of copying through temporary variables, which makes shuffling simple types around 2x faster, and refcounted object types around 20x faster.

Also updated multiple methods that insert into/modify the array to move their validated Variant to avoid an extra copy, speed increase varies depending on type and how much else the methods are doing, usually around 5-10% faster.
2025-03-19 12:17:14 -04:00
Yufeng Ying
883ca56517 Remove unneeded read_only check for Array const operator. 2025-03-12 07:22:42 +08:00
Yufeng Ying
be86ce3103 Apply iwyu suggestion in core. 2024-12-19 00:43:47 +08:00
Danil Alexeev
bd1a35ce9e
Core: Fix JSON.{from,to}_native() issues 2024-11-29 12:43:06 +03:00
A Thousand Ships
38f9769bc6
[Core] Improve error messages with vformat 2024-10-30 15:55:51 +01:00
Rémi Verschelde
6bf8a3e3f8
Merge pull request #95449 from SlashScreen/array_functions
Add callable support for `find` and `rfind` `Array` methods
2024-09-20 16:06:13 +02:00
Slashscreen
89491f4403 Add callable support for find and rfind Array methods 2024-09-13 00:01:53 -07:00
Hilderin
27d1fb63e1 Fix Unable to use ResourceLoader in C# after threaded load in GDScript #92798 2024-09-11 19:03:55 -04:00
Rémi Verschelde
496b7b8482
Core: Improve vformat error reporting on sprintf failure
And fix a few occurrences of formatting errors that led me to this.
2024-07-04 10:54:55 +02:00
kit
cb17798df3 Add Array and Variant list inializer 2024-05-02 15:35:17 -04:00
A Thousand Ships
64146cb7f3
[Core] Add iteration support to Array 2024-04-10 14:49:34 +02:00
A Thousand Ships
684752e75b
Replace error checks against size with is_empty 2024-02-09 12:50:15 +01:00
Rémi Verschelde
737c308dcc
Merge pull request #74588 from puchik/float-variant-nan-inequality
Support numeric/binary hash comparison for floats derived from Variants (as well as existing semantic comparison)
2023-09-27 14:03:34 +02:00
A Thousand Ships
893f889d74 [Core] Replace ERR_FAIL_COND with ERR_FAIL_NULL where applicable 2023-09-11 19:45:49 +02:00
Arman Elgudzhyan
ee27254602 Support both semantic (by default) and numeric Variant hash comparison
Hash comparison for Variant continues to perform semantic/logical comparison with NaN's considered equal by default (to prevent #16114, #7354, #6947, #8081), but now optionally allows for numeric comparison that does not consider NaN's equal to support proper value comparison (for #72222)
2023-08-31 09:29:48 -07:00
Ninni Pipping
7752a0d8d1 Fix range error for Array.slice 2023-07-07 23:19:42 +02:00
Ninni Pipping
10f385fb79 Fix Array.slice() rounding for abs(step) != 1 2023-03-14 15:46:45 +01:00
Dmitrii Maganov
8400308ab3 GDScript: Fix can_reference check for typed arrays 2023-02-06 22:00:16 +02:00
Rémi Verschelde
bbff9fd7a4
Merge pull request #71786 from raulsntos/dotnet/array
Sync C# Array with Core
2023-02-04 00:24:06 +01:00
Rémi Verschelde
394bb0ee2b
Fix various typos with codespell
Finally do the childs -> children rename too.
2023-02-01 08:45:41 +01:00
Dmitrii Maganov
5909f9f075 GDScript: Fix issues with typed arrays 2023-01-31 11:54:41 +02:00
Raul Santos
54f8fb010c
Sync C# Array with Core
- Add `AddRange` method.
- Add `Fill` method.
- Add `Max` and `Min` methods.
- Add `PickRandom` method.
- Add `Reverse` method.
- Add `RecursiveEqual` method.
- Add `Sort` method.
- Add `Slice` and `GetSliceRange` methods.
- Add `IndexOf` overload that takes an index parameter.
- Add `LastIndexOf` method.
- Add `BinarySearch` method.
- Add/update documentation.
2023-01-30 05:41:53 +01:00
Dmitrii Maganov
abe6d67232 GDScript: Fix test for read-only state of constants 2023-01-27 05:28:08 +02:00
Dmitrii Maganov
5e2ac1a31e GDScript: Begin making constants deep, not shallow or flat 2023-01-08 07:29:10 +02:00
Rémi Verschelde
d95794ec8a
One Copyright Update to rule them all
As many open source projects have started doing it, we're removing the
current year from the copyright notice, so that we don't need to bump
it every year.

It seems like only the first year of publication is technically
relevant for copyright notices, and even that seems to be something
that many companies stopped listing altogether (in a version controlled
codebase, the commits are a much better source of date of publication
than a hardcoded copyright statement).

We also now list Godot Engine contributors first as we're collectively
the current maintainers of the project, and we clarify that the
"exclusive" copyright of the co-founders covers the timespan before
opensourcing (their further contributions are included as part of Godot
Engine contributors).

Also fixed "cf." Frenchism - it's meant as "refer to / see".
2023-01-05 13:25:55 +01:00
rune-scape
e79be6ce07 Unify String and StringName 2022-12-05 21:46:47 -05:00
Micky
67cdac6db8 Remove Array.find_last() 2022-11-25 18:34:50 +01:00
Nonunknown
9f4dbf415d add ability to pick random value from array 2022-10-20 08:39:47 -03:00
Micky
bf64a068a4 Fix Array.slice() and filter() forgetting Typed Array 2022-10-09 12:34:19 +02:00
Juan Linietsky
d4433ae6d3 Remove Signal connect binds
Remove the optional argument p_binds from `Object::connect` since it was deprecated by Callable.bind().
Changed all uses of it to Callable.bind()
2022-07-29 16:26:13 +02:00
reduz
141c375581 Clean up Hash Functions
Clean up and do fixes to hash functions and newly introduced murmur3 hashes in #61934
* Clean up usage of murmur3
* Fixed usages of binary murmur3 on floats (this is invalid)
* Changed DJB2 to use xor (which seems to be better)
2022-06-20 12:54:19 +02:00
Pedro J. Estébanez
fb86430770 Validate every source element separately Array::append_array() 2022-06-15 20:36:20 +02:00
Hugo Locurcio
a98e31aa3a
Add any() and all() methods to Array
These can be used as faster, more convenient shorthands to
using `filter()` + `size()`.
2022-05-25 16:09:10 +02:00
George Marques
023b1d7f5a
Implement read-only arrays
Arrays can be set as read-only and thus cannot be modified. Assigning
the array will create an editable copy.

Similar to is already done to read-only dictionaries.
2022-05-17 14:14:42 -03:00
mashumafi
9c2bfeb2fb Const Ref Callable for custom sort/search 2022-03-27 22:10:36 -04:00