{"id":13730722,"url":"https://github.com/stella3d/SharedArray","last_synced_at":"2025-05-08T03:31:37.159Z","repository":{"id":54638455,"uuid":"246293667","full_name":"stella3d/SharedArray","owner":"stella3d","description":"Zero-copy sharing between managed and native arrays in Unity","archived":false,"fork":false,"pushed_at":"2020-09-16T00:59:43.000Z","size":74,"stargazers_count":163,"open_issues_count":2,"forks_count":13,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-08-04T02:09:46.957Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stella3d.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-10T12:22:08.000Z","updated_at":"2024-08-01T01:40:03.000Z","dependencies_parsed_at":"2022-08-13T22:30:32.191Z","dependency_job_id":null,"html_url":"https://github.com/stella3d/SharedArray","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stella3d%2FSharedArray","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stella3d%2FSharedArray/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stella3d%2FSharedArray/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stella3d%2FSharedArray/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stella3d","download_url":"https://codeload.github.com/stella3d/SharedArray/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224695673,"owners_count":17354452,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-03T02:01:18.556Z","updated_at":"2024-11-14T21:31:31.799Z","avatar_url":"https://github.com/stella3d.png","language":"C#","funding_links":[],"categories":["高性能数据结构和算法","C#"],"sub_categories":["FPS"],"readme":"# SharedArray\nA `SharedArray` is a segment of memory that is represented both as a normal C# array `T[]`, and a Unity [`NativeArray\u003cT\u003e`](https://docs.unity3d.com/ScriptReference/Unity.Collections.NativeArray_1.html).\n\nIt's designed to reduce the overhead of communicating between C# job data in `NativeArray` and APIs that use a normal array of structs, such as [Graphics.DrawMeshInstanced()](https://docs.unity3d.com/ScriptReference/Graphics.DrawMeshInstanced.html), by eliminating the need to copy data.\n\n## Installation\n\nMinimum Unity version is 2018.4.\n\nTo install, grab the latest .unitypackage [from the Releases Tab](https://github.com/stella3d/SharedArray/releases) and import it to your project.\n\n## Usage\n\n\n```csharp\n// SharedArray implicitly converts to both managed and native array\nSharedArray\u003cVector4\u003e shared = new SharedArray\u003cVector4\u003e(8);\nNativeArray\u003cVector4\u003e asNative = shared;\nVector4[] asManaged = shared;\n```\n\nPlease see the [demo project](https://github.com/stella3d/SharedArray-Demo) for a more detailed usage example.\n\n\n## Safety System\n\nUnity's job system has a [safety system for reading \u0026 writing data](https://docs.unity3d.com/Manual/JobSystemSafetySystem.html) (in the Editor only).  This catches cases where a data race would occur and warns you about it.\n\nSharedArray _works with this safety system_, so when you access the data on the main thread, the system knows whether it is safe to read or write, just like using a `NativeArray` allocated the normal way.\n\nHere's all of the operations that include a check of the safety system.\n\n```csharp\nSharedArray\u003cT\u003e sharedArray;            // created elsewhere \n\n// These 4 operations will check that no jobs are using the data, in any way\nT[] asNormalArray = sharedArray; \nsharedArray.Clear();\nsharedArray.Resize(32);\nsharedArray.Dispose();\n\n// Enumerating in either of these ways will check if any jobs are writing to the data, but allow other readers\nforeach(var element in sharedArray) { }\n\nvar enumerator = sharedArray.GetEnumerator();\n```\n\nThe safest way to use SharedArray is: \n1) Manipulate data in a C# job, using the `NativeArray\u003cT\u003e` representation\n2) Convert to a managed array `T[]` only right before you use it on the main thread.  \n\nThis is important if you want the safety system to work - if you pass around a reference to the managed representation, you won't get the safety system checks.\n\nYou can see this pattern demonstrated in the [demo project](https://github.com/stella3d/SharedArray-Demo).\n\n## Aliasing \n\nIt's possible to have the `NativeArray` representation of the data be of a different type than the source managed array.  \n\nTo do so, create the `SharedArray` with 2 types instead of 1 :\n\n```csharp\nVector4[] source = new Vector4[64];\nSharedArray\u003cVector4, float4\u003e shared = new SharedArray\u003cVector4, float4\u003e(source);\nNativeArray\u003cfloat4\u003e native = shared;\nVector4[] asManaged = shared;\n```\n\nThe only safety check that aliasing makes is that the types are both `unmanaged` and the same size.  \n\n#### Why Alias Types ?\n\nAliasing was made to eliminate the overhead of converting between analogous types in `Unity.Mathematics` and `UnityEngine` (such as `float4` \u003c-\u003e `Vector4` or `float4x4` \u003c-\u003e `Matrix4x4`).\n\nThese `Unity.Mathematics` types have optimizations specific to the [Burst compiler](https://docs.unity3d.com/Packages/com.unity.burst@0.2/manual/index.html), and replace the existing Unity math structs and methods.\n  We want to get the compiler-specific performance advantage of using those new types, without the overhead of converting back from `Unity.Mathematics` types. \n  \nFor types that are laid out the same in memory, we can just treat one like the other.  Since we do this for the whole array, there is never any conversion between types happening, and thus no overhead - it's just a different \"view\" on the same memory.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstella3d%2FSharedArray","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstella3d%2FSharedArray","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstella3d%2FSharedArray/lists"}