Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[unreal]结构体、容器成员Getter性能优化 #693

Closed
chexiongsheng opened this issue Mar 11, 2022 · 3 comments
Closed

[unreal]结构体、容器成员Getter性能优化 #693

chexiongsheng opened this issue Mar 11, 2022 · 3 comments

Comments

@chexiongsheng
Copy link
Collaborator

chexiongsheng commented Mar 11, 2022

目前获取结构体、容器成员,都是会new一个js对象承载结构体、容器的指针。开销比较大,实际测试Vector成员的Get会比Set慢几乎一个数量级。

最早是有个指针到js对象的缓存机制,后面去掉了。

回溯去掉的原因,当初是为了解决结构体嵌套,内外层指针相同导致冲突的问题。

比如如下类型:

USTRUCT(BlueprintType)
struct FSomeData
{
public:
    GENERATED_USTRUCT_BODY()

public:
    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    FVector2D Alignment;

    int DoNoSerialize;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
    int WillSerialize;
};

FSomeData类型对象的指针,和其成员Alignment的指针是相同的,这样会导致someData.Alignment返回的是someData对象

而容器成员由于和结构体共享cache,也这么改了。

@chexiongsheng
Copy link
Collaborator Author

解决办法

1、结构体和容器cache分开;这样容器不会和结构体冲突,它可以正常cache;
2、FindOrAddStruct添加一个ForceNoCache参数,可在结构体成员偏移为0时,保持现在的不缓存策略,而其它情况则用缓存优化性能。

@chexiongsheng
Copy link
Collaborator Author

修改后,Get甚至比Set还要稍快一点

@chexiongsheng
Copy link
Collaborator Author

chexiongsheng commented Apr 24, 2022

ForceNoCache也并不理想
比较严重的问题是静态绑定做这样的优化比较困难,其次是结构体的第一个结构体字段做不了这样的优化。
新增一个FObjectCacheNode数据结构。
思路是每个Node会存放type_id(如果在UStruct常见就是ScriptStruct对象)及其对应的v8对象。只有type_id相等了才认为是找到特定的对象了。
对于大多数情况(无嵌套struct)额外多两个指针(type_id, next)。性能上静态能比反射快。而结构体的第一个结构体字段也能优化了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant