Discussion Hermann Tegermann · May 17, 2022

What's better: Local Variable Arrays or Process Private Globals

In various tests I used both and found no real reason to prefer the one or the other.
Eventually I missed some limits. At least I didn't hit any.
What is the general opinion?
Where to use the one or the other?

Comments

Bernardo Linarez · Jul 11, 2019

Hi !

In my experience, if you process a lot of nodes into your array, is better "Process Private Globals", because it can use disk resource after complete all the RAM resource asigned to the particular process, like a normal global. In the case of  "Local Variable Arrays" you only use RAM.

If you don't need many nodes and your nodes save a small data, is better "Local Arrays". But if you have a lot of nodes and each one save large data (like xml content or csv file ) is better  "Process Private Globals".

Best Regards.

0
Robert Cemper · Jul 11, 2019

OK !
They look rather similar.

BUT:

  • Process Private Globals are "unlimited". 
    Clear there is a physical limit, but it's far away compared to local variables.
  • PPGs are best suited as Storage location + Index location of temporary tables.
    Without limit as any ordinary Global. 

So it depends on your needs.

0
John Murray · Jul 12, 2019

Bear in mind that local variable arrays can hold orefs but PPGs cannot:


SAMPLES>w $zv
Cache for Windows (x86-32) 2017.2.2 (Build 865U) Mon Jun 25 2018 11:10:00 EDT
SAMPLES>
 
SAMPLES>s oP=##class(Sample.Person).%OpenId(1)
 
SAMPLES>w oP.Name
Gallant,Yan N.
SAMPLES>
 
SAMPLES>s LocalArray(1)=oP
 
SAMPLES>s ^||PPG(1)=oP
 
SAMPLES>w LocalArray(1).Name
Gallant,Yan N.
SAMPLES>w ^||PPG(1).Name
 
W ^||PPG(1).Name
^
<INVALID OREF>
SAMPLES>w $isobject(^||PPG(1))
0
SAMPLES>w ^||PPG(1)
1@Sample.Person
SAMPLES>
 
SAMPLES>w LocalArray(1)
1@Sample.Person
SAMPLES>w $isobject(LocalArray(1))
1
SAMPLES>
0
Vitaliy Serdtsev  Jul 12, 2019 to John Murray

And if so?

<FONT COLOR="#0000ff">w $zobjref</FONT><FONT COLOR="#000000">(^||PPG(1)).</FONT><FONT COLOR="#0000ff">Name</FONT>

Still take a look at <FONT COLOR="#0000ff">$$$objOrefToInt</FONT>/<FONT COLOR="#0000ff">$$$objIntToOref</FONT> (%occObject.inc)

PS: it should be noted that OREF ≠ OID and serve different purposes.

0
Eduard Lebedyuk  Jul 14, 2019 to Vitaliy Serdtsev

That would work only in the cases where original reference to OREF exists, right?

0
Steven Hobbs  May 18, 2022 to Eduard Lebedyuk

In particular,  an object is garbage collected when there are no ways to access that object using oref values starting from an oref-type value stored in a local variable.  When an oref is converted to an integer (or to a string) then that does not count as an object reference.  Store that integer/string in a PPG and then do KILLs (or otherwise make changes) of oref-type values referring to an object then that object will be deleted despite the fact a PPG (or any other variable) contains an integer/string reference to the object.

0
Robert Cemper  Jul 12, 2019 to John Murray

Excellent & valid comment !
I didn't think of this difference before.

0
Erin Dolson · Jul 12, 2019

Hi, I'm very new to the world of InterSystems, I'm an intern at a hospital who has taken 1.5 years of CS classes. I could be misunderstanding but this seems like a 'local variable vs global variable' argument specific to InterSystems. In my classes I have always been told to stay away from global variables and to use local variables. They say it keeps the program from getting to intertwined/ tightly coupled, and makes it easier to keep your data from being modified when you don't want it to be. It supposedly makes the code easier for everyone to understand and easier to fix bugs. I found it interesting that no one mentioned this when it is mentioned a lot in school. Is this something students are told early on in their education to practice good coding or is it just not a concern in this case?

0
Robert Cemper  Jul 12, 2019 to Erin Dolson

This is a rather common misunderstanding.
The naming GLOBAL was created in the 60ties.
Long before any other programming language even had anything named Global or similar.

GLOBAL in Caché is a persistent storage component that builds the backbone of (SQL) Tables and Persistent Objects.

see this documentation: Using Caché Globals

0
Erin Dolson  Jul 15, 2019 to Robert Cemper

Interesting, thank you!

0