Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
MasterMemory v3 最速確認会
Search
Yuichiro MUKAI
December 28, 2024
Technology
0
910
MasterMemory v3 最速確認会
「【年末だよ】Unity お・と・なのLT大会 2024 」で発表した資料になります。
Yuichiro MUKAI
December 28, 2024
Tweet
Share
More Decks by Yuichiro MUKAI
See All by Yuichiro MUKAI
Timeline エディター拡張入門
yucchiy
0
1.2k
Other Decks in Technology
See All in Technology
初めてのAzure FunctionsをClaude Codeで作ってみた / My first Azure Functions using Claude Code
hideakiaoyagi
1
190
「Chatwork」の認証基盤の移行とログ活用によるプロダクト改善
kubell_hr
1
100
Welcome to the LLM Club
koic
0
140
2年でここまで成長!AWSで育てたAI Slack botの軌跡
iwamot
PRO
2
460
Amazon Bedrockで実現する 新たな学習体験
kzkmaeda
1
410
~宇宙最速~2025年AWS Summit レポート
satodesu
1
1.4k
Microsoft Build 2025 技術/製品動向 for Microsoft Startup Tech Community
torumakabe
2
210
変化する開発、進化する体系時代に適応するソフトウェアエンジニアの知識と考え方(JaSST'25 Kansai)
mizunori
0
140
Navigation3でViewModelにデータを渡す方法
mikanichinose
0
210
TechLION vol.41~MySQLユーザ会のほうから来ました / techlion41_mysql
sakaik
0
160
(非公式) AWS Summit Japan と 海浜幕張 の歩き方 2025年版
coosuke
PRO
1
340
登壇ネタの見つけ方 / How to find talk topics
pinkumohikan
3
320
Featured
See All Featured
The Language of Interfaces
destraynor
158
25k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
281
13k
Being A Developer After 40
akosma
90
590k
The World Runs on Bad Software
bkeepers
PRO
69
11k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
44
2.4k
The Cult of Friendly URLs
andyhume
79
6.4k
Site-Speed That Sticks
csswizardry
10
650
Measuring & Analyzing Core Web Vitals
bluesmoon
7
490
Building an army of robots
kneath
306
45k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
20
1.3k
Transcript
ʲͩΑʳUnity ͓ɾͱɾͳͷLTେձ 2024 2024/12/28 @yucchiy_ MasterMemory v3 最速確認会
• MasterMemoryについてのおさらい • MasterMemoryについて • MessagePackとの関係やコード生成について • MasterMemory v3の変更点の紹介 •
Source Generator対応について • MasterMemory v3の利用方法の紹介 • 導入方法から簡単な使い方について このLTで話したいこと
自己紹介 向井 祐一郎 • 株式会社 サイバーエージェント / 株式会社アプリ ボ ット
• Lead Developer Experience • システム系の基盤開発・プロジェクトの開発支援 SNS・個人活動など • @yucchiy_(X) • Unity Weekly • https://blog.yucchiy.com/tags/unity-weekly/ • UniTips(社内有志で技術書典に執筆)
MasterMemory のおさらい
• マスター デ ータの管理に主眼を置いたインメモリ デ ータベース • 省メモリ・高速なデータベースロード・高速な検索の3点に重点を置く • 弊社導入事例:
Unity/C# ゲ ーム開発における、クライアントでのマスターデータの扱い方 • https://blog.applibot.co.jp/2024/01/19/unity-master-data/ • デ ータベースのフォーマットは MessagePack • データを読み込んで、メモリ上にデータベースを構築する • 書き出されたデータの読み込みでMessagePackのデータの解析に、 MessagePackのシリアライ ザ が必要になる MasterMemory とは https://github.com/Cysharp/MasterMemory
[MemoryTable("person"), MessagePackObject(true)] public record Person { [PrimaryKey] public int PersonId
{ get; set; } [SecondaryKey(0), NonUnique] [SecondaryKey(1, keyOrder: 1), NonUnique] public int Age { get; set; } [SecondaryKey(2), NonUnique] [SecondaryKey(1, keyOrder: 0), NonUnique] public Gender Gender { get; set; } public string Name { get; set; } } デ ータの定義 属性を用いて各カラムの “扱い” を定義
var db = new MemoryDatabase( File.ReadAllBytes( Path.Combine(Application.dataPath, "StreamingAssets", "database.bytes") )
); Person person = db.PersonTable.FindByPersonId(3); RangeView<Person> persons = db.PersonTable.FindByGender(Gender.Male); デ ータベースの検索 データベースの読み込み (byte[] を引数に初期化) PrimaryKeyで検索 SecondaryKeyで範囲検索
internal sealed class PersonFormatter : MessagePack.Formatters.IMessagePackFormatter<Samples.Person> { public void Serialize(
ref MessagePack.MessagePackWriter writer, Samples.Person value, MessagePack.MessagePackSerializerOptions options) { var formatterResolver = options.Resolver; writer.WriteMapHeader(4); writer.WriteRaw(GetSpan_PersonId()); // … } public Samples.Person Deserialize( ref MessagePack.MessagePackReader reader, MessagePack.MessagePackSerializerOptions options) { // … } } 独自型のシリアライ ズ のために、内部でフォーマッタが必要になる MessagePackのフォーマッタ [MemoryTable(“person”), MessagePackObject(true)] public record Person { [PrimaryKey] public int PersonId { get; init; } [SecondaryKey(0), NonUnique] [SecondaryKey(1, keyOrder: 1), NonUnique] public int Age { get; init; } [SecondaryKey(2), NonUnique] [SecondaryKey(1, keyOrder: 0), NonUnique] public Gender Gender { get; init; } public string Name { get; init; } }
MessagePackCompiler(mpc) • IL2CPP下のUnityではフォーマッタの事前生成が必要(AOT Code Generation) • mpcを用いて実行前にコード生成しておく // Simple Sample:
dotnet mpc -i "..\src\Sandbox.Shared.csproj" -o "MessagePackGenerated.cs" // Use force map simulate DynamicContractlessObjectResolver dotnet mpc -i "..\src\Sandbox.Shared.csproj" -o "MessagePackGenerated.cs" -m dotnet tool install --global MessagePack.Generator
定義された デ ータを操作するためのロジックを生成するツール MasterMemory.Generator [MemoryTable("person"), MessagePackObject(true)] public record Person {
[PrimaryKey] public int PersonId { get; set; } [SecondaryKey(0), NonUnique] [SecondaryKey(1, keyOrder: 1), NonUnique] public int Age { get; set; } [SecondaryKey(2), NonUnique] [SecondaryKey(1, keyOrder: 0), NonUnique] public Gender Gender { get; set; } public string Name { get; set; } } Person person = db.PersonTable.FindByPersonId(3); RangeView<Person> persons = db.PersonTable.FindByGender(Gender.Male); MasterMemory.Generator.exe -i “C:\UnitySample” -o “C:\UnitySample\Generated" -n "UnitySample"
MasterMemory v3の変更点
前提: MessagePack v3の ア ップ デ ートについて • v3(v3.0.3)にて、フォーマッタの事前生成がSource Generator化された
• プロジェクトコードのコンパイル時にフォーマッタが自動生成される • 上記の対応に伴い、mpcが不要になった https://github.com/MessagePack-CSharp/MessagePack-CSharp/releases/tag/v3.0.3
• MessagePack v3が取り込まれた • これによりMessagePackのSource Generator対応が取り込まれた • あわせて、MasterMemory.GeneratorがSource Generator化された •
MasterMemoryのコード生成に関する実装が、すべてSource Generator化された MasterMemory v3の ア ップ デ ート https://neue.cc/2024/12/20_mastermemory_v3.html
MasterMemory v3の導入方法
NuGet For Unityで「MasterMemory」をインストールする( だ け!) インストール
“Override References” をONにし、”Assembly References”にDLLを設定 asmdef を切る場合の設定
各アセン ブ リごとに「MasterMemoryGeneratorOptions」を設定する デ ータベースの設定 using MasterMemory; [assembly: MasterMemoryGeneratorOptions( Namespace
= "Samples", IsReturnNullIfKeyNotFound = false, PrefixClassName = "Hoge" )] namespace System.Runtime.CompilerServices { internal sealed class IsExternalInit { } } v2以前ではコマンドラインのオプションで指定していた箇所 Initプロパティを利用したい場合は定義
MasterMemory v3の使い方
[MemoryTable("person"), MessagePackObject(true)] public record Person { [PrimaryKey] public int PersonId
{ get; init; } [SecondaryKey(0), NonUnique] [SecondaryKey(1, keyOrder: 1), NonUnique] public int Age { get; init; } [SecondaryKey(2), NonUnique] [SecondaryKey(1, keyOrder: 0), NonUnique] public Gender Gender { get; init; } public string Name { get; init; } } v2以前と比べ、利用方法に変更はない デ ータの定義
var builder = new DatabaseBuilder(); builder.Append(new Person[] { new() {
PersonId = 1, Age = 20, Gender = Gender.Male, Name = "John" }, new() { PersonId = 2, Age = 22, Gender = Gender.Female, Name = "Jane" }, new() { PersonId = 3, Age = 24, Gender = Gender.Male, Name = "Jack" }, }); File.WriteAllBytes( Path.Combine(Application.dataPath, "StreamingAssets", "database.bytes"), builder.Build() ); v2以前と比べ、利用方法に変更はない デ ータベースの書き出し ゲ ームごとに管理されている マスターデータを読み込んで データベースファイルとして書き出す
var db = new MemoryDatabase( File.ReadAllBytes( Path.Combine(Application.dataPath, "StreamingAssets", "database.bytes") )
); Person person = db.PersonTable.FindByPersonId(3); RangeView<Person> persons = db.PersonTable.FindByGender(Gender.Male); v2以前と比べ、利用方法に変更はない デ ータベースの検索
Unity 6で再現する、Unity側のバ グ とのこと 余談: コンパイル時のエラー https://x.com/neuecc/status/1870994368020296171
• MasterMemory v3の、Source Generator対応に焦点を当てて紹介しました • Source Generator対応により、導入からコード生成の手間が大幅に省けた • これによって開発体験が大きく向上した •
MasterMemory v2からv3について利用方法の大きな変更はない • 下回り(Source Generator対応)がメインになる • 移行コストは低そう(Unityバージョンの要求は高いので、運用タイトルからの移行はチョット高い) まとめ → MasterMemory v3、積極的に使っていきたい