河合 宜文 / Kawai Yoshifumi / @neuecc
New World, Inc.
C#
Unity
C#大統一理論
C#最速シリアライザ
https://github.com/neuecc/MessagePack-CSharp/
Reactive Extensions for Unity
https://github.com/neuecc/UniRx/
async/await(UniTask)
async UniTask<string> DemoAsync()
{
// You can await Unity's AsyncObject
var asset = await Resources.LoadAsync<TextAsset>("foo");
// .ConfigureAwait accepts progress callback
await SceneManager.LoadSceneAsync("scene2").ConfigureAwai
// await frame-based operation(you can also await frame c
await UniTask.Delay(TimeSpan.FromSeconds(3));
// like 'yield return WaitForEndOfFrame', or Rx's Observe
await UniTask.Yield(PlayerLoopTiming.PostLateUpdate);
// You can await standard task
await Task.Run(() => 100);
// get async webrequest
async UniTask<string> GetTextAsync(UnityWebRequest req)
{
var op = await req.SendWebRequest();
return op.downloadHandler.text;
}
var task1 = GetTextAsync(UnityWebRequest.Get("http://goog
var task2 = GetTextAsync(UnityWebRequest.Get("http://bing
var task3 = GetTextAsync(UnityWebRequest.Get("http://yaho
// concurrent async-wait and get result easily by tuple s
var (google, bing, yahoo) = await UniTask.WhenAll(task1,
// You can handle timeout easily
await GetTextAsync(UnityWebRequest.Get("http://unity.com"
Introduction
ええ、まぁ、はい。
物理エンジン
ツイーンエンジン
ECS is safe, pure C#...is a lie.
C# on Virtual Machine
Publishing Fall 2018
https://prodotnetmemory.com
CoreCLR - .NET Core Runtime System
Unity Runtime
https://github.com/Unity-Technologies/bdwgc
https://github.com/dotnet/coreclr/tree/master/src/vm
VMが違えばメモリ管理も違う
Stack and Heap
AppDomain
Thread
Stack
HeapThread
Stack
AppDomain
Thread
Stack
HeapThread
Stack
SharpLabでメモリの中身を見よう
https://sharplab.io/
Native Collections
Heap is managed, is uncontrollable
public unsafe struct SimpleNativeArray<T> : IDisposable where T : struct
{
void* nativeMemory;
int length;
Allocator allocator;
public SimpleNativeArray(int length, Allocator allocator)
{
var size = UnsafeUtility.SizeOf<T>() * (long)length;
this.nativeMemory = UnsafeUtility.Malloc(size, UnsafeUtility.AlignOf<T>(), allocator);
this.length = length;
this.allocator = allocator;
}
public int Length => length;
public T this[int index]
{
get => UnsafeUtility.ReadArrayElement<T>(nativeMemory, index);
set => UnsafeUtility.WriteArrayElement(nativeMemory, index, value);
}
public void Dispose()
{
UnsafeUtility.Free(nativeMemory, allocator);
}
}
public unsafe struct Data
{
public byte* header;
[NativeSetThreadIndex]
public int threadIndex;
public fixed int slot[JobsUtility.MaxJobThreadCount];
}
public struct Entity : IEquatable<Entity>
{
public int Index;
public int Version;
}
Conclusion
この世は荒野だ!
Managedで平和なC# Worldは終わった
時代はVMをどうバイパスするかに命をかける時代
(この傾向はCoreCLRも同様で、配列の代わりに裏でポインタを持つSpan<T>と
いう型を投入して同じようなことやってる)
なるべくsafeな世界に見せようという努力はそこそこ伺える
が、まぁもう隠しきれない本質的なunsafe worldがそこに……
(C#なのに!)メモリを意識することで(C#なのに!)、より挙動を理解し
やすく、また、より効率の良いAPIの使い方ができるでしょう

Memory Management of C# with Unity Native Collections