Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the need for unsafe code #20

Closed
wants to merge 3 commits into from
Closed

Conversation

AjaxGb
Copy link

@AjaxGb AjaxGb commented Mar 15, 2016

Unsafe code was only used in three places in the NbtBinaryWriter class: once each to cast floats to uint bits and doubles to ulong bits, and once to use this method. I have changed the casting to make use of struct member overlap, and there is an almost identical method that uses safe code.

These changes should make compiling easier (they did for me).

@mstefarov
Copy link
Owner

Looks good! I'll check for performance impact of these changes, and merge it if there are no problems.

@mstefarov
Copy link
Owner

Alright, I did some profiling. Changes to Write(float) and Write(double) don't cause any problems, but changes to Write(string) cause a 25% increase in CPU time, as well as doubling the amount of heap allocation. That added value.ToCharArray() call is pretty expensive.

Under-the-hood in the .NET Framework, EncoderNLS.GetBytes(Char[]...) simply proxies to EncoderNLS.GetBytes(Char*...) anyway. Is there really a good reason not to stick to the current, "unsafe" but efficient implementation of NbtBinaryWriter?

@mstefarov
Copy link
Owner

fNbt Issue 20 profiling results in dotTrace

Here are the profiling results by the way, before/after.

@AjaxGb
Copy link
Author

AjaxGb commented Mar 28, 2016

Nah, that sounds reasonable.

@NiclasOlofsson
Copy link

FYI, I just forked the project and had absolutely no problems getting it to compile.

@mstefarov mstefarov closed this May 17, 2024
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.

None yet

3 participants