Skip to content

Commit

Permalink
[k2] implement msgpack serialize functions (#1215)
Browse files Browse the repository at this point in the history
  • Loading branch information
astrophysik authored Jan 21, 2025
1 parent d14882b commit 35de7ea
Show file tree
Hide file tree
Showing 72 changed files with 407 additions and 356 deletions.
1 change: 1 addition & 0 deletions .idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions builtin-functions/kphp-light/math.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,15 @@ function bcsqrt($num ::: string, $scale ::: int = PHP_INT_MIN): string;

function bcsub ($lhs ::: string, $rhs ::: string, $scale ::: int = PHP_INT_MIN) ::: string;

function is_nan ($v ::: float) ::: bool;

// ===== UNSUPPORTED =====

/** @kphp-extern-func-info generate-stub */
function is_finite ($v ::: float) ::: bool;
/** @kphp-extern-func-info generate-stub */
function is_infinite ($v ::: float) ::: bool;
/** @kphp-extern-func-info generate-stub */
function is_nan ($v ::: float) ::: bool;
/** @kphp-extern-func-info generate-stub */
function random_int($l ::: int, $r ::: int) ::: int | false;
/** @kphp-extern-func-info generate-stub */
function random_bytes($length ::: int) ::: string | false;
Expand Down
16 changes: 8 additions & 8 deletions builtin-functions/kphp-light/serialize.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ function serialize($v ::: mixed): string;
*/
function unserialize ($v ::: string): mixed;

// ===== UNSUPPORTED =====
function instance_serialize(object $instance) ::: string | null;

/** @kphp-extern-func-info cpp_template_call */
function instance_deserialize($serialized ::: string, $to_type ::: string) ::: instance<^2>;

/** @kphp-extern-func-info generate-stub */
function msgpack_serialize($v ::: mixed) ::: string | null;
/** @kphp-extern-func-info generate-stub */

function msgpack_deserialize($v ::: string) ::: mixed;

// ===== UNSUPPORTED =====

/** @kphp-extern-func-info can_throw generate-stub */
function msgpack_serialize_safe($v ::: mixed) ::: string;
/** @kphp-extern-func-info can_throw generate-stub */
function msgpack_deserialize_safe($v ::: string) ::: mixed;

function instance_serialize(object $instance) ::: string | null;
/** @kphp-extern-func-info can_throw */
function instance_serialize_safe(object $instance) ::: string;
/** @kphp-extern-func-info cpp_template_call */
function instance_deserialize($serialized ::: string, $to_type ::: string) ::: instance<^2>;
/** @kphp-extern-func-info cpp_template_call can_throw */
function instance_deserialize_safe($serialized ::: string, $to_type ::: string) ::: instance<^2>;

16 changes: 5 additions & 11 deletions compiler/code-gen/declarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -946,10 +946,6 @@ void ClassDeclaration::compile_accept_visitor_methods(CodeGenerator &W, ClassPtr
}

void ClassDeclaration::compile_msgpack_declarations(CodeGenerator &W, ClassPtr klass) {
if (G->is_output_mode_k2()) {
// The current version of runtime-light does not support msgpack visitors
return;
}
if (!klass->is_serializable) {
return;
}
Expand Down Expand Up @@ -1070,7 +1066,7 @@ void ClassMembersDefinition::compile(CodeGenerator &W) const {
(klass->need_instance_cache_visitors && !G->is_output_mode_k2()) ||
(klass->need_instance_memory_estimate_visitor && !G->is_output_mode_k2());

if (!need_generic_accept && (!klass->is_serializable || G->is_output_mode_k2()) && klass->json_encoders.empty()) {
if (!need_generic_accept && !klass->is_serializable && klass->json_encoders.empty()) {
return;
}

Expand Down Expand Up @@ -1113,12 +1109,10 @@ void ClassMembersDefinition::compile(CodeGenerator &W) const {
W << NL;
compile_accept_json_visitor(W, klass);

if (!G->is_output_mode_k2()) {
W << NL;
compile_msgpack_serialize(W, klass);
W << NL;
compile_msgpack_deserialize(W, klass);
}
W << NL;
compile_msgpack_serialize(W, klass);
W << NL;
compile_msgpack_deserialize(W, klass);

W << CloseNamespace();

Expand Down
40 changes: 40 additions & 0 deletions runtime-common/core/allocator/script-allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Compiler for PHP (aka KPHP)
// Copyright (c) 2025 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#pragma once

#include <cstddef>

#include "runtime-common/core/allocator/runtime-allocator.h"

namespace kphp {

namespace memory {

template<typename T>
struct script_allocator {
using value_type = T;

constexpr value_type *allocate(size_t n) noexcept {
return static_cast<value_type *>(RuntimeAllocator::get().alloc_script_memory(n * sizeof(T)));
}

constexpr void deallocate(T *p, size_t n) noexcept {
RuntimeAllocator::get().free_script_memory(p, n);
}
};

template<class T, class U>
constexpr bool operator==(const script_allocator<T> & /*unused*/, const script_allocator<U> & /*unused*/) {
return true;
}

template<class T, class U>
constexpr bool operator!=(const script_allocator<T> & /*unused*/, const script_allocator<U> & /*unused*/) {
return false;
}

} // namespace memory

} // namespace kphp
File renamed without changes.
2 changes: 1 addition & 1 deletion runtime-common/core/utils/kphp-assert-core.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <stdlib.h>
#include <cstdlib>

#include "common/wrappers/likely.h"

Expand Down
4 changes: 4 additions & 0 deletions runtime-common/stdlib/math/math-functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ inline double f$log(double v) noexcept {
return log(v);
}

inline bool f$is_nan(double v) noexcept {
return (std::fpclassify(v) == FP_NAN);
}

inline double f$log(double v, double base) noexcept {
if (v <= 0.0 || base <= 0.0 || fabs(base - 1.0) < 1e-9) {
return 0.0;
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
#include <stdexcept>

#include "runtime-common/core/runtime-core.h"
#include "runtime/msgpack/adaptor_base.h"
#include "runtime/msgpack/check_instance_depth.h"
#include "runtime/msgpack/object.h"
#include "runtime/msgpack/packer.h"
#include "runtime/msgpack/unpack_exception.h"
#include "runtime-common/stdlib/msgpack/adaptor_base.h"
#include "runtime-common/stdlib/msgpack/check_instance_depth.h"
#include "runtime-common/stdlib/msgpack/object.h"
#include "runtime-common/stdlib/msgpack/packer.h"
#include "runtime-common/stdlib/msgpack/unpack_exception.h"

namespace vk::msgpack {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@

#include <cstddef>

#include "runtime-common/stdlib/serialization/serialization-context.h"

namespace vk::msgpack {
struct CheckInstanceDepth {
static size_t depth;
static constexpr size_t max_depth = 20;

CheckInstanceDepth() {
depth++;
SerializationLibContext::get().instance_depth++;
}

CheckInstanceDepth(const CheckInstanceDepth &) = delete;
CheckInstanceDepth &operator=(const CheckInstanceDepth &) = delete;

static bool is_exceeded() {
return depth > max_depth;
return SerializationLibContext::get().instance_depth > max_depth;
}

~CheckInstanceDepth() {
if (!is_exceeded()) {
depth--;
SerializationLibContext::get().instance_depth--;
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <cstdint>
#include <type_traits>

#include "runtime/msgpack/adaptor_base.h"
#include "runtime-common/stdlib/msgpack/adaptor_base.h"

namespace vk::msgpack {
enum class stored_type {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

#include <cstring>

#include "runtime/msgpack/object_visitor.h"
#include "runtime-common/stdlib/msgpack/object_visitor.h"

#include "runtime/msgpack/unpack_exception.h"
#include "runtime/msgpack/zone.h"
#include "runtime-common/stdlib/msgpack/unpack_exception.h"
#include "runtime-common/stdlib/msgpack/zone.h"

namespace vk::msgpack {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
#include <vector>

#include "common/mixin/not_copyable.h"
#include "runtime/msgpack/object.h"
#include "runtime-common/core/allocator/script-allocator.h"
#include "runtime-common/core/std/containers.h"
#include "runtime-common/stdlib/msgpack/object.h"

namespace vk::msgpack {

Expand All @@ -18,7 +20,7 @@ class object_visitor : vk::not_copyable {
public:
explicit object_visitor(msgpack::zone &zone) noexcept;

msgpack::object &&flush() &&noexcept {
msgpack::object &&flush() && noexcept {
return std::move(m_obj);
}

Expand Down Expand Up @@ -65,7 +67,7 @@ class object_visitor : vk::not_copyable {

private:
msgpack::object m_obj;
std::vector<msgpack::object *> m_stack{};
kphp::stl::vector<msgpack::object *, kphp::memory::script_allocator> m_stack{};
msgpack::zone &m_zone;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// Copyright (c) 2022 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include "runtime/msgpack/packer.h"
#include "runtime-common/stdlib/msgpack/packer.h"

#include "runtime-common/core/runtime-core.h"
#include "runtime/msgpack/sysdep.h"
#include "runtime-common/stdlib/msgpack/sysdep.h"

namespace vk::msgpack {

Expand Down Expand Up @@ -372,6 +372,4 @@ void packer<Stream>::append_buffer(const char *buf, size_t len) noexcept {

template class packer<string_buffer>;

uint32_t packer_float32_decorator::serialize_as_float32_ = 0;

} // namespace vk::msgpack
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

#include "common/mixin/not_copyable.h"

#include "runtime/msgpack/adaptor_base.h"
#include "runtime-common/stdlib/msgpack/adaptor_base.h"
#include "runtime-common/stdlib/serialization/serialization-context.h"

namespace vk::msgpack {

Expand Down Expand Up @@ -68,14 +69,15 @@ class packer : private vk::not_copyable {
class packer_float32_decorator {
public:
static void clear() noexcept {
serialize_as_float32_ = 0;
SerializationLibContext::get().serialize_as_float32_ = 0;
}

template<class StreamT, class T>
static void pack_value_float32(packer<StreamT> &packer, const T &value) {
++serialize_as_float32_;
auto &serialization_context{SerializationLibContext::get()};
++serialization_context.serialize_as_float32_;
pack_value(packer, value);
--serialize_as_float32_;
--serialization_context.serialize_as_float32_;
}

template<class StreamT, class T>
Expand All @@ -85,15 +87,12 @@ class packer_float32_decorator {

template<class StreamT>
static void pack_value(packer<StreamT> &packer, double value) {
if (serialize_as_float32_ > 0) {
if (SerializationLibContext::get().serialize_as_float32_ > 0) {
packer.pack(static_cast<float>(value));
} else {
packer.pack(value);
}
}

private:
static uint32_t serialize_as_float32_;
};

} // namespace vk::msgpack
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
#include <cassert>
#include <cstring>
#include <type_traits>
#include <vector>

#include "runtime/msgpack/object_visitor.h"
#include "runtime/msgpack/parser.h"
#include "runtime/msgpack/sysdep.h"
#include "runtime-common/core/allocator/script-allocator.h"
#include "runtime-common/core/std/containers.h"
#include "runtime-common/stdlib/msgpack/object_visitor.h"
#include "runtime-common/stdlib/msgpack/parser.h"
#include "runtime-common/stdlib/msgpack/sysdep.h"

namespace vk::msgpack {
namespace {
Expand Down Expand Up @@ -128,7 +129,7 @@ struct unpack_stack {
parse_return consume(Visitor &visitor);

private:
std::vector<stack_elem> m_stack;
kphp::stl::vector<stack_elem, kphp::memory::script_allocator> m_stack;
};

unpack_stack::unpack_stack() noexcept {
Expand Down Expand Up @@ -199,19 +200,19 @@ parse_return unpack_stack::consume(Visitor &visitor) {
enum class msgpack_cs : uint32_t {
HEADER = 0x00, // nil

// MSGPACK_CS_ = 0x01,
// MSGPACK_CS_ = 0x02, // false
// MSGPACK_CS_ = 0x03, // true
// MSGPACK_CS_ = 0x01,
// MSGPACK_CS_ = 0x02, // false
// MSGPACK_CS_ = 0x03, // true

// bin and ext support is removed
//
// BIN_8 = 0x04,
// BIN_16 = 0x05,
// BIN_32 = 0x06,
//
// EXT_8 = 0x07,
// EXT_16 = 0x08,
// EXT_32 = 0x09,
// bin and ext support is removed
//
// BIN_8 = 0x04,
// BIN_16 = 0x05,
// BIN_32 = 0x06,
//
// EXT_8 = 0x07,
// EXT_16 = 0x08,
// EXT_32 = 0x09,

FLOAT = 0x0a,
DOUBLE = 0x0b,
Expand All @@ -224,13 +225,13 @@ enum class msgpack_cs : uint32_t {
INT_32 = 0x12,
INT_64 = 0x13,

// ext support is removed
//
// FIXEXT_1 = 0x14,
// FIXEXT_2 = 0x15,
// FIXEXT_4 = 0x16,
// FIXEXT_8 = 0x17,
// FIXEXT_16 = 0x18,
// ext support is removed
//
// FIXEXT_1 = 0x14,
// FIXEXT_2 = 0x15,
// FIXEXT_4 = 0x16,
// FIXEXT_8 = 0x17,
// FIXEXT_16 = 0x18,

STR_8 = 0x19, // str8
STR_16 = 0x1a, // str16
Expand Down
File renamed without changes.
Loading

0 comments on commit 35de7ea

Please sign in to comment.