246 Commits

Author SHA1 Message Date
A Thousand Ships
dbaf8f3e7a
[Core] Fix performance issues in some CowData methods
These relied on `get` which does a bounds check each time on debug
builds
2025-03-15 19:20:48 +01:00
Pāvels Nadtočajevs
bdb5d522d1 Use atomic flag to prevent flush_if_pending from reading unlocked command_mem. 2025-02-25 23:26:43 +02:00
Matias N. Goldberg
7b2f1e1d09 Fix union order to simplify empty initializers
This is a followup to PR #101344 (commit
0e06eb80bc9cf08000bf86d06c0ce57bcf7775be).

Some of them were not an issue because Godot was initializing all
members, but they were "fixed" just in case since it could become a
problem in the future.

Valgrind was specifically complaining about HashMapData &
GlobalPipelineData.
2025-01-14 19:05:01 -03:00
Lukas Tenbrink
cf145de21e Revert regression (GH-31736) of memory unsafe append_array (append vector to itself). Add comments to prevent future regressions. 2025-01-10 15:30:36 +01:00
Aarni Koskela
f134769506 Fix various typos
* Add TODO notes for typos that should be fixed for 5.0

Co-authored-by: Rémi Verschelde <rverschelde@gmail.com>
2025-01-08 14:47:42 +02:00
Rémi Verschelde
1aaf20b1f1
Merge pull request #98488 from RandomShaper/lockless_rid_fetch_pro
Fix `RID_Owner` synchronization
2025-01-06 08:38:14 +01:00
Pedro J. Estébanez
de7e4efef8 Fix RID_Owner synchronization 2025-01-03 17:05:36 +01:00
Rémi Verschelde
21e6671740
Merge pull request #100937 from Repiteo/style/clang-format-sync
Style: Enforce `AllowShortFunctionsOnASingleLine`
2025-01-03 00:49:44 +01:00
Rémi Verschelde
dd7d36e803
Merge pull request #100770 from hpvb/command-queue-mt
Core: Refactor CommandQueueMT to use vararg templates for performance and maintainability
2025-01-03 00:49:02 +01:00
Thaddeus Crews
e06d83860d
Style: Enforce AllowShortFunctionsOnASingleLine 2025-01-02 10:09:41 -06:00
HP van Braam
cccd2432c3 Refactor CommandQueueMT to use vararg templates
In order to make CommandQueueMT more maintainable this PR changes the
previous macro hell with variadic templates instead. This makes the
class far more explicit and will allow us to more easily change the way
the class functions in the future.

Furthermore this refactoring has allowed for some optimizations. In
particular by using std::forward to delay the decision of decaying the
type to as late as possible we are able to move the data from the
callsite into our Command buffer and later move it to the call.

In practice what this means is that compared to the old version instead
of copying values 3 times, we can now get away with 1 copy, and 1 move
for lvalues, and just 2 moves for rvalues. This saves quite a few
operations in a hot codepath.

We also now test to make sure that the amount of copies and moves are
what we expect. This way we can spot performance regressions in this
code easily.

Somewhat unscientifically, running TPS-demo by pressing enter and not
touching the controls average mspf, repeatable across many runs:

before: 6.467
after : 6.202
2025-01-02 15:35:08 +01:00
Thaddeus Crews
08d4dd7fd8
Merge pull request #100694 from Ivorforce/cowdata-destruct-graciously
Destruct `CowData` more graciously by avoiding accidentally exposing a half-destructed buffer.
2024-12-23 11:15:18 -06:00
Lukas Tenbrink
25cd923ea1 Destruct CowData more graciously by avoiding accidentally exposing a half-destructed buffer. This can avoid problems if any of the destructed objects tries to access the data while it's being destructed. 2024-12-21 20:01:03 +01:00
Lukas Tenbrink
0e32f3b957 Make LocalVector -> Vector automatic conversion safe for non-trivial types. 2024-12-21 10:28:57 +01:00
Thaddeus Crews
151e7fc687
Merge pull request #100477 from Ivorforce/cowdata-move-insert-n-remove
Optimize `CowData` and `LocalVector` functions `.insert` and `.remove_at` by using move semantics.
2024-12-19 20:00:20 -06:00
Thaddeus Crews
1b8a2d930f
Merge pull request #100474 from Ivorforce/cowdata-abstract-realloc
Abstract `CowData`'s reallocations into `_realloc` to consolidate duplicate logic.
2024-12-19 20:00:19 -06:00
Thaddeus Crews
bf9ef5f8a5
Merge pull request #100564 from YYF233333/iwyu
Remove unused headers in core
2024-12-19 19:59:55 -06:00
Thaddeus Crews
92615f24e7
Merge pull request #100563 from Ivorforce/move-semantics-list
Add move semantics (constructor, operator=) to `List`.
2024-12-19 19:59:54 -06:00
Thaddeus Crews
0b01f3cc14
Merge pull request #100560 from Ivorforce/localvector-move-semantics
Add `LocalVector` move semantics (constructor and operator=).
2024-12-19 19:59:51 -06:00
Lukas Tenbrink
8483d794ff Abstract CowData's reallocations into _realloc to consolidate duplicate logic. 2024-12-19 17:27:16 +01:00
Lukas Tenbrink
3564e7c231 Add LocalVector move semantics (constructor and operator=). 2024-12-19 15:05:05 +01:00
Lukas Tenbrink
19992e3284 Add move semantics (constructor, operator=) to List. 2024-12-19 15:04:15 +01:00
Thaddeus Crews
361e3b4fe8
Core: Expand std::initializer_list support 2024-12-18 18:46:59 -06:00
Yufeng Ying
be86ce3103 Apply iwyu suggestion in core. 2024-12-19 00:43:47 +08:00
HP van Braam
240f510fa7 Core ubsan fixes
This fixes UBSAN errors reported by running our testsuite, importing the
TPS demo, and running the TPS demo. I have tried, wherever possible, to
fix issues related to reported issues but not directly reported by UBSAN
because thse code paths just happened to not have been exercised in
these cases.

These fixes apply only to errors reported, and caused by, core/

The following things have been changed:

* Make sure there are no implicit sign changing casts in core.
* Explicitly type enums that are part of a public API such that users of
  the API cannot pass in wrongly-sized values leading to potential stack
  corruption.
* Ensure that memcpy is never called with invalid or null pointers as
  this is undefined behavior, and when the engine is built with
  optimizations turned on leads to memory corruption and hard to debug
  crashes.
* Replace enum values only used as static values with constexpr static
  const values instead. This has no runtime overhead. This makes it so
  that the size of the enums is explicit.
* Make sure that nan and inf is handled consistently in String.
* Implement a _to_int template to ensure that all of the paths use the
  same algorhithm, and correct the negative integer case.
* Changed the way the json serializer precision work, and added tests to
  verify the new behavior. The behavior doesn't quite match master in
  particulary for negative doubles as the original code tried to cast -inf
  to an int. This then led to negative doubles losing all but one of
  their decimal points when serializing. Behavior in GDScript remains
  unchanged.
2024-12-18 14:31:12 +01:00
Lukas Tenbrink
a636c04244 Optimize CowData and LocalVector functions .insert and .remove_at by using move semantics. 2024-12-17 14:10:10 +01:00
Lukas Tenbrink
57073ba14e Add move constructor and move assignment to CowData, String, Char16String, CharString and Vector. 2024-12-11 15:52:15 +01:00
Sai Nane
66b7d5f1b5 Remove apparent contradiction in vector.h header
3205a92ad8 was a major commit which removed `PoolVector`, and replaced
most references to `PoolVector` with `Vector` instead. In most cases,
this was appropriate, given that `PoolVector` was being replaced with
`Vector`, as an effective generalist in 64-bit address space layouts.

However, vector.h itself was left with an artifact advising the reader
to use `Vector` instead of `Vector` for large arrays. While this led
to a fascinating deep dive, and hopefully improved some of the
documentation along the way, it's probably best to clean this up for
the next person.
2024-12-10 01:51:30 +00:00
Thaddeus Crews
370c692bcb
Merge pull request #98706 from Nazarwadim/fix_capture_cache_was_not_cached
Fix `capture_cache.animation` was not cached
2024-11-13 08:33:54 -06:00
Juan
fe34c45d2a
Allow passing UID to importer
This helps, for importers spitting out new resources to the res://
filesystem to actually hash them to generate deterministic UIDs.

This PR also fixes the determinism for translations.
2024-11-11 15:22:42 +01:00
rune-scape
a47daa0a44 Avoid const_cast in List::erase by requiring mutable elements 2024-11-08 00:10:08 -08:00
Nazarii
d1dc7afdd5 Fix capture_cache.animation was not cached 2024-10-31 19:26:02 +02:00
Thaddeus Crews
a8931f1aa9
Merge pull request #98652 from stuartcarnie/2d_texture_state_fixes
2D: Fix various issues and minor performance optimisations
2024-10-29 19:25:54 -05:00
Thaddeus Crews
b7a0971ad2
Merge pull request #97934 from adamscott/give-AThousandShips-a-break
[Codestyle] Set clang-format `RemoveSemicolon` rule to `true`
2024-10-29 19:25:36 -05:00
Stuart Carnie
0d1d945727
2D: Fix various issues and minor performance optimisations 2024-10-30 08:36:45 +11:00
Adam Scott
0d350e7108
Set clang-format RemoveSemicolon rule to true
- Set clang-format `Standard` rule to `c++20`
2024-10-25 13:49:43 -04:00
nazarii
76208f7155 Implement array based hash map 2024-10-24 21:34:12 +03:00
Rémi Verschelde
8e6ade8708
Merge pull request #97465 from DarioSamo/lock-free-rid
Make `RID_Owner` lock-free for fetching.
2024-10-04 11:21:38 +02:00
Aaron Franke
a7f6ec0e4c
Add a sort method to Dictionary and HashMap 2024-10-02 14:26:33 -07:00
A Thousand Ships
75cb3539b1
[Core] Add missing HashMapComparatorDefault cases
Added for:
* `AABB`
* `Basis`
* `Color`
* `Plane`
* `Projection`
* `Quaternion`
* `Rect2`
* `Transform2D`
* `Transform3D`
* `Vector4`

Ensures these handles `NaN` correctly
2024-09-28 17:58:49 +02:00
Juan Linietsky
08947d366f Make RID_Owner lock-free for fetching.
This PR makes RID_Owner lock free for fetching values, this should give a very
significant peformance boost where used.

Some considerations:

* A maximum number of elements to alocate must be given (by default 256k).
* Access to the RID structure is still safe given they are independent from addition/removals.
* RID access was never really thread-safe in the sense that the contents of the data are not protected anyway. Each server needs to implement locking as it sees fit.
2024-09-25 15:05:24 -03:00
Thaddeus Crews
9f9ee0c813
SCons: Add unobtrusive type hints in SCons files 2024-09-25 09:34:35 -05:00
Rémi Verschelde
8019cdb444
Merge pull request #94137 from Craig-Stoneham/master
Improve template class conditionals with constexpr (code style)
2024-08-26 22:45:17 +02:00
Craig-Stoneham
604df4fc83 Improve Template Class Conditionals (Code Style)
Refactored the following template classes by replacing runtime checks with compile-time checks using if constexpr for improved code clarity and maintainability:

- RID_Alloc
- SortArray
- PagedAllocator

Changes made:

- Updated conditional checks for THREAD_SAFE in the RID_Alloc class.
- Updated conditional checks for Validate in the SortArray class.
- Updated conditional checks for thread_safe in the PagedAllocator class.
2024-08-25 18:10:51 +01:00
Pedro J. Estébanez
f4d76853b9 WorkerThreadPool (plus friends): Overhaul unlock allowance zones
This fixes a rare but possible deadlock, maybe due to undefined behavior. The new implementation is safer, at the cost of some added boilerplate.
2024-08-21 12:22:52 +02:00
Rémi Verschelde
f1d6677713
Merge pull request #95469 from bruvzg/cowdata_unref
`CowData` remove hardcoded offset and unused argument from `_unref`.
2024-08-16 10:36:28 +02:00
Rémi Verschelde
91bf992168
Merge pull request #94353 from aaronp64/cowdata_insert
Improve `CowData::insert` performance
2024-08-16 10:34:22 +02:00
bruvzg
273ba27c2f
CowData remove hardcoded offset and unused argument from _unref. 2024-08-13 08:53:45 +03:00
Pedro J. Estébanez
5b5cdf2414 Fixup recent changes to threading concerns
ResourceLoader:
- Fix invalid tokens being returned.
- Remove no longer written `ThreadLoadTask::dependent_path` and the code reading from it.
- Clear deadlock hazard by keeping the mutex unlocked during userland polling.

WorkerThreadPool:
- Include thread call queue override in the thread state reset set, which allows to simplify the code that handled that (imperfectly) in the ResourceLoader.
- Handle the mutex type correctly on entering an allowance zone.

CommandQueueMT:
- Handle the additional possibility of command buffer reallocation that mutex unlock allowance introduces.
2024-07-16 11:03:02 +02:00
aaronp64
c1afe7dcdf Improve CowData::insert performance
Update CowData::insert to call ptrw() before loop, to avoid calling _copy_on_write for each item in the array, as well as repeated index checks in set and get.  For larger Vectors/Arrays, this makes inserts around 10x faster for ints, 3x faster for Strings, and 2x faster for Variants.  Less of an impact on smaller Vectors/Arrays, as a larger percentage of the time is spent allocating.
2024-07-15 14:01:19 -04:00