BeyondDimension / SteamTools

🛠「Watt Toolkit」是一个开源跨平台的多功能 Steam 工具箱。
https://steampp.net
GNU General Public License v3.0
20.09k stars 1.3k forks source link

👑[Enhancement] Improve SettingsHost #1668

Closed AigioL closed 2 years ago

AigioL commented 2 years ago

Settings 需要一个键值对存储以及实现 MVVM,目前使用的方案 V2 SettingsHost 来自 V1 SettingsHost 版本中改进,但还不够好

目前所有数据存储在单个文件上 Config.mpo 任何更改触发的保存都将数据全量写入一次,且在启动时需要初始化读取文件

在 Android 上,根据耗时监控的数据,初始化花费了 267ms,预期希望耗时减少到个位数或 0ms `(R8`{UV}MM0SU LLYW3}QL

解决方案

SharedPreferences

它使用 xml 文件存储,且有 JNI 开销,可能效果还不如现在使用的 V2 SettingsHost
Xamarin.Essentials / MAUI Essentials 中 Preferences 也是使用 SharedPreferences 实现的 https://github.com/xamarin/Essentials/blob/1.7.3/Xamarin.Essentials/Preferences/Preferences.android.cs#L145 https://github.com/dotnet/maui/blob/6.0.408/src/Essentials/src/Preferences/Preferences.android.cs#L145

AndroidX Jetpack DataStore

DataStore 是一种数据存储解决方案,允许您使用协议缓冲区存储键值对或类型化对象。DataStore 使用 Kotlin 协程和 Flow 以异步、一致的事务方式存储数据。 使用 Preferences DataStore 但 Xamarin.AndroidX 中暂未提供该库的绑定 在 config.json 可查询已提供绑定的库清单

Tencent.MMKV

MMKV 是基于 mmap 内存映射的 key-value 组件,底层序列化/反序列化使用 protobuf 实现,性能高,稳定性强。从 2015 年中至今在微信上使用,其性能和稳定性经过了时间的验证。近期也已移植到 Android / macOS / Win32 / POSIX 平台,一并开源。 releases 上没有编译好的二进制文件,得克隆源码自行编译😓,Android 到是有 Maven 包 核心实现在 C++/Native 中,或许能直接 P/Invoke 减少 .NET 类型到 JVM 类型中的开销

Microsoft.Extensions.Configuration

.NET 中的配置 ASP.NET Core 中的配置 在 ASP.NET Core 中默认使用的配置,也是一个抽象层,一般使用 Json 的提供程序。 优点在于较方便更改,适合明文存储的配置,且配合 IOptionsMonitor 能够简单实现监听本地文件修改
不过需要自行实现在退出时或类似的事件上进行保存
但性能应该与 V2 版本的 SettingsHost 相近

DBreeze Database

DBreeze Database is a professional, open-source, multi-paradigm (embedded Key-Value store, objects, NoSql, text search, multi-parameter search etc.), multi-threaded, transactional and ACID-compliant data management system for .NET 3.5> / Xamarin MONO Android iOS / .NET Core 1.0> / .NET Standard 1.6> / Universal Windows Platform / .NET Portable / .NET5 / CoreRT ...for servers, desktops, mobiles and internet-of-things... Made with C#

FASTER

The FasterKV key-value store and cache in C# works in .NET Framework and .NET core, and can be used in both a single-threaded and highly concurrent setting. It has been tested to work on both Windows and Linux. It exposes an API that allows one to performs a mix of Reads, Blind Updates (Upserts), and atomic Read-Modify-Write operations. It supports data larger than memory, and accepts an IDevice implementation for storing logs on storage. We have provided IDevice implementations for local file system and Azure Page Blobs, but one may create new devices as well. We also offer meta-devices that can group device instances into sharded and tiered configurations.

ElemenTP commented 2 years ago

也许可以试试KV数据库? hhblaze/DBreeze microsoft/FASTER

AigioL commented 2 years ago

也许可以试试KV数据库? hhblaze/DBreeze microsoft/FASTER

DBreeze 看上去还行,之后改进会搞个性能测试比较一下

FASTER 这个文档上写了

 It has been tested to work on both Windows and Linux.

之后也可以放在性能测试里试一下,不过感觉这类用于服务端的库,可能会像 EFCore 一样不太适合在客户端上用,像 EFCore,DbContext 中有少量的实体,初始化时就相当耗时,导致 App 启动屏幕停留时间太久

AigioL commented 2 years ago

https://github.com/BeyondDimension/SteamTools/commit/48083a90d2d0792cb9930cf0aa35c93d1845e5ae DBreeze