# System.util.globals Performance

**URL:** <https://forum.inductiveautomation.com/t/system-util-globals-performance/92142>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [June 20, 2024, 9:08am UTC](https://forum.inductiveautomation.com/t/system-util-globals-performance/92142 "2024-06-20T09:08:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![weixionglichina](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/weixionglichina/32/10650_2.png) [@weixionglichina](https://forum.inductiveautomation.com/u/weixionglichina)\
**Post date:** [June 20, 2024, 9:08am UTC](https://forum.inductiveautomation.com/t/system-util-globals-performance/92142/1 "2024-06-20T09:08:15Z")

</div>

Dear All,

I am trying to use system.util.globals as in-memory cache provider because it takes time to get data from Database, the issue I found that it's even running slower than without system.util.globals support.

```python
#cacheProvider = {}
cacheProvider = system.util.globals

def getCache():
	if not GLOBAL_CACHE_ENABLED:
		return None
		
	if cacheProvider.get(CACHE_ROOT_KEY) is None:
		cacheProvider[CACHE_ROOT_KEY] = {}
	
	return cacheProvider.get(CACHE_ROOT_KEY)

```

Any recommendations what we could use it properly? or any other in-memory cache alternative providers?

Below is description of getGlobals()  
[system.util.getGlobals | Ignition User Manual (inductiveautomation.com)](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-util/system-util-getGlobals)

## Description[​](https://www.docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-util/system-util-getGlobals#description)

This method returns a dictionary that provides access to the legacy global namespace. As of version 7.7.0, most new scripts use the modern style of scoping, which makes the 'global' keyword act very differently. Most importantly, the modern scoping rules mean that variables declared as 'global' are only global within that one module. The system.util.getGlobals() method can be used to interact with older scripts that used the old meaning of the 'global' keyword.

New in 8.1.0

The globals dictionary now persists across the lifetime of the JVM, and is accessible at system.util.globals. This means system.util.globals will work as an alternative to system.util.getGlobals. However, system.util.getGlobals is still the only option that will appear in the Designer's autocomplete feature.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 20, 2024, 10:52am UTC](https://forum.inductiveautomation.com/t/system-util-globals-performance/92142/2 "2024-06-20T10:52:41Z")

</div>

Just having that function `getCache` will reduce performance.

I recommend restructuring like so:

```python
myCache = system.util.getGlobals().setdefault(CACHE_ROOT_KEY, {})

```

{ Jython's `.setdefault` dictionary method is atomic--that is, it is race-free. }

Then wherever you were using `getCache()` or `someScript.getCache()`, use `myCache` or `someScript.myCache`.

Do be aware that such a cache is itself a memory leak if you have no mechanism to prune old entries, and the resulting memory pressure can make all of Ignition slow.

(Because of the ties to the legacy scripting namespace, I created a more functional and more isolated alternative as [system.util.globalVarMap()](https://www.automation-pros.com/toolkit/doc/scripting.html#globalvarmap).)

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [June 20, 2024, 12:30pm UTC](https://forum.inductiveautomation.com/t/system-util-globals-performance/92142/3 "2024-06-20T12:30:40Z")

</div>

> [@pturmel](#):
>
> Do be aware that such a cache is itself a memory leak if you have no mechanism to prune old entries

When you say old entries, I assume you mean just removing keys from the globals dict if they're no longer needed?

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 20, 2024, 1:12pm UTC](https://forum.inductiveautomation.com/t/system-util-globals-performance/92142/4 "2024-06-20T13:12:54Z")

</div>

> [@nminchin](#):
>
> no longer needed?

That, and forcibly pruning least recently used when memory gets tight.

---

<div class="post-metadata">

**Author:** ![weixionglichina](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/weixionglichina/32/10650_2.png) [@weixionglichina](https://forum.inductiveautomation.com/u/weixionglichina)\
**Post date:** [June 20, 2024, 1:19pm UTC](https://forum.inductiveautomation.com/t/system-util-globals-performance/92142/5 "2024-06-20T13:19:17Z")

</div>

We could remove those ones manually, performance is the key for us, no enhancement made even I use  
`myCache = system.util.getGlobals().setdefault(CACHE_ROOT_KEY, {})`

Any further help would be appreciated.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [June 20, 2024, 1:40pm UTC](https://forum.inductiveautomation.com/t/system-util-globals-performance/92142/6 "2024-06-20T13:40:00Z")

</div>

You'll probably have to share your entire project here to get free help. If you want to pay for formal assistance, contact me privately next week.

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [June 20, 2024, 2:08pm UTC](https://forum.inductiveautomation.com/t/system-util-globals-performance/92142/7 "2024-06-20T14:08:35Z")

</div>

Are you sure that it's "get globals" that is causing your performance issues? What else is in the chain? Where does this poor performance come into play?
