Skip to content

Commit

Permalink
Merge pull request #208 from jss2a98aj/backport-assorted
Browse files Browse the repository at this point in the history
[backport] Assorted fixes and optimizations
  • Loading branch information
Bioblaze authored Jan 4, 2025
2 parents 1180435 + a1bfcdc commit 7eacb15
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 160 deletions.
10 changes: 6 additions & 4 deletions core/string/string_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ String StringBuilder::as_string() const {
int godot_string_elem = 0;
int c_string_elem = 0;

for (int i = 0; i < appended_strings.size(); i++) {
if (appended_strings[i] == -1) {
for (uint32_t i = 0; i < appended_strings.size(); i++) {
const int32_t str_len = appended_strings[i];

if (str_len == -1) {
// Godot string
const String &s = strings[godot_string_elem];

Expand All @@ -81,11 +83,11 @@ String StringBuilder::as_string() const {
} else {
const char *s = c_strings[c_string_elem];

for (int32_t j = 0; j < appended_strings[i]; j++) {
for (int32_t j = 0; j < str_len; j++) {
buffer[current_position + j] = s[j];
}

current_position += appended_strings[i];
current_position += str_len;

c_string_elem++;
}
Expand Down
8 changes: 4 additions & 4 deletions core/string/string_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,17 @@
#define STRING_BUILDER_H

#include "core/string/ustring.h"
#include "core/templates/vector.h"
#include "core/templates/local_vector.h"

class StringBuilder {
uint32_t string_length = 0;

Vector<String> strings;
Vector<const char *> c_strings;
LocalVector<String> strings;
LocalVector<const char *> c_strings;

// -1 means it's a Godot String
// a natural number means C string.
Vector<int32_t> appended_strings;
LocalVector<int32_t> appended_strings;

public:
StringBuilder &append(const String &p_string);
Expand Down
6 changes: 0 additions & 6 deletions core/string/string_name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,6 @@ bool StringName::operator!=(const char *p_name) const {
return !(operator==(p_name));
}

bool StringName::operator!=(const StringName &p_name) const {
// the real magic of all this mess happens here.
// this is why path comparisons are very fast
return _data != p_name._data;
}

char32_t StringName::operator[](int p_index) const {
if (_data) {
if (_data->cname) {
Expand Down
22 changes: 19 additions & 3 deletions core/string/string_name.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,13 @@ class StringName {
return _data >= p_name._data;
}
_FORCE_INLINE_ bool operator==(const StringName &p_name) const {
// the real magic of all this mess happens here.
// this is why path comparisons are very fast
// The real magic of all this mess happens here.
// This is why path comparisons are very fast.
return _data == p_name._data;
}
_FORCE_INLINE_ bool operator!=(const StringName &p_name) const {
return _data != p_name._data;
}
_FORCE_INLINE_ uint32_t hash() const {
if (_data) {
return _data->hash;
Expand All @@ -145,7 +148,6 @@ class StringName {
_FORCE_INLINE_ const void *data_unique_pointer() const {
return (void *)_data;
}
bool operator!=(const StringName &p_name) const;

_FORCE_INLINE_ operator String() const {
if (_data) {
Expand Down Expand Up @@ -185,8 +187,22 @@ class StringName {
};

StringName &operator=(const StringName &p_name);
StringName &operator=(StringName &&p_name) {
if (_data == p_name._data) {
return *this;
}

unref();
_data = p_name._data;
p_name._data = nullptr;
return *this;
}
StringName(const char *p_name, bool p_static = false);
StringName(const StringName &p_name);
StringName(StringName &&p_name) {
_data = p_name._data;
p_name._data = nullptr;
}
StringName(const String &p_name, bool p_static = false);
StringName(const StaticCString &p_static_string, bool p_static = false);
StringName() {}
Expand Down
75 changes: 29 additions & 46 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,21 +319,14 @@ void String::copy_from(const char *p_cstr) {

resize(len + 1); // include 0

const char *end = p_cstr + len;
char32_t *dst = ptrw();

for (size_t i = 0; i <= len; i++) {
#if CHAR_MIN == 0
uint8_t c = p_cstr[i];
#else
uint8_t c = p_cstr[i] >= 0 ? p_cstr[i] : uint8_t(256 + p_cstr[i]);
#endif
if (c == 0 && i < len) {
print_unicode_error("NUL character", true);
dst[i] = _replacement_char;
} else {
dst[i] = c;
}
for (; p_cstr < end; ++p_cstr, ++dst) {
// If char is int8_t, a set sign bit will be reinterpreted as 256 - val implicitly.
*dst = static_cast<uint8_t>(*p_cstr);
}
*dst = 0;
}

void String::copy_from(const char *p_cstr, const int p_clip_to) {
Expand All @@ -356,22 +349,14 @@ void String::copy_from(const char *p_cstr, const int p_clip_to) {

resize(len + 1); // include 0

const char *end = p_cstr + len;
char32_t *dst = ptrw();

for (int i = 0; i < len; i++) {
#if CHAR_MIN == 0
uint8_t c = p_cstr[i];
#else
uint8_t c = p_cstr[i] >= 0 ? p_cstr[i] : uint8_t(256 + p_cstr[i]);
#endif
if (c == 0) {
print_unicode_error("NUL character", true);
dst[i] = _replacement_char;
} else {
dst[i] = c;
}
for (; p_cstr < end; ++p_cstr, ++dst) {
// If char is int8_t, a set sign bit will be reinterpreted as 256 - val implicitly.
*dst = static_cast<uint8_t>(*p_cstr);
}
dst[len] = 0;
*dst = 0;
}

void String::copy_from(const wchar_t *p_cstr) {
Expand Down Expand Up @@ -463,27 +448,25 @@ void String::copy_from(const char32_t *p_cstr, const int p_clip_to) {
// p_length <= p_char strlen
void String::copy_from_unchecked(const char32_t *p_char, const int p_length) {
resize(p_length + 1);

const char32_t *end = p_char + p_length;
char32_t *dst = ptrw();
dst[p_length] = 0;

for (int i = 0; i < p_length; i++) {
if (p_char[i] == 0) {
print_unicode_error("NUL character", true);
dst[i] = _replacement_char;
for (; p_char < end; ++p_char, ++dst) {
const char32_t chr = *p_char;
if ((chr & 0xfffff800) == 0xd800) {
print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)chr));
*dst = _replacement_char;
continue;
}
if ((p_char[i] & 0xfffff800) == 0xd800) {
print_unicode_error(vformat("Unpaired surrogate (%x)", (uint32_t)p_char[i]));
dst[i] = _replacement_char;
continue;
}
if (p_char[i] > 0x10ffff) {
print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)p_char[i]));
dst[i] = _replacement_char;
if (chr > 0x10ffff) {
print_unicode_error(vformat("Invalid unicode codepoint (%x)", (uint32_t)chr));
*dst = _replacement_char;
continue;
}
dst[i] = p_char[i];
*dst = chr;
}
*dst = 0;
}

void String::operator=(const char *p_str) {
Expand Down Expand Up @@ -3969,17 +3952,17 @@ float String::similarity(const String &p_string) const {
return 0.0f;
}

Vector<String> src_bigrams = bigrams();
Vector<String> tgt_bigrams = p_string.bigrams();

int src_size = src_bigrams.size();
int tgt_size = tgt_bigrams.size();
const int src_size = length() - 1;
const int tgt_size = p_string.length() - 1;

int sum = src_size + tgt_size;
const int sum = src_size + tgt_size;
int inter = 0;
for (int i = 0; i < src_size; i++) {
const char32_t i0 = get(i);
const char32_t i1 = get(i + 1);

for (int j = 0; j < tgt_size; j++) {
if (src_bigrams[i] == tgt_bigrams[j]) {
if (i0 == p_string.get(j) && i1 == p_string.get(j + 1)) {
inter++;
break;
}
Expand Down
13 changes: 13 additions & 0 deletions core/templates/list.h
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,15 @@ class List {
it = it->next();
}
}
void operator=(List &&p_list) {
if (unlikely(this == &p_list)) {
return;
}

clear();
_data = p_list._data;
p_list._data = nullptr;
}

// Random access to elements, use with care,
// do not use for iteration.
Expand Down Expand Up @@ -760,6 +769,10 @@ class List {
it = it->next();
}
}
List(List &&p_list) {
_data = p_list._data;
p_list._data = nullptr;
}

List() {}

Expand Down
30 changes: 30 additions & 0 deletions core/templates/local_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,16 @@ class LocalVector {
data[i] = p_from.data[i];
}
}
_FORCE_INLINE_ LocalVector(LocalVector &&p_from) {
data = p_from.data;
count = p_from.count;
capacity = p_from.capacity;

p_from.data = nullptr;
p_from.count = 0;
p_from.capacity = 0;
}

inline void operator=(const LocalVector &p_from) {
resize(p_from.size());
for (U i = 0; i < p_from.count; i++) {
Expand All @@ -334,6 +344,26 @@ class LocalVector {
data[i] = p_from[i];
}
}
inline void operator=(LocalVector &&p_from) {
if (unlikely(this == &p_from)) {
return;
}
reset();

data = p_from.data;
count = p_from.count;
capacity = p_from.capacity;

p_from.data = nullptr;
p_from.count = 0;
p_from.capacity = 0;
}
inline void operator=(Vector<T> &&p_from) {
resize(p_from.size());
for (U i = 0; i < count; i++) {
data[i] = std::move(p_from[i]);
}
}

_FORCE_INLINE_ ~LocalVector() {
if (data) {
Expand Down
9 changes: 2 additions & 7 deletions core/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
// Should be available everywhere.
#include "core/error/error_list.h"
#include <cstdint>
#include <utility>

// Turn argument to string constant:
// https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html#Stringizing
Expand Down Expand Up @@ -126,13 +127,7 @@ constexpr auto CLAMP(const T m_a, const T2 m_min, const T3 m_max) {

// Generic swap template.
#ifndef SWAP
#define SWAP(m_x, m_y) __swap_tmpl((m_x), (m_y))
template <typename T>
inline void __swap_tmpl(T &x, T &y) {
T aux = x;
x = y;
y = aux;
}
#define SWAP(m_x, m_y) std::swap((m_x), (m_y))
#endif // SWAP

/* Functions to handle powers of 2 and shifting. */
Expand Down
4 changes: 1 addition & 3 deletions drivers/d3d12/rendering_device_driver_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2225,6 +2225,7 @@ RDD::FenceID RenderingDeviceDriverD3D12::fence_create() {

Error RenderingDeviceDriverD3D12::fence_wait(FenceID p_fence) {
FenceInfo *fence = (FenceInfo *)(p_fence.id);
fence->d3d_fence->SetEventOnCompletion(fence->fence_value, fence->event_handle);
DWORD res = WaitForSingleObjectEx(fence->event_handle, INFINITE, FALSE);
#ifdef PIX_ENABLED
PIXNotifyWakeFromFenceSignal(fence->event_handle);
Expand Down Expand Up @@ -2319,7 +2320,6 @@ Error RenderingDeviceDriverD3D12::command_queue_execute_and_present(CommandQueue
FenceInfo *fence = (FenceInfo *)(p_cmd_fence.id);
fence->fence_value++;
command_queue->d3d_queue->Signal(fence->d3d_fence.Get(), fence->fence_value);
fence->d3d_fence->SetEventOnCompletion(fence->fence_value, fence->event_handle);
}
}

Expand Down Expand Up @@ -2526,8 +2526,6 @@ Error RenderingDeviceDriverD3D12::swap_chain_resize(CommandQueueID p_cmd_queue,
break;
}

print_verbose("Using swap chain flags: " + itos(creation_flags) + ", sync interval: " + itos(sync_interval) + ", present flags: " + itos(present_flags));

if (swap_chain->d3d_swap_chain != nullptr && creation_flags != swap_chain->creation_flags) {
// The swap chain must be recreated if the creation flags are different.
_swap_chain_release(swap_chain);
Expand Down
Loading

0 comments on commit 7eacb15

Please sign in to comment.