Does Having 1,000 Global Variables Slow Down the KM Editor or KM Performance?

I first started years ago using global variables never minding their potential impact.
Of course I now use local and instance variables.

I'd like to clean up my global variables.
It would be great to know the last time a variable was accessed.
Is there something that would do that?

If there isn't:
My hack approach would be to delete like 50 variables at a time and see what gets broke. =)

I understand that would be ok if a variable is deleted and the macro that uses that deleted variable has a 'set variable xx to xx' at the top of the macro. I am not sure that is the case for a lot of my variables.

I am sure that 90% of my current variables are not really needed. The problem is knowing which 10% should stay!

The last date accessed approach would be splendid if it existed.

Cheers

You could run Rob's MACRO: Run or Edit Recent (Edited, Created, or Used) Macros From a List to walk down the list of the macros you use now, taking a peek to see if they use globals, before you wipe out a bunch of them. The more macros you check, the small the pain will be.

1 Like

Wow, you just gave me a great idea. I could create a macro that examines the contents of all variables (using the For Each action using the option of "Variables") and store the results of all variables in a dictionary where the key is the variable's name. It would also store the time of the inspection in a second dictionary. The next time the macro runs (perhaps on an hourly periodic timer?) it would compare the current value to the last recorded value. If the values differ, it would update the time in the second dictionary, but if the value were the same, it would not update the second dictionary. Using this polling technique, we could create a second macro that could use these two dictionaries to sort and display the variables by "most recently used."

It would also be possible using a third dictionary to keep a tally of the number of times a variables changes. This could provide a means of generating a "most frequently used" list.

I doubt that this would take more than an hour to program. If someone wants the glory, go ahead and code it, I won't mind. I need to go make dinner.

EDIT: Okay, I've finished it. Technically, it took me 90 minutes but 66% of that time was getting frustrated by two problems I had, that might be bugs in KM actions. I'll post my solution shortly in the Macros forum of this website.

My solution for tracking when variables were changed, and how often they were changed, is uploaded here:

1 Like

FWIW I personally prefer to list KM Variables by descending size (and optionally clear some of them.

1 Like

This is the closest I could come up with. It'll go through each of your globals, looking for macros whose XML contains the global's name inside either >...< or %...% (to match both actions and tokens) and get the "Last Used" date for the most recently run macro.

It will throw false positives if your global's name happens to match a "keyword" in KM -- so the global "Files" will be matched with a "For Each: items in a directory" action because that has the XML <key>CollectionType</key<string>Files</string> -- but that should be "safe" because it will appear that the global was more recently used than it actually was.

Output is a tab-delimited list that you can copy to a spreadsheet:

...and a global that has no macro referencing it will be dated 1 Jan 1970. (It wouldn't be too difficult to make the date output numerical so you could easily sort it, or you could use spreadsheet text functions.)

Since this is matching every global name against every macro, this could take a while if you've lots of both!

List Globals and Last Used.kmmacros (2.8 KB)

AppleScript
tell application "Keyboard Maestro Engine"
	set theVars to name of every variable
end tell

set theList to ""

tell application "Keyboard Maestro"
	considering case
		repeat with eachVar in theVars
			if eachVar = "ENV_PATH" then
				-- do nothing
			else
				set theList to theList & eachVar
				set theDate to date "Thursday 1 January 1970 at 00:00:00"
				set macroIDs to (get every macro whose xml contains (">" & eachVar & "<") or xml contains ("%" & eachVar & "%"))
				repeat with eachMacro in macroIDs
					if used date of eachMacro > theDate then set theDate to used date of eachMacro
				end repeat
				set theList to theList & tab & theDate & linefeed
			end if
		end repeat
	end considering
end tell

return theList
2 Likes

This is a slight double-post, but my "VIP - Variable Inspector Prompt" was initially written tp help deal with trimming down variables, sorting by size, and other things. Works great for this purpose.

image

1 Like

wow, @Nige_S thank you, I ran the macro and nothing happened... I waited quite awhile, can't tell if it's running, the KM menu bar icon goes active for a second or so then inactive.

I was getting an error on " set theDate to date "Thursday 1 January 1970 at 00:00:00"

LLM used:

tell application "Keyboard Maestro Engine"
	set theVars to name of every variable
end tell

set theList to ""

tell application "Keyboard Maestro"
	considering case
		repeat with eachVar in theVars
			if eachVar = "ENV_PATH" then
				-- do nothing
			else
				set theList to theList & eachVar
				set theDate to current date
				set theDate's year to 1970
				set theDate's month to January
				set theDate's day to 1
				set theDate's time to 0
				set macroIDs to (get every macro whose xml contains (">" & eachVar & "<") or xml contains ("%" & eachVar & "%"))
				repeat with eachMacro in macroIDs
					if used date of eachMacro > theDate then set theDate to used date of eachMacro
				end repeat
				set theList to theList & tab & theDate & linefeed
			end if
		end repeat
	end considering
end tell

return theList

That's the second time that's happened in date-related ASs... The trick seems to be to force a recompile of the script to recreate the date object, or you can do as you have and build it every run.

If you go back to my version you'd change the line

set theDate to date "Thursday 1 January 1970 at 00:00:00"

to

set theDate to date "1/1/1970"

replacing 1/1/1970 with the representation of 1 January 1970 in your locale's format. Hit the Enter key and the script will recompile to give you the "full" date object.

Is it working now you've fixed the error? The KM icon in the menu bar should spin for as long as it takes for the macro to complete and show the "Display Text" with results.

1 Like

I ran it on one of my remote computers, KM icon busy for a while, I drove home (3 minutes) checked the cpu and it was done but no window.
I just ran it again on the same CPU remotely, keeping an eye on it. It ran for like 23 minutes =) and then displayed the window. I captured the text put it into a spreadsheet and it is AWESOME. Bro, thank you a bunch......

Now 'a variables we will trim, a variables we will trim.... hi ho.............. you know the rest...

Cheers.

1 Like

Only thing I can think of is that you've enough globals and macros that the get every macro command is timing out (all the other commands are, individually, quick -- just repeated many, many times).

If you want to use this again, try putting the command in a with timeout block -- for example, this will allow the command 10 minutes to complete:

				with timeout of 600 seconds
					set macroIDs to (get every macro whose xml contains (">" & eachVar & "<") or xml contains ("%" & eachVar & "%"))
				end timeout
1 Like

nice, I put the 'timeout' in there and it ran fine. Now I have an awesome detailed list to work from. thanx