Skip to content

Lots of optimization. - #52

Merged
franciscojunior merged 12 commits into
npgsql:masterfrom
glenebob:text_backend_encoding_ascii
Sep 21, 2013
Merged

franciscojunior merged 12 commits into
npgsql:masterfrom
glenebob:text_backend_encoding_ascii

Conversation

@glenebob

Copy link
Copy Markdown
Contributor

Francisco,

Here is where I am currently at with Npgsql. This branch is a fork and continuation of PR 50, so if you merge this, you will get everything in 50 as well. If you merge 50, I will need to marge on my end and send up a new PR for these changes.

This last commit was inspired by Rungee's work on bytea text decoding.

The text backend type codecs now deal with byte[] to and from PG, not string. This allows a potentially huge memory savings in things like bytea, since it can be encoded directly to/from byte[], rather than using strings (which are UTF16) and then UTF8 encoding/decoding. Much much more efficient now. Text array encoding is also now efficient in the same way.

In order to keep that new efficiency, I also had to re-write the query command text generator, writing directly to byte[]. That allows text parameter values to be concatenated without first UTF8 decoding them (and then later UTF8 re-encoding).

The only text encoded data that actually uses anything above the ASCII character range is text itself. In theory, all encodings besides raw text could go directly to/from byte[]. However, I put together an Int2ASCII() function in test (writing directly to byte[]), and no matter how I optimized it, I could not get it much faster than UTF8Encoding.GetBytes(Int32.ToString()), and since the space savings on an individual Int32 is negligible, I left it out.

Some of the optimizations from PR 40 (bind optimizations) are also in here. See my latest comment on that PR.

Note that I did not optimize the bytea text decoders. Rungee did a great job with that. Instead, after this all gets merged, I plan to modify Rungee's optimizations to read directly from the byte[] data from PG, which will make it even more efficient. In the mean time, Rungee's implementation will need to use a UTF8Encoding.GetString() to get the string data it currently needs. Switching it to byte[] will be very simple for me to do.

There's more work to do on this. Bit string I think could be rewritten for use directly on the byte[]. I also noticed a huge opportunity to optimize NpgsqlCommand.GetParseCommandText(). But I kind of want to slow down and get synced back up with your master branch before doing any more work. This PR has gotten really massive.

Welcome back, and happy testing!

-Glen

Add support for binary array backend encoding.
Move OID type mapping from Connector to ConverterOptions, defaulting to
null.
Add supporting util functions, some of which are intended for use by future binary backend codecs.
Change version on default oid type mapping to somethimg safer.
Revert recent changes to EF code in favor of a now defaulted parameter,
since EF doesn't know anything about it.
Support arbitrary lower bounds in binary arrays.
Evidentally, the null map flag isn't used at all in the frontend/backend binary array representation.
Don't use PGUtil.WriteBytes(), because it appends a 0-byte.
Added tests to beat up binary integer array handling.
Refactored out some duplicated test code.
Properly double-quote and escape array elements in extended query text array formatter.
Add binary backend codecs for text, float and double.
Add relevent tests.
Demote oid to 32 bits, per its actual size in PG.
Move the UTF8Encoding.GetBytes()/GetString() into the types converters.
Optimize some of the type converters, such as ByteArrayToByteaText*(), to avoid the UTF16 encoded Strings; memory used to text encode bytea is now cut in half.
Optimize Bind, Parse, Execute, Describe by eliminating calls to UTF8Encoding.GetByteCount().
Queries and text arrays are now built directly to byte[] (MemoryStream), since text parameters are now all returned as UTF8 encoded byte[].
Remove numerous instantiations of UTF8Encoding in favor of one global instantiation.
Added an enum of commonly used ASCII characters (as byte) to avoid repeatedly running the encoding on their char counterparts.
Added some commonly used ASCII strings (as byte[]).
PGUtil.Write*() functions are now extensions to Stream, and follow the fluent pattern.
Many updates to use the new pattern.
Also some reformatting.
@franciscojunior

Copy link
Copy Markdown
Member

Hi, @glenebob !
Thank you for your compliment. My vacation was very nice!

What a huge PR :) Excellent!

I also think that using byte[] is much better to avoid all the conversion from text.

Rungee's work on #51 is also very nice and will be a great contribution.

I'll review your PR now and add my comments.
Again, well done, @glenebob !

Optimize NpgsqlCommand.GetParseCommandText(), minus the part that subs parameters (see TODO note).
@glenebob

Copy link
Copy Markdown
Contributor Author

I know I said I wanted to slow down, but, well, I'm obsessed apparently.

Added PGUtil.WriteLimBytes().
Fluentize the WriteLim*Functions.
StartupPacket optimized, fluentized, and reformatted.
More fluentization to FastPath.
@franciscojunior

Copy link
Copy Markdown
Member

I have some minor observations about this pr:

On NpgsqlBind, you created members to hold the byte values, but kept the string versions. Is this intended? https://github.com/franciscojunior/Npgsql2/pull/52/files#L3R41 I mean, as you already have the byte version of the values, I think there wouldn't need to keep the string version. I think the same happens in the NpgsqlParse class?

In NpgsqlCommand.GetParseCommandText, I think it would be better to get another name for the tText variable. Although there is the comment explaining what it is, I think it is a lot confusing. Even more if you consider there is also a parseCommand variable which also can be a possible return value! In the original version there was only the parseCommand variable whose name was self-explainable.

About this line: https://github.com/franciscojunior/Npgsql2/pull/52/files#L16R66 in NpgsqlQuery, I think we should still keep it in mind in case of regressions. I remember we received a lot of complains in the past because the user was getting problems caused by the network buffer not being empty before Npgsql started to write on it. I couldn't figure out where the problem was, but we need to check for possible problems when we remove this line.

At this line: https://github.com/franciscojunior/Npgsql2/pull/52/files#L29R50, I think the variable name for the minus infinity could be something like: MINUSINFINITY or even MINUS_INFINITY. ?

I also think the variable name of https://github.com/franciscojunior/Npgsql2/pull/52/files#L29R45 could be named BYTE_0 and BYTE_1?

I think that everything else is ok.

Excellent job, @glenebob . I think those changes create a foundation which we can rely on to create other optimizations. :)

@glenebob

Copy link
Copy Markdown
Contributor Author

On Fri, Sep 20, 2013 at 11:31 AM, Francisco Figueiredo Jr. <
[email protected]> wrote:

I have some minor observations about this pr:

On NpgsqlBind, you created members to hold the byte values, but kept the
string versions. Is this intended?
https://github.com/franciscojunior/Npgsql2/pull/52/files#L3R41 I mean, as
you already have the byte version of the values, I think there wouldn't
need to keep the string version. I think the same happens in the
NpgsqlParse class?

On NpgsqlBind, there are properties to get the values if those strings, so
the strings are still needed.

On NpgsqlParse, you're right, the strings can be tossed without effect.
Same with NpgsqlDescribe.

In NpgsqlCommand.GetParseCommandText, I think it would be better to get
another name for the tText variable. Although there is the comment
explaining what it is, I think it is a lot confusing. Even more if you
consider there is also a parseCommand variable which also can be a possible
return value! In the original version there was only the parseCommand
variable whose name was self-explainable.

I'm not real happy with the way that function came out, It should now be
much more efficient than it was, but it's code paths are confusing. But,
at the least, I'll try to make it less confusing until a better code path
jumps out at me.

I started to attack GetPrepareCommandText(), but it got even more
confusing, so I left it. I understood the code well enough to optimize out
some obvious string handling issues, but not really well enough for a top
-bottom rewrite.

Actually, I really want to get the parameter substitution logic down to one
scan of the query string. Once I get that written, both functions will
naturally look much different.

About this line:

https://github.com/franciscojunior/Npgsql2/pull/52/files#L16R66 in
NpgsqlQuery, I think we should still keep it in mind in case of
regressions. I remember we received a lot of complains in the past because
the user was getting problems caused by the network buffer not being empty
before Npgsql started to write on it. I couldn't figure out where the
problem was, but we need to check for possible problems when we remove this
line.

Do you know of any bug reports I can look at?

If it's still a problem, maybe I can figure it out. If not, maybe I can
prove it. We should be able to devise a test to make sure it doesn't
[re-]emerge.

In the mean time, I'll un-comment the Flush().

At this line:
https://github.com/franciscojunior/Npgsql2/pull/52/files#L29R50, I think
the variable name for the minus infinity could be something like:
MINUSINFINITY or even MINUS_INFINITY. ?

How about NEG_INFINITY, or INFINITY_NEG? :)

I also think the variable name of
https://github.com/franciscojunior/Npgsql2/pull/52/files#L29R45 could be
named BYTE_0 and BYTE_1?

Sure. Looking at it again, those labels are much more readable and
consistent with the others.

I think that everything else is ok.

Excellent job, @glenebob https://github.com/glenebob . I think those
changes create a foundation which we can rely on to create other
optimizations. :)

Excellent! I'll have a commit pushed up shortly :)

-Glen

@franciscojunior

Copy link
Copy Markdown
Member

Nice catch about NpgsqlBind. Git diff tricked me :)

About NpgsqlCommand.GetParseCommandText, it is really confusing. Please, try getting it at a better shape. It would be very welcome! :)
Getting the parameter substitution to one query scan would be awesome!

Regarding the Flush() call, I don't remember if there was a bug report about it. Or if it was a forum message. I'll look for it and post a link to here.

NEG_INFINITY is ok.

I'm looking forward your new commits :)

Removed stored strings that weren't being used.
Un-comment outputStream.Flush() in NpgsqlQuery, as the problem is not well understaood.
Changed some labels to be a bit more readable.
franciscojunior added a commit that referenced this pull request Sep 21, 2013
@franciscojunior
franciscojunior merged commit 46cae97 into npgsql:master Sep 21, 2013
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