0

I have a function that does work on an array, and the first 4 bytes are reserved for instructions. After the operations, the result lies from index 3 to the end. I am currently creating another array and copying from element 4 to the end.

Is there any way to modify the array (possibly using unsafe code) in order to change the starting location / resize? This would remove the copying and allocation and massively boost performance. There is Array.Resize, but it is not what i am looking for.

8
  • What about this question is not explained here where it says: "Copies a range of elements from an Array starting at the specified source index and pastes them to another Array starting at the specified destination index. The length and the indexes are specified as 64-bit integers." ? Commented Mar 6, 2021 at 16:10
  • Or, do I miss something? Will removing these first 3 elements boost performance? Commented Mar 6, 2021 at 16:11
  • it is technically possible to access the array via pointers using unsafe code. Tho at this point the question should really be, is C# the language you should be using given your problem. Commented Mar 6, 2021 at 16:14
  • @Luuk i want to avoid copying, which is what i am currently doing. Commented Mar 6, 2021 at 16:30
  • @Vulpex there is no other way, this integrates into a c# framework. I may actually be able to hack it because the data is strings, so i can implement a base64 conversion that uses an index. Commented Mar 6, 2021 at 16:31

1 Answer 1

1

You can use Span<T> to represent data without copying the array.

var testData = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
var result = new Span<byte>(testData).Slice(3);
//result -> {3, 4, 5, 6, 7, 8, 9, 10}

https://learn.microsoft.com/en-us/archive/msdn-magazine/2018/january/csharp-all-about-span-exploring-a-new-net-mainstay

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.