GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Eliminate `#if` regions related to unity by AArnott · Pull Request #1825 · MessagePack-CSharp/MessagePack-CSharp

Conversation

@AArnott

@AArnott AArnott commented May 24, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

This PR removes unity/mono-specific branches, or changes them from #if based to runtime check based, since in v3.0 we won't have a custom compilation based on unity.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

pCYSl5EDgo reacted with eyes emoji
All reactions
  • 👀 1 reaction
@AArnott
Proposal for how to eliminate #if related to unity
70b7d8f
@AArnott AArnott added this to the v3.0 milestone May 24, 2024
@AArnott
AArnott requested a review from neuecc May 24, 2024 02:35
@AArnott AArnott mentioned this pull request May 24, 2024
Unity consumes messagepack as a nuget package instead of source #1734
Merged
pCYSl5EDgo
pCYSl5EDgo reviewed May 24, 2024
View reviewed changes
Comment thread src/MessagePack/MessagePackUnityOptions.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

pCYSl5EDgo
pCYSl5EDgo reviewed May 24, 2024
View reviewed changes
Comment thread src/MessagePack/SafeBitConverter.cs

@pCYSl5EDgo pCYSl5EDgo May 24, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IL2CPP of Unity 2022 treats Unsafe.ReadUnaligned<T> in Android Armv7 and browser environments specially to ensure that no unaligned memory access happens.
#1734 (comment)

This MessagePackUnityOptions.UnityAndroid conditional branch seems not necessary.
However I do not have Arm v7 Android machine, so I can not guarantee that it won't crash...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AArnott AArnott May 24, 2024

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Back in 2020, @neuecc said differently when he wrote this code:
c37aa26

However, I'll be delighted if that is old news and that modern Unity has fixed the bug. I'll make the change you suggest and hope that @neuecc agrees.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@AArnott
Apply PR review suggestions
e86c37f
AArnott
AArnott commented May 24, 2024
View reviewed changes
Comment thread src/MessagePack/MessagePackSerializer.Json.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

pCYSl5EDgo
pCYSl5EDgo reviewed May 25, 2024
View reviewed changes
Comment thread src/MessagePack/MessagePackSerializer.NonGeneric.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

pCYSl5EDgo
pCYSl5EDgo reviewed May 25, 2024
View reviewed changes
Comment thread src/MessagePack/SafeBitConverter.cs

@pCYSl5EDgo pCYSl5EDgo May 25, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I previously reffered to as Unsafe.ReadUnaligned was actually T Unsafe.ReadUnaligned<T> (ref byte source).
fixed statement and its performance penalty can be avoided.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AArnott AArnott May 26, 2024

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadUnaligned<T>(ref T source) requires a ref, sadly. But ReadOnlySpan<T>.this[int] only returns a ref readonly.
I can't figure out a way to get it to compile. This is my best attempt:

ref byte p = ref value[0];
return Unsafe.ReadUnaligned<ushort>(ref p);

But it produces:

Cannot use property 'this' as a ref or out value because it is a readonly variable

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AArnott AArnott May 26, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a modification of mine that still uses pointers produces the fewest native instructions.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AArnott AArnott May 26, 2024

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found how (on .NET 8 only) to avoid pointers. But it still produces one more native instruction than the pointer version.
see here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pCYSl5EDgo pCYSl5EDgo May 26, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better.

ref T Unsafe.AsRef<T>(in T) can be used and also treated specially in IL2CPP.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AArnott reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@pCYSl5EDgo pCYSl5EDgo May 26, 2024

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template<typename T>
inline T* il2cpp_unsafe_as_ref(void* source)
{
    return reinterpret_cast<T*>(source);
}

Oh, Unity 2022.3.30f1 does not convert ref T Unsafe.AsRef<T>(in T) into il2cpp_unsafe_as_ref.
They just prepare the above api.

The below Update method is transpiled to C++ by IL2CPP like this.

void Update()
{
    ref byte src = ref Unsafe.AsRef(in MemoryMarshal.GetReference(Something));
    
    Vector3 velocity = new Vector3(Unsafe.ReadUnaligned<ushort>(ref src), Unsafe.ReadUnaligned<ushort>(ref Unsafe.Add(ref src, 2)), Unsafe.ReadUnaligned<ushort>(ref Unsafe.Add(ref src, 4)));

    transform.position += velocity * Time.deltaTime;
}
IL2CPP_EXTERN_C IL2CPP_METHOD_ATTR void TestForConst_Update_mAECC56A194C2E9D162EAFFB3B9F23B0B4EBCC9F7 (TestForConst_tDA600D04A981E1B53CD873D2147332DB5AA8480E* __this, const RuntimeMethod* method) 
{
	static bool s_Il2CppMethodInitialized;
	if (!s_Il2CppMethodInitialized)
	{
		il2cpp_codegen_initialize_runtime_metadata((uintptr_t*)&MemoryMarshal_GetReference_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_m9D86D3A7A7F6A344D16464E6638E2BEAD3F4BC90_RuntimeMethod_var);
		il2cpp_codegen_initialize_runtime_metadata((uintptr_t*)&Unsafe_AsRef_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_m3528C0EF81F905FBD26961BD8FD2BD02F40F8789_RuntimeMethod_var);
		s_Il2CppMethodInitialized = true;
	}
	uint8_t* V_0 = NULL;
	Vector3_t24C512C7B96BBABAD472002D0BA2BDA40A5A80B2 V_1;
	memset((&V_1), 0, sizeof(V_1));
	{
		ReadOnlySpan_1_tA850A6C0E88ABBA37646A078ACBC24D6D5FD9B4D L_0;
		L_0 = TestForConst_get_Something_m668E94568DBCA757DA0100F4555A919CC39E0B41(NULL);
		uint8_t* L_1;
		L_1 = MemoryMarshal_GetReference_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_m9D86D3A7A7F6A344D16464E6638E2BEAD3F4BC90(L_0, MemoryMarshal_GetReference_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_m9D86D3A7A7F6A344D16464E6638E2BEAD3F4BC90_RuntimeMethod_var);
		uint8_t* L_2;
		L_2 = Unsafe_AsRef_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_m3528C0EF81F905FBD26961BD8FD2BD02F40F8789_inline(L_1, Unsafe_AsRef_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_m3528C0EF81F905FBD26961BD8FD2BD02F40F8789_RuntimeMethod_var);
		V_0 = L_2;
		uint8_t* L_3 = V_0;
		uint16_t L_4;
		L_4 = il2cpp_unsafe_read_unaligned<uint16_t>(L_3);
		uint8_t* L_5 = V_0;
		uint8_t* L_6;
		L_6 = il2cpp_unsafe_add<uint8_t,int32_t>(L_5, 2);
		uint16_t L_7;
		L_7 = il2cpp_unsafe_read_unaligned<uint16_t>(L_6);
		uint8_t* L_8 = V_0;
		uint8_t* L_9;
		L_9 = il2cpp_unsafe_add<uint8_t,int32_t>(L_8, 4);
		uint16_t L_10;
		L_10 = il2cpp_unsafe_read_unaligned<uint16_t>(L_9);
		Vector3__ctor_m376936E6B999EF1ECBE57D990A386303E2283DE0_inline((&V_1), ((float)L_4), ((float)L_7), ((float)L_10), NULL);
		Transform_tB27202C6F4E36D225EE28A13E4D662BF99785DB1* L_11;
		L_11 = Component_get_transform_m2919A1D81931E6932C7F06D4C2F0AB8DDA9A5371(__this, NULL);
		Transform_tB27202C6F4E36D225EE28A13E4D662BF99785DB1* L_12 = L_11;
		NullCheck(L_12);
		Vector3_t24C512C7B96BBABAD472002D0BA2BDA40A5A80B2 L_13;
		L_13 = Transform_get_position_m69CD5FA214FDAE7BB701552943674846C220FDE1(L_12, NULL);
		Vector3_t24C512C7B96BBABAD472002D0BA2BDA40A5A80B2 L_14 = V_1;
		float L_15;
		L_15 = Time_get_deltaTime_mC3195000401F0FD167DD2F948FD2BC58330D0865(NULL);
		Vector3_t24C512C7B96BBABAD472002D0BA2BDA40A5A80B2 L_16;
		L_16 = Vector3_op_Multiply_m87BA7C578F96C8E49BB07088DAAC4649F83B0353_inline(L_14, L_15, NULL);
		Vector3_t24C512C7B96BBABAD472002D0BA2BDA40A5A80B2 L_17;
		L_17 = Vector3_op_Addition_m78C0EC70CB66E8DCAC225743D82B268DAEE92067_inline(L_13, L_16, NULL);
		NullCheck(L_12);
		Transform_set_position_mA1A817124BB41B685043DED2A9BA48CDF37C4156(L_12, L_17, NULL);
		return;
	}
}

inline uint8_t* Unsafe_AsRef_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_m3528C0EF81F905FBD26961BD8FD2BD02F40F8789_inline (uint8_t* ___0_source, const RuntimeMethod* method)
{
	return ((  uint8_t* (*) (uint8_t*, const RuntimeMethod*))Unsafe_AsRef_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_m3528C0EF81F905FBD26961BD8FD2BD02F40F8789_gshared_inline)(___0_source, method);
}

IL2CPP_MANAGED_FORCE_INLINE IL2CPP_METHOD_ATTR uint8_t* Unsafe_AsRef_TisByte_t94D9231AC217BE4D2E004C4CD32DF6D099EA41A3_m3528C0EF81F905FBD26961BD8FD2BD02F40F8789_gshared_inline (uint8_t* ___0_source, const RuntimeMethod* method) 
{
	{
		uint8_t* L_0 = ___0_source;
		return L_0;
	}
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@AArnott AArnott May 26, 2024

Copy link
Copy Markdown
Collaborator Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The x64 instructions for 32 and 16 bit reads with your proposed code is impressively small. 64 bit reads still require several more instructions, interestingly. I've made the change.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
AArnott added 2 commits May 25, 2024 17:23
@AArnott
Reduce native instructions, eliminate the unity options class
61247d5
@AArnott
Even fewer native instructions (for 16 and 32 bit reads)
c3efcba
AlanLiu90
AlanLiu90 reviewed May 27, 2024
View reviewed changes
Comment thread src/MessagePack/MessagePackWriter.cs Resolved

Uh oh!

There was an error while loading. Please reload this page.

@AArnott
Remove mono-specific Buffer.MemoryCopy alternative
1d026c3
@AArnott AArnott changed the title Proposal for how to eliminate #if related to unity Eliminate #if regions related to unity May 28, 2024
@AArnott

AArnott commented May 28, 2024

Copy link
Copy Markdown
Collaborator Author

I'm thinking with @pCYSl5EDgo and @AlanLiu90's feedback that this might be ready to merge. @neuecc do you have time to review?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@AArnott
AArnott merged commit cc1fdef into new-structure Jun 3, 2024
@AArnott
AArnott deleted the ifdefproposal branch June 3, 2024 12:32
@AlanLiu90 AlanLiu90 mentioned this pull request Jun 10, 2024
Use MemoryMarshal.Cast<byte, _> on 64-bit Android #1606
Closed
@dependabot dependabot Bot mentioned this pull request Jul 21, 2025
Bump BenchmarkDotNet and 18 others haga-rak/fluxzy.core#439
Closed
This was referenced Aug 4, 2025
Bump Amazon.Lambda.AspNetCoreServer and 31 others exceptionless/Exceptionless.Net#341
Closed
Bump Akka and 8 others akkadotnet/Akka.Persistence.Redis#425
Open
Bump MessagePack from 2.5.187 to 3.1.4 akkadotnet/Akka.Persistence.Redis#429
Open
@dependabot dependabot Bot mentioned this pull request Sep 19, 2025
Bump MessagePack from 2.6.100-alpha to 3.0.300 Revolutionary-Games/Arch.Extended#1
Closed
This was referenced Nov 3, 2025
⬆️ Bump MessagePack from 2.5.187 to 3.1.4 eugenecp/setlist-studio#74
Merged
deps: Bump MessagePack and MessagePack.Annotations msbolton/Conduit#10
Open
deps: Bump MessagePack.Annotations from 2.5.187 to 3.1.4 msbolton/Conduit#11
Open
@dependabot dependabot Bot mentioned this pull request Nov 10, 2025
Bump MessagePack from 2.5.198 to 3.1.4 lucaspimentel/MockTraceAgent#3
Closed
This was referenced Dec 2, 2025
Bump MessagePack from 2.3.85 to 3.1.4 dbolin/Apex.Serialization#262
Closed
Bump MessagePack and MessagePack.ImmutableCollection dbolin/Apex.Serialization#263
Open
This was referenced Nov 9, 2026
Bump MessagePack from 2.5.303 to 3.1.12 Franchef/Pifeon#5
Merged
Bump AWSSDK.S3 and 16 others PauloVianaR/ViverAppWeb#25
Closed
Bump the dotnet-dependencies group with 17 updates PauloVianaR/ViverAppWeb#26
Closed
Bump the nuget-major group with 9 updates Tjeerd-menno/nats-manager-ui#380
Closed
chore: Bump the nuget-updates group with 2 updates w1ck3ds0d4/GrainWallet#1
Closed
Bump the nuget group with 17 updates NikiforovAll/adaptive-rate-limiting-dotnet#16
Closed
Bump the nuget group with 1 update TanmayS0406/SSMSClaude-Script#14
Closed
deps: Bump MessagePack from 2.5.307 to 3.1.12 RMBarros-Dev/Hydra-Digital-Shop#14
Closed
Bump Avalonia and 11 others jeremychn/amarre#8
Closed
Bump the dotnet-major group with 8 updates chelomama-dev/finances-hub#32
Open
deps: Bump MessagePack from 2.5.307 to 3.1.12 erikdarlingdata/PerformanceStudio#715
Closed
This was referenced Nov 16, 2026
Bump AWSSDK.S3 and 16 others PauloVianaR/ViverAppWeb#27
Closed
Bump the dotnet-dependencies group with 17 updates PauloVianaR/ViverAppWeb#28
Open
Bump MessagePack from 2.5.307 to 3.1.12 afernandes/DiscoveryDeviceAndroid#5
Open
Bump MessagePack from 2.5.307 to 3.1.12 ErikEJ/SqlServer.Rules#820
Closed
Bump MessagePack from 2.5.307 to 3.1.12 thekim1/kalla#837
Closed
Bump the nuget-major group with 9 updates Tjeerd-menno/nats-manager-ui#386
Closed
Bump MessagePack from 2.5.307 to 3.1.12 jonnymuir/Heliograph#34
Closed
Bump AWSSDK.S3 and 15 others PauloVianaR/ViverAppWeb#30
Open
Bump the nuget-shipped group with 2 updates alexFinn97/scaffolder-for-optimizely#8
Closed
Bump the nuget group with 17 updates NikiforovAll/adaptive-rate-limiting-dotnet#17
Closed
Bump MessagePack from 2.5.307 to 3.1.12 Tjeerd-menno/nats-manager-ui#398
Merged
Bump the nuget group with 3 updates TanmayS0406/SSMSClaude-Script#17
Open
Bump MessagePack and 7 others chelomama-dev/finances-hub#34
Open
This was referenced Nov 23, 2026
Bump AWSSDK.S3 and 15 others PauloVianaR/ViverAppWeb#32
Closed
Bump the dotnet-dependencies group with 16 updates PauloVianaR/ViverAppWeb#33
Open
Bump MessagePack from 2.5.303 to 3.1.12 mortenbrudvik/Obfy#43
Closed
deps: Bump the "weekly" group with 1 update across multiple ecosystems ftichy/MiniShop#192
Merged
Bump MessagePack from 2.5.303 to 3.1.12 D-WIS/DWIS-Blackboard-Tests#45
Open
Bump the nuget group with 17 updates NikiforovAll/adaptive-rate-limiting-dotnet#18
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@neuecc neuecc Awaiting requested review from neuecc
2 more reviewers
@AlanLiu90 AlanLiu90 AlanLiu90 left review comments
@pCYSl5EDgo pCYSl5EDgo pCYSl5EDgo left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

v3.0

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@AArnott @AlanLiu90 @pCYSl5EDgo
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.