#1 2022-07-22 13:31:15

igors233
Member
Registered: 2012-09-10
Posts: 241

TDocVariantData and storing item by reference

If we have a FAST_JSON DVD - TDocVariantData.InitFast, how to actually add Variants by reference to it?

AddItem will store sent value with SetVariantByValue, same for AddValue (I haven't tried aValueOwned in mORMot2, just using ver1 for now).

Last edited by igors233 (2022-07-22 13:31:41)

Offline

#2 2022-07-22 14:21:11

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,651
Website

Re: TDocVariantData and storing item by reference

The only option I see is to use SetCapacity, SetCount, then directly copy by reference to VValues[].

The fact that we force SetVariantByValue() is that copying by reference may trigger some GPF or leaks if not properly used, since there is no reference counting, just passing pointers.

What do you want to store varByRef variants?
The performance difference won't be noticeable - it may even be slower if it contains mostly simple types. And for RawUtf8 variant strings, they will use RawUtf8 reference counting, so are fast too.

Offline

#3 2022-07-22 15:49:27

igors233
Member
Registered: 2012-09-10
Posts: 241

Re: TDocVariantData and storing item by reference

> The fact that we force SetVariantByValue() is that copying by reference may trigger some GPF or leaks if not properly used, since there is no reference counting, just passing pointers.

I'm mostly storing larger json objects and they could be added to several other objects so storing by reference would be ideal to save memory and increase speed.

Sometimes it could also be used to easily update all objects (change one property and it changes to all).

Looking at code, is dvoValueCopiedByReference designed to handle json objects variants? For example using AddNameValuesToObject with json object, would dereference it anyway, create a copy?

Offline

#4 2022-07-22 16:00:06

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,651
Website

Re: TDocVariantData and storing item by reference

Values are already copied by reference from one TDocVariantData to another, if you use "fast" TDocVariantData, dvoValueCopiedByReference.
It means the VNames[] and VValues[] arrays are copied by reference, i.e. using reference counting.

Offline

Board footer

Powered by FluxBB