Progress Bar in a Power Table, client and Designer become too slow when script is executed

Hello to everyone, I have code in a power table to add a progress bar to some columns of the table. The code is set in the extension function (Configure Cell). The code is doing what it's supposed to do, but the problem is that when the code is enabled, the designer and client become too slow when the window containing the power table is open.

I'm working with a trial license. I appreciate any help. Thanks

This is the code

def configureCell(self, value, textValue, selected, rowIndex, colIndex, colName, rowView, colView):
	from javax.swing import JProgressBar    
	from java.awt import Color
	from javax.swing.border import LineBorder
	
	if colName=="kWh": 
		y=int(system.dataset.toPyDataSet(system.tag.read("[tp_TurbinePlant]La Loma Substation/Energy Hour Target/Target_PowerTable/Target_1_12").value)[rowIndex][0])
		a = str(value)
		ratio = (value/y)*100 if y != 0 else 0
		if ratio < 100:
			foreColor = Color(255,255,204,255)
		elif ratio >= 100:
			foreColor = Color(204,255,204,255)
		bar = JProgressBar(0, y, stringPainted=True, string = a)
		bar.setValue(value)
		bar.background = Color.WHITE
		bar.setForeground(foreColor)
		return {'renderer': bar}
	elif colName=="kWh_":
		y=int(system.dataset.toPyDataSet(system.tag.read("[tp_TurbinePlant]La Loma Substation/Energy Hour Target/Target_PowerTable/Target_13_24").value)[rowIndex][0])
		a = str(value)
		ratio = (value/y)*100 if y != 0 else 0
		if ratio < 100:
			foreColor = Color(255,255,204,255)
		elif ratio >= 100:
			foreColor = Color(204,255,204,255)
		bar = JProgressBar(0, y, stringPainted=True, string = a)
		bar.setValue(value)
		bar.background = Color.WHITE
		bar.setForeground(foreColor)
		return {'background': '#646464', 'foreground': '#FFFFFF', 'renderer': bar}
	elif colName =="Hr":
		X = system.tag.read("[tp_TurbinePlant]La Loma Substation/Energy Hour Target/Grid_MWH_Index").value
		if value == X:
			return {'border': LineBorder(system.gui.color(128,128,128), 2)}
	elif colName =="Hr_":
		X1 = system.tag.read("[tp_TurbinePlant]La Loma Substation/Energy Hour Target/Grid_MWH_Index").value
		if value == X1:
			return {'border': LineBorder(system.gui.color(128,128,128), 2)}

You're reading a tag in the configureCell function. configureCell gets called constantly whenever the table needs to repaint a cell, and these tag reads are being done synchronously on the main GUI thread, which is what leads to slowdown.

To avoid the performance hit, create new custom properties on the table for the tags you want to read. Give them tag bindings - this will automatically set up subscription infrastructure so that these tags are read asynchronously. Then change your configureCell script to refer to these custom properties, which will be more or less instant.

3 Likes

Note: That will solve the most significant performance problem, but there's still opportunities for improvement.

  • Frequently creating new components is inefficient, both in terms of memory and CPU.
    You can cache arbitrary objects in a component with getClientProperty(key, value) and putClientProperty(key, value)
  • Imports should be avoided in "hot" code - push them to a project library script.
4 Likes

Thank you for your help, i created a custom property and bind the tag to it, thats improved a lot of the performance, however, is still a little low. What do you mean when you say * Imports should be avoided in "hot" code - push them to a project library script*.

do you mean to take this part of the code (below) and create a project script in order to call from the configure cell function?

from javax.swing import JProgressBar    
	from java.awt import Color
	from javax.swing.border import LineBorder