Skip to content

Conversation

@martinmine
Copy link

Today when adding a json parameter in queries, the serialized json document in bytes gets copied multiple times. One time in JsonHandler.SerializeJsonDocument when writing the byte array, which is then copied once the function returns. If this function gets called frequently with large documents, blocks will become allocated on LOH which is far from ideal in high performance critical application.

This change allows me to use strategies such as buffer pooling when building the JSON object, for example:

var document = JsonSerializer.SerializeToDocument(obj);
var documentBytes = SerializeToBytes(document);

... 

new NpgsqlParameter("document", NpgsqlDbType.Jsonb)
{
    Value = document,
    ConvertedValue = documentBytes
}

The function SerializeToBytes can then use for example a RecyclableMemoryStream to re-use the memory being used for sending the documents.

Related to the discussion here: #1727

@NinoFloris
Copy link
Member

ConvertedValue is no more after #5123.

I posted an update in #1727 a while back.
Effectively all the pieces are in place to add fully buffered writes, it just needs some wiring up to make it work.

As such I'll close the PR.

@NinoFloris NinoFloris closed this Oct 3, 2023
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