Skip to content

Commit

Permalink
Revert "Improve code layout in runtime (#1112)" (#1147)
Browse files Browse the repository at this point in the history
This reverts commit 89b0aa6.

Signed-off-by: Petr Shumilov <p.shumilov@vkteam.ru>
  • Loading branch information
PetrShumilov authored Nov 13, 2024
1 parent d4c545d commit 651710a
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 110 deletions.
10 changes: 5 additions & 5 deletions runtime-common/core/core-types/decl/mixed_decl.inl
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ public:
void convert_to_float();
void convert_to_string();

inline const bool &as_bool(const char *function) const;
inline const int64_t &as_int(const char *function) const;
inline const double &as_float(const char *function) const;
inline const string &as_string(const char *function) const;
inline const array<mixed> &as_array(const char *function) const;
const bool &as_bool(const char *function) const;
const int64_t &as_int(const char *function) const;
const double &as_float(const char *function) const;
const string &as_string(const char *function) const;
const array<mixed> &as_array(const char *function) const;

bool &as_bool(const char *function);
int64_t &as_int(const char *function);
Expand Down
4 changes: 2 additions & 2 deletions runtime-common/core/core-types/decl/string_decl.inl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private:
inline string_inner *inner() const;

inline bool disjunct(const char *s) const;
void set_size(size_type new_size);
inline void set_size(size_type new_size);

inline static char *create(const char *beg, const char *end);
// IMPORTANT: this function may return read-only strings for n == 0 and n == 1.
Expand Down Expand Up @@ -105,7 +105,7 @@ public:
inline string(size_type n, bool b);
inline explicit string(int64_t i);
inline explicit string(int32_t i): string(static_cast<int64_t>(i)) {}
explicit string(double f);
inline explicit string(double f);

~string() noexcept;

Expand Down
66 changes: 55 additions & 11 deletions runtime-common/core/core-types/definition/mixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ mixed &mixed::operator/=(const mixed &other) {

mixed &mixed::operator%=(const mixed &other) {
int64_t div = other.to_int();
if (unlikely(div == 0)) {
if (div == 0) {
php_warning("Modulo by zero");
*this = false;
return *this;
Expand Down Expand Up @@ -278,10 +278,6 @@ mixed &mixed::operator>>=(const mixed &other) {


mixed &mixed::operator++() {
if (likely (get_type() == type::INTEGER)) {
++as_int();
return *this;
}
switch (get_type()) {
case type::NUL:
type_ = type::INTEGER;
Expand All @@ -290,6 +286,9 @@ mixed &mixed::operator++() {
case type::BOOLEAN:
php_warning("Can't apply operator ++ to boolean");
return *this;
case type::INTEGER:
++as_int();
return *this;
case type::FLOAT:
as_double() += 1;
return *this;
Expand All @@ -308,11 +307,6 @@ mixed &mixed::operator++() {
}

const mixed mixed::operator++(int) {
if (likely(get_type() == type::INTEGER)) {
mixed res(as_int());
++as_int();
return res;
}
switch (get_type()) {
case type::NUL:
type_ = type::INTEGER;
Expand Down Expand Up @@ -840,6 +834,56 @@ void mixed::convert_to_string() {
}
}

const bool &mixed::as_bool(const char *function) const {
switch (get_type()) {
case type::BOOLEAN:
return as_bool();
default:
php_warning("%s() expects parameter to be boolean, %s is given", function, get_type_or_class_name());
return empty_value<bool>();
}
}

const int64_t &mixed::as_int(const char *function) const {
switch (get_type()) {
case type::INTEGER:
return as_int();
default:
php_warning("%s() expects parameter to be int, %s is given", function, get_type_or_class_name());
return empty_value<int64_t>();
}
}

const double &mixed::as_float(const char *function) const {
switch (get_type()) {
case type::FLOAT:
return as_double();
default:
php_warning("%s() expects parameter to be float, %s is given", function, get_type_or_class_name());
return empty_value<double>();
}
}

const string &mixed::as_string(const char *function) const {
switch (get_type()) {
case type::STRING:
return as_string();
default:
php_warning("%s() expects parameter to be string, %s is given", function, get_type_or_class_name());
return empty_value<string>();
}
}

const array<mixed> &mixed::as_array(const char *function) const {
switch (get_type()) {
case type::ARRAY:
return as_array();
default:
php_warning("%s() expects parameter to be array, %s is given", function, get_type_or_class_name());
return empty_value<array<mixed>>();
}
}


bool &mixed::as_bool(const char *function) {
switch (get_type()) {
Expand Down Expand Up @@ -1091,7 +1135,7 @@ mixed &mixed::operator[](const string &string_key) {
}

mixed &mixed::operator[](tmp_string string_key) {
if (likely(get_type() == type::ARRAY)) {
if (get_type() == type::ARRAY) {
return as_array()[string_key];
}
return (*this)[materialize_tmp_string(string_key)];
Expand Down
41 changes: 0 additions & 41 deletions runtime-common/core/core-types/definition/mixed.inl
Original file line number Diff line number Diff line change
Expand Up @@ -152,47 +152,6 @@ inline bool mixed::is_object() const {
return get_type() == type::OBJECT;
}


inline const bool &mixed::as_bool(const char *function) const {
if (likely(get_type() == type::BOOLEAN)) {
return as_bool();
}
php_warning("%s() expects parameter to be boolean, %s is given", function, get_type_or_class_name());
return empty_value<bool>();
}

inline const int64_t &mixed::as_int(const char *function) const {
if (likely(get_type() == type::INTEGER)) {
return as_int();
}
php_warning("%s() expects parameter to be int, %s is given", function, get_type_or_class_name());
return empty_value<int64_t>();
}

inline const double &mixed::as_float(const char *function) const {
if (likely(get_type() == type::FLOAT)) {
return as_double();
}
php_warning("%s() expects parameter to be float, %s is given", function, get_type_or_class_name());
return empty_value<double>();
}

inline const string &mixed::as_string(const char *function) const {
if (likely(get_type() == type::STRING)) {
return as_string();
}
php_warning("%s() expects parameter to be string, %s is given", function, get_type_or_class_name());
return empty_value<string>();
}

inline const array<mixed> &mixed::as_array(const char *function) const {
if (likely(get_type() == type::ARRAY)) {
return as_array();
}
php_warning("%s() expects parameter to be array, %s is given", function, get_type_or_class_name());
return empty_value<array<mixed>>();
}

inline void mixed::swap(mixed &other) {
::swap(type_, other.type_);
::swap(as_double(), other.as_double());
Expand Down
51 changes: 0 additions & 51 deletions runtime-common/core/core-types/definition/string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,4 @@ string::~string() noexcept {
destroy();
}

string::string(double f) {
constexpr uint32_t MAX_LEN = 4096;
char result[MAX_LEN + 2];
result[0] = '\0';
result[1] = '\0';

char *begin = result + 2;
if (std::isnan(f)) {
// to prevent printing `-NAN` by snprintf
f = std::abs(f);
}
int len = snprintf(begin, MAX_LEN, "%.14G", f);
if (static_cast<uint32_t>(len) < MAX_LEN) {
if (static_cast<uint32_t>(begin[len - 1] - '5') < 5 && begin[len - 2] == '0' && begin[len - 3] == '-') {
--len;
begin[len - 1] = begin[len];
}
if (begin[1] == 'E') {
result[0] = begin[0];
result[1] = '.';
result[2] = '0';
begin = result;
len += 2;
} else if (begin[0] == '-' && begin[2] == 'E') {
result[0] = begin[0];
result[1] = begin[1];
result[2] = '.';
result[3] = '0';
begin = result;
len += 2;
}
php_assert (len <= STRLEN_FLOAT);
p = create(begin, begin + len);
} else {
php_warning("Maximum length of float (%d) exceeded", MAX_LEN);
p = string_cache::empty_string().ref_data();
}
}

void string::set_size(size_type new_size) {
if (inner()->is_shared()) {
string_inner *r = string_inner::create(new_size, capacity());

inner()->dispose();
p = r->ref_data();
} else if (new_size > capacity()) {
p = inner()->reserve(new_size);
}
inner()->set_length_and_sharable(new_size);
}

static_assert(sizeof(string) == SIZEOF_STRING, "sizeof(string) at runtime doesn't match compile-time");
49 changes: 49 additions & 0 deletions runtime-common/core/core-types/definition/string.inl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,17 @@ bool string::disjunct(const char *s) const {
return (s < p || p + size() < s);
}

void string::set_size(size_type new_size) {
if (inner()->is_shared()) {
string_inner *r = string_inner::create(new_size, capacity());

inner()->dispose();
p = r->ref_data();
} else if (new_size > capacity()) {
p = inner()->reserve(new_size);
}
inner()->set_length_and_sharable(new_size);
}

char *string::create(const char *beg, const char *end) {
const size_type dnew = static_cast<size_type>(end - beg);
Expand Down Expand Up @@ -221,6 +231,45 @@ string::string(int64_t i) {
p[inner()->size] = '\0';
}

string::string(double f) {
constexpr uint32_t MAX_LEN = 4096;
char result[MAX_LEN + 2];
result[0] = '\0';
result[1] = '\0';

char *begin = result + 2;
if (std::isnan(f)) {
// to prevent printing `-NAN` by snprintf
f = std::abs(f);
}
int len = snprintf(begin, MAX_LEN, "%.14G", f);
if (static_cast<uint32_t>(len) < MAX_LEN) {
if (static_cast<uint32_t>(begin[len - 1] - '5') < 5 && begin[len - 2] == '0' && begin[len - 3] == '-') {
--len;
begin[len - 1] = begin[len];
}
if (begin[1] == 'E') {
result[0] = begin[0];
result[1] = '.';
result[2] = '0';
begin = result;
len += 2;
} else if (begin[0] == '-' && begin[2] == 'E') {
result[0] = begin[0];
result[1] = begin[1];
result[2] = '.';
result[3] = '0';
begin = result;
len += 2;
}
php_assert (len <= STRLEN_FLOAT);
p = create(begin, begin + len);
} else {
php_warning("Maximum length of float (%d) exceeded", MAX_LEN);
p = string_cache::empty_string().ref_data();
}
}

string::string(ArrayBucketDummyStrTag) noexcept
: p(nullptr) {}

Expand Down

0 comments on commit 651710a

Please sign in to comment.