Skip to content

Commit 1bb3e3c

Browse files
committed
Some perf-analyzis-driven improvements.
- Added a safety variant for hot path operations in the bump allocator and the ordering table. - Unrolled the insert operation for the ordering table.
1 parent d989f49 commit 1bb3e3c

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

src/mips/psyqo/bump-allocator.h renamed to src/mips/psyqo/bump-allocator.hh

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ SOFTWARE.
3232
#include "psyqo/fragments.hh"
3333
#include "psyqo/kernel.hh"
3434
#include "psyqo/primitive-concept.hh"
35+
#include "psyqo/shared.hh"
3536

3637
namespace psyqo {
3738

@@ -51,13 +52,15 @@ namespace psyqo {
5152
*
5253
* @tparam N The size of the memory buffer in bytes.
5354
*/
54-
template <size_t N>
55+
template <size_t N, Safe safety = Safe::Yes>
5556
class BumpAllocator {
5657
public:
5758
template <Primitive P, typename... Args>
5859
Fragments::SimpleFragment<P> &allocateFragment(Args &&...args) {
5960
static constexpr size_t size = sizeof(Fragments::SimpleFragment<P>);
60-
psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
61+
if constexpr (safety == Safe::Yes) {
62+
psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
63+
}
6164
uint8_t *ptr = m_current;
6265
m_current += size;
6366
return *new (ptr) Fragments::SimpleFragment<P>(eastl::forward<Args>(args)...);
@@ -72,7 +75,9 @@ class BumpAllocator {
7275
size += alignedptr - ptr;
7376
ptr = alignedptr;
7477
}
75-
psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
78+
if constexpr (safety == Safe::Yes) {
79+
psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
80+
}
7681
m_current += size;
7782
return *new (ptr) T(eastl::forward<Args>(args)...);
7883
}

src/mips/psyqo/ordering-table.hh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ SOFTWARE.
2626

2727
#pragma once
2828

29+
#include <EASTL/algorithm.h>
2930
#include <stdint.h>
3031

3132
#include "psyqo/fragment-concept.hh"
33+
#include "psyqo/shared.hh"
3234

3335
namespace psyqo {
3436

@@ -37,7 +39,6 @@ class GPU;
3739
class OrderingTableBase {
3840
protected:
3941
static void clear(uint32_t* table, size_t size);
40-
static void insert(uint32_t* table, int32_t size, uint32_t* head, uint32_t shiftedFragmentSize, int32_t z);
4142
};
4243

4344
/**
@@ -54,7 +55,7 @@ class OrderingTableBase {
5455
* @tparam N The number of buckets in the ordering table. The larger the number,
5556
* the more precise the sorting will be, but the more memory will be used.
5657
*/
57-
template <size_t N = 4096>
58+
template <size_t N = 4096, Safe safety = Safe::Yes>
5859
class OrderingTable : private OrderingTableBase {
5960
public:
6061
OrderingTable() { clear(); }
@@ -74,15 +75,22 @@ class OrderingTable : private OrderingTableBase {
7475
*
7576
* @details This function inserts a fragment into the ordering table. The fragment
7677
* will be inserted into the bucket corresponding to its Z value. Any value outside
77-
* of the range [0, N - 1] will be clamped to the nearest valid value.
78+
* of the range [0, N - 1] will be clamped to the nearest valid value when `safety`
79+
* is set to `Safe::Yes`, which is the default.
7880
*
7981
* @param frag The fragment to insert.
8082
* @param z The Z value of the fragment.
8183
*/
8284
template <Fragment Frag>
8385
void insert(Frag& frag, int32_t z) {
8486
// TODO: cater for big packets
85-
OrderingTableBase::insert(m_table, N, &frag.head, uint32_t(frag.getActualFragmentSize() << 24), z);
87+
uint32_t* table = m_table + 1;
88+
uint32_t* head = &frag.head;
89+
if constexpr (safety == Safe::Yes) {
90+
z = eastl::clamp(z, int32_t(0), int32_t(N - 1));
91+
}
92+
*head = (frag.getActualFragmentSize() << 24) | table[z];
93+
table[z] = reinterpret_cast<uint32_t>(head) & 0xffffff;
8694
}
8795

8896
private:

src/mips/psyqo/shared.hh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
MIT License
4+
5+
Copyright (c) 2025 PCSX-Redux authors
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.
24+
25+
*/
26+
27+
#pragma once
28+
29+
namespace psyqo {
30+
31+
/**
32+
* @brief Shared type for safe and unsafe operations throughout the codebase.
33+
*/
34+
enum class Safe : unsigned { No, Yes };
35+
36+
}

src/mips/psyqo/src/ordering-table.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,9 @@ SOFTWARE.
2626

2727
#include "psyqo/ordering-table.hh"
2828

29-
#include <EASTL/algorithm.h>
30-
3129
void psyqo::OrderingTableBase::clear(uint32_t* table, size_t size) {
3230
table[0] = 0xffffff;
3331
for (size_t i = 1; i <= size; i++) {
3432
table[i] = reinterpret_cast<uint32_t>(&table[i - 1]) & 0xffffff;
3533
}
3634
}
37-
38-
void psyqo::OrderingTableBase::insert(uint32_t* table, int32_t size, uint32_t* head, uint32_t shiftedFragmentSize,
39-
int32_t z) {
40-
z = eastl::clamp(z, int32_t(0), size) + 1;
41-
*head = shiftedFragmentSize | table[z];
42-
table[z] = reinterpret_cast<uint32_t>(head) & 0xffffff;
43-
}

0 commit comments

Comments
 (0)