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

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

  • + 2
  • 0
  • 166
  • 8
  • 3

Answers

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.

 

 

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.

 

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>

And if so?

w $zobjref(^||PPG(1)).Name

Still take a look at $$$objOrefToInt/$$$objIntToOref (%occObject.inc)

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

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

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

Comments

Both answers are helpful.
Wish I could accept both.

Temporary tables are slightly more important to me

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?

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