Repository: dotnet/runtime
File: src/coreclr/nativeaot/Common/src/Internal/Runtime/CompilerHelpers/StartupCodeHelpers.cs (C#)

Code before:
                    Unsafe.Add(ref rawSpineData, currentBase) = obj;

                    // Update the base pointer to point to the pinned object
                    *pBlock = *(IntPtr*)&obj;
                }

                currentBase++;
            }

            return spine;

            static void* ReadRelPtr32(void* address)
                => (byte*)address + *(int*)address;
        }

        private static unsafe void RehydrateData(IntPtr dehydratedData, int length)
        {
            // Destination for the hydrated data is in the first 32-bit relative pointer
            byte* pDest = (byte*)ReadRelPtr32((void*)dehydratedData);

            // Next is the number of fixups at the end of the stream
            int numFixups = *(int*)(dehydratedData + sizeof(int));

            // The dehydrated data follows
            byte* pCurrent = (byte*)dehydratedData + sizeof(int) * 2;
            byte* pEnd = (byte*)dehydratedData + length - (sizeof(int) * numFixups);

            // Fixup table immediately follows the command stream
            int* pFixups = (int*)pEnd;

            while (pCurrent < pEnd)
            {
                pCurrent = DehydratedDataCommand.Decode(pCurrent, out int command, out int payload);
                switch (command)
                {
                    case DehydratedDataCommand.Copy:
                        Debug.Assert(payload != 0);
                        if (payload < 4)
                        {
                            *pDest = *pCurrent;
                            if (payload > 1)
                                *(short*)(pDest + payload - 2) = *(short*)(pCurrent + payload - 2);
                        }
                        else if (payload < 8)
                        {
                            *(int*)pDest = *(int*)pCurrent;
                            *(int*)(pDest + payload - 4) = *(int*)(pCurrent + payload - 4);
                        }
                        else if (payload <= 16)
                        {
#if TARGET_64BIT

Review comment (suggestion):
I think this would be easier math/require fewer axioms if instead of number of fixups we encode size of dehydrated data. (Also drop the length parameter from the method.)

```suggestion
            // Next is length of the dehydrated data
            int length = *(int*)(dehydratedData + sizeof(int));

// The dehydrated data follows
            byte* pCurrent = (byte*)dehydratedData + sizeof(int) * 2;
            byte* pEnd = (byte*)dehydratedData + length;
```

Code after:
                    Unsafe.Add(ref rawSpineData, currentBase) = obj;

                    // Update the base pointer to point to the pinned object
                    *pBlock = *(IntPtr*)&obj;
                }

                currentBase++;
            }

            return spine;

            static void* ReadRelPtr32(void* address)
                => (byte*)address + *(int*)address;
        }

        private static unsafe void RehydrateData(IntPtr dehydratedData)
        {
            // Destination for the hydrated data is in the first 32-bit relative pointer
            byte* pDest = (byte*)ReadRelPtr32((void*)dehydratedData);

            // Next is length of the dehydrated data
            int length = *(int*)(dehydratedData + sizeof(int));

            // The dehydrated data follows
            byte* pCurrent = (byte*)dehydratedData + sizeof(int) * 2;
            byte* pEnd = (byte*)dehydratedData + length;

            // Fixup table immediately follows the command stream
            int* pFixups = (int*)pEnd;

            while (pCurrent < pEnd)
            {
                pCurrent = DehydratedDataCommand.Decode(pCurrent, out int command, out int payload);
                switch (command)
                {
                    case DehydratedDataCommand.Copy:
                        Debug.Assert(payload != 0);
                        if (payload < 4)
                        {
                            *pDest = *pCurrent;
                            if (payload > 1)
                                *(short*)(pDest + payload - 2) = *(short*)(pCurrent + payload - 2);
                        }
                        else if (payload < 8)
                        {
                            *(int*)pDest = *(int*)pCurrent;
                            *(int*)(pDest + payload - 4) = *(int*)(pCurrent + payload - 4);
                        }
                        else if (payload <= 16)
                        {
#if TARGET_64BIT