I want to make sure my understanding about how c-objects are handled in 4D
memory. Mainly because it seems this understanding is cobbled together from
a number of different sources few of which I can easily find again. So...

1) c-objects are created within the 3 4D scopes: IP, process or local.

2) but they share some characteristics with hLists and menuRefs in that
once created they can be referred to by different instances. For example,
if I create an process object called "prosObj" as:

{ obj1: {name: Kirk, ID: 1234},

   obj2: {name: Joe, ID: 5678}}


I can create a local obj as:

$obj:=OB GET(prosObj;"obj1")

In this case $obj can be thought of as a pointer to prosObj.obj1 because if
I change the values in $obj these changes will also be made in ProsObj. So

OB SET($obj;"name";"Xavier")

​and then look at prosObj I'll see:

{ obj1: {name: Xavier, ID: 1234},

   obj2: {name: Joe, ID: 5678}}

​

3) My first point of clarification is that $obj is simply referencing
prosObj so it's not significantly increasing the amount of memory used.

4) Conversely if I created $obj using the copy command it would. So

$obj:=OB COPY(OB GET(prosObj;obj1))

would create a full copy of obj1 and take up more memory.

5) This referencing feature also applies to objects in arrays. It's easy to
discover this inadvertently:

ARRAY OBJECT($aObjs;0)

OB SET($obj;key;"new value")

APPEND TO ARRAY($aObjs;$obj)
APPEND TO ARRAY($aObjs;$obj)
APPEND TO ARRAY($aObjs;$obj)

$aObjs has 3 element each containing: {key;"new Value"}.

If I modify $obj:

OB SET($obj;key;"defined value")

​each element of $aObjs is now ​
 {key;"new Value"}
​:​


​Again, I'm assuming $obj only exists in memory one time with each element
of $aObjs referring to it.

6) I can clear $obj without erasing the referenced values by calling:

$obj:=JSON PARSE("{}")

7) ​And unlike menus and hLists ​the memory occupied by $obj is released
when the process end.
​

8) Passing objects as parameters is essentially equivalent to passing
pointers to c-obj vars EXCEPT when working with a method called with
Execute on server - object references don't propagate when moving between
the twin and local process. So let's say myMethod
does this:

 OB
​ ​
SET($1;"name";"Kirk")


I can execute this method locally myMethod($obj) and $obj = {"name";"Kirk"}

If I set the EOS parameter (and run it on the server) no errors are thrown
but the client side version of $obj doesn't change.

​If I change myMethod to take a pointer;

​OB
 ​
SET($1->;"name";"Kirk")

​then myMethod will work the same way on the client or EOS. However I
suspect the time to dereference a pointer adds some time on the client side
that wouldn't be required by simply accessing the object directly. ​


For these examples the memory use is trivial, of course. But as I coming to
appreciate how great c-objects are for managing a lot of data in memory I
can see a time when I might be working with significant memory allocations
where these things can really matter.


-- 
Kirk Brooks
San Francisco, CA
=======================

*The only thing necessary for the triumph of evil is for good men to do
nothing.*

*- Edmund Burke*
**********************************************************************
4D Internet Users Group (4D iNUG)
FAQ:  http://lists.4d.com/faqnug.html
Archive:  http://lists.4d.com/archives.html
Options: http://lists.4d.com/mailman/options/4d_tech
Unsub:  mailto:[email protected]
**********************************************************************

Reply via email to