Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Memory Management of C# with Unity Native Collections

12,661 views

Published on

ECS Perfect Understand Seminor.

Published in: Technology
  • Be the first to comment

Memory Management of C# with Unity Native Collections

  1. 1. 河合 宜文 / Kawai Yoshifumi / @neuecc New World, Inc. C# Unity C#大統一理論
  2. 2. C#最速シリアライザ https://github.com/neuecc/MessagePack-CSharp/
  3. 3. 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"
  4. 4. Introduction
  5. 5. ええ、まぁ、はい。 物理エンジン ツイーンエンジン
  6. 6. ECS is safe, pure C#...is a lie.
  7. 7. C# on Virtual Machine
  8. 8. Publishing Fall 2018 https://prodotnetmemory.com
  9. 9. CoreCLR - .NET Core Runtime System Unity Runtime
  10. 10. https://github.com/Unity-Technologies/bdwgc https://github.com/dotnet/coreclr/tree/master/src/vm
  11. 11. VMが違えばメモリ管理も違う
  12. 12. Stack and Heap
  13. 13. AppDomain Thread Stack HeapThread Stack
  14. 14. AppDomain Thread Stack HeapThread Stack
  15. 15. SharpLabでメモリの中身を見よう https://sharplab.io/
  16. 16. Native Collections
  17. 17. Heap is managed, is uncontrollable
  18. 18. 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); } }
  19. 19. public unsafe struct Data { public byte* header; [NativeSetThreadIndex] public int threadIndex; public fixed int slot[JobsUtility.MaxJobThreadCount]; }
  20. 20. public struct Entity : IEquatable<Entity> { public int Index; public int Version; }
  21. 21. Conclusion
  22. 22. この世は荒野だ! Managedで平和なC# Worldは終わった 時代はVMをどうバイパスするかに命をかける時代 (この傾向はCoreCLRも同様で、配列の代わりに裏でポインタを持つSpan<T>と いう型を投入して同じようなことやってる) なるべくsafeな世界に見せようという努力はそこそこ伺える が、まぁもう隠しきれない本質的なunsafe worldがそこに…… (C#なのに!)メモリを意識することで(C#なのに!)、より挙動を理解し やすく、また、より効率の良いAPIの使い方ができるでしょう

×