Skip to content

Create BinaryFormatter replacement for known types - #9088

Merged
JeremyKuhne merged 5 commits into
dotnet:mainfrom
JeremyKuhne:bfobject
May 11, 2023
Merged

JeremyKuhne merged 5 commits into
dotnet:mainfrom
JeremyKuhne:bfobject

Conversation

@JeremyKuhne

@JeremyKuhne JeremyKuhne commented May 8, 2023 •

Copy link
Copy Markdown
Member

In a number of cases we know what data we're serializing or deserializing and can avoid using the BinaryFormatter if we weren't concerned about compatibility.

I was considering using a different format on the wire and writing a parser to deal with legacy data. It finally occured to me that if I'm going to parse legacy data I might as well put the data out in the legacy format to maintain compatibility.

This creates a model for the NRBF format that can be used to look at data the BinaryFormatter puts out. The top level of the model is represented by the BinaryFormattedObject class.

The binary records that are part of the model all self serialize and deserialize, which allows manually constructing known models to export data that can be read by the BinaryFormatter. The records never hydrate any complex types other than strings, that must be done manually.

BinaryFormatReader and BinaryFormatWriter have helper methods for creating and reading a set of known types. I've handled primitive types and Hashtable and started on List as well.

Clipboard will no longer use the BinaryFormatter for primitive types. Tests have been updated appropriately.

My next step is to change the ActiveX property bag serialization to use the Hashtable serialization that this change introduces. I also have a few more tests to port from my prototype repo.

The idea is to then further reduce the need to fall back on the BinaryFormatter by providing support for other BinaryFormatted types in WinForms and other types that WinForms projects would commonly use as public property types. This includes things like PointF and RectangleF, primitive arrays, and other collections of primitive types (List and ArrayList are the priorities there).

Note that performance isn't far off from using the BinaryFormatter. For serializing a really large Hashtable (around 10K records) we are about the same speed and use a slightly larger amount of memory. It's possible it could be significantly better if we use the enumerator to get the data instead of ISerializable- would have to spend some time to figure out the right values for the other private fields without using reflection.

BinaryFormattedObject does not yet support multidimensional or jagged arrays. This doesn't hit any of our scenarios, will implement when needed.

This change also consumes FluentAssertions in the Primitives test assembly. We've discussed using this as a team and will be looking at it in use here before committing to the rest of the test projects.

Microsoft Reviewers: Open in CodeFlow

In a number of cases we know what data we're serializing or deserializing and can avoid using the BinaryFormatter if we weren't concerned about compatibility.

I was considering using a different format on the wire and writing a parser to deal with legacy data. It finally occured to me that if I'm going to parse legacy data I might as well put the data out in the legacy format to maintain compatibility.

This creates a model for the NRBF format that can be used to look at data the BinaryFormatter puts out. The top level of the model is represented by the BinaryFormattedObject class.

The binary records that are part of the model all self serialize and deserialize, which allows manually constructing known models to export data that can be read by the BinaryFormatter. The records never hydrate any complex types other than strings, that must be done manually.

BinaryFormatReader and BinaryFormatWriter have helper methods for creating and reading a set of known types. I've handled primitive types and Hashtable and started on List<T> as well.

Clipboard will no longer use the BinaryFormatter for primitive types. Tests have been updated appropriately.

My next step is to change the ActiveX property bag serialization to use the Hashtable serialization that this change introduces.  I also have a few more tests to port from my prototype repo.

The idea is to then further reduce the need to fall back on the BinaryFormatter by providing support for other BinaryFormatted types in WinForms and other types that WinForms projects would commonly use as public property types. This includes things like PointF and RectangleF, primitive arrays, and other collections of primitive types (List<T> and ArrayList are the priorities there).

BinaryFormattedObject does not yet support multidimensional or jagged arrays.
@JeremyKuhne
JeremyKuhne requested a review from a team as a code owner May 8, 2023 04:05
@ghost ghost assigned JeremyKuhne May 8, 2023
@JeremyKuhne

Copy link
Copy Markdown
Member Author

There are a little over 500 lines of executable code to implement this. I expect it will total 600 or so when the other items are implemented. I've tried to keep the maintainability index high (currently 84).

I've also leaned heavily on patterns to help avoid parsing mistakes. Usage of BinaryReader/Writer takes a fair amount of validation off of our hands. I've encapsulated counts and identifiers into structs that enforce positive numbers. I've also guarded when reading data from stream provided counts.

@JeremyKuhne

Copy link
Copy Markdown
Member Author

cc: @lonitra, @GrabYourPitchforks

@Tanya-Solyanik Tanya-Solyanik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I didn't finish, will get back to it later.

@dreddy-work dreddy-work left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

RLGTM.

@JeremyKuhne
JeremyKuhne merged commit b6da3f9 into dotnet:main May 11, 2023
@JeremyKuhne
JeremyKuhne deleted the bfobject branch May 11, 2023 00:53
@ghost ghost added this to the 8.0 Preview5 milestone May 11, 2023
"ticks", "dateData"
};

private const string Mscorlib = "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Had you tested copy/pasting between .NET Framework applications and Core applications? At some point it used to work. Perhaps a unit test to test it can be added?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not explicitly, but the BinaryFormatter uses the mscorlib versions (when it puts out a version). Everything I've done here is matching what it puts out.

@ghost ghost locked as resolved and limited conversation to collaborators Jun 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants