Skip to content

Beautify T4 of mpc.exe and reduce local variables of the deserialization#1074

Closed
pCYSl5EDgo wants to merge 8 commits intoMessagePack-CSharp:masterfrom
pCYSl5EDgo:mpc-fix
Closed

Beautify T4 of mpc.exe and reduce local variables of the deserialization#1074
pCYSl5EDgo wants to merge 8 commits intoMessagePack-CSharp:masterfrom
pCYSl5EDgo:mpc-fix

Conversation

@pCYSl5EDgo
Copy link
Copy Markdown
Contributor

Refactor common codes between int/string key into function.
More strict namespace.

Add some logic to the string-key.
Optimize int-key when member array is empty.

I tried reducing the using System.Buffers; and using MessagePack; but I failed.

@pCYSl5EDgo
Copy link
Copy Markdown
Contributor Author

pCYSl5EDgo commented Oct 1, 2020

Added some optimizations.

During deserialization

  • If there is no members, just Skip().
  • If the constructor has no parameters, writes directly to the members.
    • This reduces unnecessary local variables and assignments.

@pCYSl5EDgo
Copy link
Copy Markdown
Contributor Author

I think about that braking change.
It only affects string-key types which has a parameter-less constructor and not default-initialized serializable and writable field/properties when the encoded byte sequence does not include those fields' string-key.
The impact range is limited.

If the object can be created before acutual deserialization,
the stack local variables can be removed.
@pCYSl5EDgo
Copy link
Copy Markdown
Contributor Author

Circle CI is unstable.
The errors are thrown during the environment setup process.

@pCYSl5EDgo pCYSl5EDgo changed the title Beautify T4 format of int-key formatter Beautify T4 of mpc.exe and reduce local variables of the deserialization Oct 17, 2020
@AArnott
Copy link
Copy Markdown
Collaborator

AArnott commented Oct 17, 2020

Circle CI is unstable.

We're not using Circle CI.

Current dynamic IL generating resolver emits the codes which do set members to their default values...
I have another PR to change dynamic
resolver's behaviour to reduce stack allocation.

Why is the stack allocation important?
We want to keep the AOT and dynamic resolver behavior equivalent. So the two changes should be merged together first and then submitted as one coherent PR so that AOT and dynamic resolver change together.

I think about that braking change.
It only affects string-key types which has a parameter-less constructor and not default-initialized serializable and writable field/properties when the encoded byte sequence does not include those fields' string-key.
The impact range is limited.

It's unclear to me what the value is though. We should only take breaking changes if their value significantly outweighs the risk of breaking folks, and I don't see reducing the stack size as significantly valuable. Are you aware of something that makes this valuable?

Copy link
Copy Markdown
Collaborator

@AArnott AArnott left a comment

Choose a reason for hiding this comment

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

Rejecting this till the breaking change is justified and the dynamic resolver change can be made at the same time, with tests.

@AArnott AArnott closed this Oct 17, 2020
AArnott added a commit to AArnott/MessagePack-CSharp that referenced this pull request Oct 17, 2020
This behavior may not be desirable, but the test documents current behavior.
See MessagePack-CSharp#1074 (comment)
AArnott added a commit to AArnott/MessagePack-CSharp that referenced this pull request Oct 18, 2020
This behavior may not be desirable, but the test documents current behavior.
See MessagePack-CSharp#1074 (comment)
@pCYSl5EDgo
Copy link
Copy Markdown
Contributor Author

pCYSl5EDgo commented Oct 18, 2020

@AArnott
I do not find the test C# file which examines the formatters generated by mpc.exe.
Where is it?
Should I create new csproject to test those formatters generated by mpc.exe?

I made a DynamicResolver change and test for it.
The undesirable behavior is fixed in all the 2^3 ==8 cases(Dynamic and mpc.exe, int-key and string-key, and class and struct).

It's unclear to me what the value is though. We should only take breaking changes if their value significantly outweighs the risk of breaking folks, and I don't see reducing the stack size as significantly valuable. Are you aware of something that makes this valuable?

It is valuable.
SkipLocalsInitAttribute will appear since .NET 5. .NET's PR
C# initializes local variables with the default value.
The programmers have paid the initialization cost twice.
IL2CPP zero-ing is inefficient. It initializes each of the local variables and a lot of function calls happen.

If the programmers can avoid the local variables, the programmers should. (It is a good act to write local variables to help them understand as far as C# compiler can remove the local variables)
This parameter-less constructor case is the best suitable case.

pCYSl5EDgo added a commit to pCYSl5EDgo/MessagePack-CSharp that referenced this pull request Oct 23, 2020
This is a refactoring of int-key mpc.exe tt file.
pCYSl5EDgo added a commit to pCYSl5EDgo/MessagePack-CSharp that referenced this pull request Oct 23, 2020
This is a refactoring of int-key mpc.exe tt file.
AArnott pushed a commit that referenced this pull request Oct 25, 2020
* This is a cherry-pick of #1074.
This is a refactoring of int-key mpc.exe tt file.

* Change the format to decrease template part lines.

Change the iterator `i` type from `int` to `var`.

* fix CRLF

* Change: var to globall::
Revert: DepthStep position
Revert: }#> -> } #>

* Fix: space

* Fix: Line Feed

* Fix: var -> global::MessagePack.IFormatterResolver
pCYSl5EDgo added a commit to pCYSl5EDgo/MessagePack-CSharp that referenced this pull request Oct 27, 2020
…rp#1092

commit d7a50b9
Merge: 02ae418 5a6cda6
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Mon Oct 26 09:33:45 2020 +0900

    Merge remote-tracking branch 'upstream/develop' into solution#1085-mpc

commit 02ae418
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Mon Oct 26 09:25:20 2020 +0900

    Update: Loop demonstration

commit d84ae75
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Mon Oct 26 09:24:31 2020 +0900

    Change: drop deserialized value -> reader.Skip()

commit 9b0d4a2
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Mon Oct 26 00:27:10 2020 +0900

    Fix: remove unused boolean local variables

commit fdfabeb
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Mon Oct 26 00:00:28 2020 +0900

    Update: Make the same as Dynamic

commit bef4692
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Sun Oct 25 19:37:11 2020 +0900

    Add test project

commit a8fac80
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Sun Oct 25 18:55:27 2020 +0900

    Add types to sandbox

commit 6ffc3c3
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Sun Oct 25 18:45:40 2020 +0900

    Update: string-key mpc.exe

commit 44819e6
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Sun Oct 25 18:05:24 2020 +0900

    Update: int-key mpc.exe

commit 0a2fef4
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Sun Oct 25 12:12:57 2020 +0900

    Fix: var -> global::MessagePack.IFormatterResolver

commit 086aa11
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Sun Oct 25 12:05:21 2020 +0900

    Fix: Line Feed

commit 39d3d08
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Sun Oct 25 12:03:37 2020 +0900

    Fix: space

commit c57f423
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Sun Oct 25 12:00:32 2020 +0900

    Change: var to globall::
    Revert: DepthStep position
    Revert: }#> -> } #>

commit de6fd23
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Sat Oct 24 11:04:44 2020 +0900

     fix CRLF

commit 4b6ae7c
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Sat Oct 24 10:59:50 2020 +0900

    Change the format to decrease template part lines.

    Change the iterator `i` type from `int` to `var`.

commit 95f41ad
Author: pCYSl5EDgo <pCYSl5EDgo@yahoo.co.jp>
Date:   Fri Oct 23 21:03:54 2020 +0900

    This is a cherry-pick of MessagePack-CSharp#1074.
    This is a refactoring of int-key mpc.exe tt file.
ColtonMori added a commit to ColtonMori/MessagePack that referenced this pull request Oct 21, 2022
This behavior may not be desirable, but the test documents current behavior.
See MessagePack-CSharp/MessagePack-CSharp#1074 (comment)
DotDeveloper95 added a commit to DotDeveloper95/MessagePack-CSharp that referenced this pull request Sep 4, 2023
This behavior may not be desirable, but the test documents current behavior.
See MessagePack-CSharp/MessagePack-CSharp#1074 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants