Skip to content

Commit

Permalink
Merge pull request #1849 from nicolasnoble/safety-and-unroll
Browse files Browse the repository at this point in the history
Some perf-analyzis-driven improvements.
  • Loading branch information
nicolasnoble authored Jan 20, 2025
2 parents d989f49 + 1bb3e3c commit 9e37585
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ SOFTWARE.
#include "psyqo/fragments.hh"
#include "psyqo/kernel.hh"
#include "psyqo/primitive-concept.hh"
#include "psyqo/shared.hh"

namespace psyqo {

Expand All @@ -51,13 +52,15 @@ namespace psyqo {
*
* @tparam N The size of the memory buffer in bytes.
*/
template <size_t N>
template <size_t N, Safe safety = Safe::Yes>
class BumpAllocator {
public:
template <Primitive P, typename... Args>
Fragments::SimpleFragment<P> &allocateFragment(Args &&...args) {
static constexpr size_t size = sizeof(Fragments::SimpleFragment<P>);
psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
if constexpr (safety == Safe::Yes) {
psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
}
uint8_t *ptr = m_current;
m_current += size;
return *new (ptr) Fragments::SimpleFragment<P>(eastl::forward<Args>(args)...);
Expand All @@ -72,7 +75,9 @@ class BumpAllocator {
size += alignedptr - ptr;
ptr = alignedptr;
}
psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
if constexpr (safety == Safe::Yes) {
psyqo::Kernel::assert(remaining() >= size, "BumpAllocator: Out of memory");
}
m_current += size;
return *new (ptr) T(eastl::forward<Args>(args)...);
}
Expand Down
16 changes: 12 additions & 4 deletions src/mips/psyqo/ordering-table.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ SOFTWARE.

#pragma once

#include <EASTL/algorithm.h>
#include <stdint.h>

#include "psyqo/fragment-concept.hh"
#include "psyqo/shared.hh"

namespace psyqo {

Expand All @@ -37,7 +39,6 @@ class GPU;
class OrderingTableBase {
protected:
static void clear(uint32_t* table, size_t size);
static void insert(uint32_t* table, int32_t size, uint32_t* head, uint32_t shiftedFragmentSize, int32_t z);
};

/**
Expand All @@ -54,7 +55,7 @@ class OrderingTableBase {
* @tparam N The number of buckets in the ordering table. The larger the number,
* the more precise the sorting will be, but the more memory will be used.
*/
template <size_t N = 4096>
template <size_t N = 4096, Safe safety = Safe::Yes>
class OrderingTable : private OrderingTableBase {
public:
OrderingTable() { clear(); }
Expand All @@ -74,15 +75,22 @@ class OrderingTable : private OrderingTableBase {
*
* @details This function inserts a fragment into the ordering table. The fragment
* will be inserted into the bucket corresponding to its Z value. Any value outside
* of the range [0, N - 1] will be clamped to the nearest valid value.
* of the range [0, N - 1] will be clamped to the nearest valid value when `safety`
* is set to `Safe::Yes`, which is the default.
*
* @param frag The fragment to insert.
* @param z The Z value of the fragment.
*/
template <Fragment Frag>
void insert(Frag& frag, int32_t z) {
// TODO: cater for big packets
OrderingTableBase::insert(m_table, N, &frag.head, uint32_t(frag.getActualFragmentSize() << 24), z);
uint32_t* table = m_table + 1;
uint32_t* head = &frag.head;
if constexpr (safety == Safe::Yes) {
z = eastl::clamp(z, int32_t(0), int32_t(N - 1));
}
*head = (frag.getActualFragmentSize() << 24) | table[z];
table[z] = reinterpret_cast<uint32_t>(head) & 0xffffff;
}

private:
Expand Down
36 changes: 36 additions & 0 deletions src/mips/psyqo/shared.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
MIT License
Copyright (c) 2025 PCSX-Redux authors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

#pragma once

namespace psyqo {

/**
* @brief Shared type for safe and unsafe operations throughout the codebase.
*/
enum class Safe : unsigned { No, Yes };

}
9 changes: 0 additions & 9 deletions src/mips/psyqo/src/ordering-table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,9 @@ SOFTWARE.

#include "psyqo/ordering-table.hh"

#include <EASTL/algorithm.h>

void psyqo::OrderingTableBase::clear(uint32_t* table, size_t size) {
table[0] = 0xffffff;
for (size_t i = 1; i <= size; i++) {
table[i] = reinterpret_cast<uint32_t>(&table[i - 1]) & 0xffffff;
}
}

void psyqo::OrderingTableBase::insert(uint32_t* table, int32_t size, uint32_t* head, uint32_t shiftedFragmentSize,
int32_t z) {
z = eastl::clamp(z, int32_t(0), size) + 1;
*head = shiftedFragmentSize | table[z];
table[z] = reinterpret_cast<uint32_t>(head) & 0xffffff;
}

0 comments on commit 9e37585

Please sign in to comment.