跳到主要内容

Fory v0.15.0 发布

· 阅读需 19 分钟
杨朝坤

Apache Fory 团队很高兴宣布 0.15.0 版本正式发布。这是一个重要版本,包含来自 17 位贡献者的 144 个 PR。请访问 Install 页面 获取各平台安装方式。

发布亮点

Go Serialization:首次发布

Apache Fory 0.15.0 是首个正式提供 Go serialization 支持的版本。 该实现提供了高性能序列化能力、跨语言兼容性,以及面向生产环境的配置选项。

关键能力:

  • 支持与 Java、Python、cpp、Rust、JavaScript 的 Cross-language mode(fory.WithXlang(true)
  • 默认基于 reflection 的 serialization,并提供可选的实验性 AOT code generation 以优化 hot paths
  • 支持 shared/circular object graphs 的 reference tracking(fory.WithTrackRef(true) + fory:"ref" tags)
  • 提供用于 schema evolution 的 Compatible mode(fory.WithCompatible(true)),支持字段新增/删除/重排
  • 提供 thread-safe wrapper(github.com/apache/fory/go/fory/threadsafe)以支持并发工作负载

快速开始

package main

import (
"github.com/apache/fory/go/fory"
)

type User struct {
ID int64
Name string
}

func main() {
f := fory.New(
fory.WithXlang(true),
fory.WithCompatible(true),
)
_ = f.RegisterStruct(User{}, 1)

data, _ := f.Serialize(&User{ID: 1, Name: "Alice"})
var out User
_ = f.Deserialize(data, &out)
}

Go 基准测试

以下是代表性数据结构上的耗时结果(ns/op,越低越好),用于比较 Fory、Protobuf 与 Msgpack。

Data TypeOperationForyProtobufMsgpack
StructSerialize66.097.8184.9
StructDeserialize82.790.9309.6
StructlistSerialize632.81783.03340.0
StructlistDeserialize906.41891.05709.0
SampleSerialize137.3367.31492.0
SampleDeserialize263.6422.22661.0
SamplelistSerialize1962.07087.026169.0
SamplelistDeserialize4234.09321.053615.0
MediacontentSerialize268.8471.1773.7
MediacontentDeserialize426.9553.11432.0
MediacontentlistSerialize3736.09107.013911.0
MediacontentlistDeserialize7247.011435.027975.0

序列化数据大小(bytes):

Data TypeForyProtobufMsgpack
Struct586157
Sample446375524
MediaContent342301400
StructList56012601146
SampleList7600756010486
MediaContentList577660808006

注:结果与硬件和实现版本相关。详细说明请参考 Go benchmark 文档: https://fory.apache.org/docs/benchmarks/go/

Fory Schema IDL 与 Compiler:首次发布

Apache Fory 0.15.0 同时带来了 Fory schema IDL 与 compiler toolchain 的首次发布。你可以一次定义 schema,并为多语言生成 native types 与 registration code。

关键能力:

  • Schema-first 开发,支持 enummessageunion
  • Fory-native 字段语义:optional(nullability)、ref(shared/circular references)、listmap
  • 支持 Java、Python、Go、Rust 与 cpp 的多语言代码生成
  • 支持 Protobuf(.proto)与 FlatBuffers(.fbs)前端,并转换为 Fory IR/codegen
  • 提供 idiomatic 的生成 API,并包含 to/from bytes helper

快速开始

pip install fory-compiler
foryc example.fdl --lang java,python,go,rust,cpp --output ./generated
package example;

message Person [id=101] {
string name = 1;
optional string email = 2;
}

生成代码示例

foryc 会生成 idiomatic 的 native types,并附带 registration/byte helpers。以上 schema 的生成代码示例如下:

// Go (excerpt)
type Person struct {
Name string `fory:"id=1"`
Email optional.Optional[string] `fory:"id=2"`
}

func (m *Person) ToBytes() ([]byte, error) { ... }
func (m *Person) FromBytes(data []byte) error { ... }
func RegisterTypes(f *fory.Fory) error {
return f.RegisterStruct(Person{}, 101)
}
// Java (excerpt)
public class Person {
@ForyField(id = 1)
private String name;

@ForyField(id = 2, nullable = true)
private String email;

public byte[] toBytes() { ... }
public static Person fromBytes(byte[] bytes) { ... }
}
# Python (excerpt)
@pyfory.dataclass
class Person:
name: str = pyfory.field(id=1, default="")
email: Optional[str] = pyfory.field(id=2, default=None)

def to_bytes(self) -> bytes: ...
@classmethod
def from_bytes(cls, data: bytes) -> "Person": ...

功能特性

Bug 修复

其他改进

新贡献者

完整变更日志: https://github.com/apache/fory/compare/v0.14.1...v0.15.0