PDA

View Full Version : CleanUp those millions of constants



ReneMiner
04-10-2020, 15:29
very simple. To avoid the engine must load a few thousand const-values every time when i run a script - and then my own constants also - its eating up time and i guess of the more than 5000 that are poked to memory if Uses "UI" we might use a few hundreds per script, if at all. And then the names - what const names are available for %MB_ ??? - yes, a lot.

Can you tell what is the difference between this



Begin Const
$SE_CREATE_TOKEN_NAME = "SeCreateTokenPrivilege"
$SE_ASSIGNPRIMARYTOKEN_NAME = "SeAssignPrimaryTokenPrivilege"
$SE_LOCK_MEMORY_NAME = "SeLockMemoryPrivilege"
$SE_INCREASE_QUOTA_NAME = "SeIncreaseQuotaPrivilege"
$SE_UNSOLICITED_INPUT_NAME = "SeUnsolicitedInputPrivilege"
$SE_MACHINE_ACCOUNT_NAME = "SeMachineAccountPrivilege"
$SE_TCB_NAME = "SeTcbPrivilege"
$SE_SECURITY_NAME = "SeSecurityPrivilege"
$SE_TAKE_OWNERSHIP_NAME = "SeTakeOwnershipPrivilege"
$SE_LOAD_DRIVER_NAME = "SeLoadDriverPrivilege"
$SE_SYSTEM_PROFILE_NAME = "SeSystemProfilePrivilege"
$SE_SYSTEMTIME_NAME = "SeSystemtimePrivilege"
$SE_PROF_SINGLE_PROCESS_NAME = "SeProfileSingleProcessPrivilege"
$SE_INC_BASE_PRIORITY_NAME = "SeIncreaseBasePriorityPrivilege"
$SE_CREATE_PAGEFILE_NAME = "SeCreatePagefilePrivilege"
$SE_CREATE_PERMANENT_NAME = "SeCreatePermanentPrivilege"
$SE_BACKUP_NAME = "SeBackupPrivilege"
$SE_RESTORE_NAME = "SeRestorePrivilege"
$SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
$SE_DEBUG_NAME = "SeDebugPrivilege"
$SE_AUDIT_NAME = "SeAuditPrivilege"
$SE_SYSTEM_ENVIRONMENT_NAME = "SeSystemEnvironmentPrivilege"
$SE_CHANGE_NOTIFY_NAME = "SeChangeNotifyPrivilege"
$SE_REMOTE_SHUTDOWN_NAME = "SeRemoteShutdownPrivilege"
End Const





and this :



Alias Function As Equate
Alias Type As Enum_Const


Enum_Const String_SE


Equate CREATE_TOKEN_NAME() As String : Equate = "SeCreateTokenPrivilege" : End Equate
Equate ASSIGNPRIMARYTOKEN_NAME() As String : Equate ="SeAssignPrimaryTokenPrivilege" : End Equate
Equate LOCK_MEMORY_NAME() As String : Equate ="SeLockMemoryPrivilege" : End Equate
Equate INCREASE_QUOTA_NAME() As String : Equate ="SeIncreaseQuotaPrivilege" : End Equate
Equate UNSOLICITED_INPUT_NAME() As String : Equate ="SeUnsolicitedInputPrivilege" : End Equate
Equate MACHINE_ACCOUNT_NAME() As String : Equate ="SeMachineAccountPrivilege" : End Equate
Equate TCB_NAME() As String : Equate ="SeTcbPrivilege" : End Equate
Equate SECURITY_NAME() As String : Equate ="SeSecurityPrivilege" : End Equate
Equate TAKE_OWNERSHIP_NAME() As String : Equate ="SeTakeOwnershipPrivilege" : End Equate
Equate LOAD_DRIVER_NAME() As String : Equate ="SeLoadDriverPrivilege" : End Equate
Equate SYSTEM_PROFILE_NAME() As String : Equate ="SeSystemProfilePrivilege" : End Equate
Equate SYSTEMTIME_NAME() As String : Equate ="SeSystemtimePrivilege" : End Equate
Equate PROF_SINGLE_PROCESS_NAME () As String : Equate ="SeProfileSingleProcessPrivilege" : End Equate
Equate INC_BASE_PRIORITY_NAME () As String : Equate ="SeIncreaseBasePriorityPrivilege" : End Equate
Equate CREATE_PAGEFILE_NAME () As String : Equate ="SeCreatePagefilePrivilege" : End Equate
Equate CREATE_PERMANENT_NAME () As String : Equate ="SeCreatePermanentPrivilege" : End Equate
Equate BACKUP_NAME() As String : Equate ="SeBackupPrivilege" : End Equate
Equate RESTORE_NAME() As String : Equate ="SeRestorePrivilege" : End Equate
Equate SHUTDOWN_NAME() As String : Equate ="SeShutdownPrivilege" : End Equate
Equate DEBUG_NAME() As String : Equate ="SeDebugPrivilege" : End Equate
Equate AUDIT_NAME() As String : Equate ="SeAuditPrivilege" : End Equate
Equate SYSTEM_ENVIRONMENT_NAME() As String : Equate ="SeSystemEnvironmentPrivilege" : End Equate
Equate CHANGE_NOTIFY_NAME() As String : Equate ="SeChangeNotifyPrivilege" : End Equate
Equate REMOTE_SHUTDOWN_NAME() As String : Equate ="SeRemoteShutdownPrivilege" : End Equate

end Enum_Const


Global SE# as String_SE


Dim a$ as String = SE#.BACKUP_NAME


msgbox a$


Right. It is only available after Global SE#. Not poked to memory nor variables table
it could be better if the top line were


Enum Const SE# As String

to make it unnecessary to repeat "() As String : Equate = "..." : End Equate
String - on top - all equates in an enumeration are of the same type.
There can only be one of this udt if the last line (actually End Type) would finally dimension it and it were no more possible
to dim anything else as String_SE because String_SE is only the name for this example - actually SE# ends with that numbering symbol # - it works to have subelements but it could be restricted in Local/Global/Dim/Redim.
No Variables name neither a type to dim As must end with # in a Dim-statement
If Keyword "Type" for this "Enumerated Constant" is replaced by another keyword or combination of keywords "Enum" & "Const" it should be not a problem to make the "End Enum Const" finally dimension the Global UDT with the Name given in the beginning.
It could be Enum Equate as well but I used it to replace keyword Function. so i had to use it 3 times per line
(BUG: thinAir will not realize "Function fName() :Return something : End Function " on one line as valid function.
But using Alias Function As Equate works fine.
Final syntax of camouflaged Type-functions to Enumerate Equates can be simpler- no parenthesis nor anything else - just the result to return has to be assigned.


Begin Equate SE# As String
LOAD_DRIVER_NAME = "SeLoadDriverPrivilege"
' function will always return this value
' - so its not possible to change it once the script runs
'....
End Equate


These constants are from WinApi "NT Defined Privileges" - just taken for the example
for the purpose of reading something in the msgbox

Petr Schreiber
04-10-2020, 20:11
Hi Rene,

I like how you think about improving things other take for granted - please continue.

However, I am afraid your proposal generates higher memory footprint + takes longer to execute:
- each function name is stored + its signature + its return type (compared to string equate hash, which simply knows all items are string and does not store this info for each)
- the overhead of function call is much higher than referencing a value from internal hash and you propose type function, which adds one more level of resolving


Petr

ReneMiner
06-10-2020, 02:44
Really? i thought as long as not called it will not be arranged .
Anyway, the type-functions are actually UDT-Statics containing a function pointer only.

The idea is to use these statics for a 1-time write and being read-only thereafter. and to have only 1 variable of this udt.

Also its to group equates like all windows-messages %WM_xxx [~500 equates] would end up in one variable as
"WM#" only. it can expand the names when a dot was typed to "WM#.<messagename>" instead of torturing thinAir to load all the 500 %WM_'s and keep them present as soon as % is typed and making slower pcs lag.

I think you know the Enum-End Enum stuff from Visual whatever - i think its present since Visual Basic 1.0 - where you can


Enum Furniture As Byte
Chair 0x01 ' actually it will increase by 1 since it is Ascend (except Enum something As String or Enum Descend)
Table 0x02
Bench 0x03
Cabinet 0x80
End Enum


' and in a function, method or property you can specify to accept a parameter as

Sub CleanTheKitchen(Byval startingWith as Furniture)
' the parameter is actually a byte as Furniture is Enumerated "As Byte"
' it limits the values to be passed here to a certain amount of possibilities
' Intellisense (AutoComplete) knows what parameters to suggest
' and even the user is informed what can be here. Anyway any expression
' in the range of BYTE is valid to pass here
End sub


Enum [Ascend|Descend|Bits]
' auto-enum (no value given by the user) starts with 0 and increases by 1 if Enum Ascend (default)
' auto-enum descend starts depending on Vartype with all bits set, decrement 1 then, alike 0xFF Byte, 0xFFFFFFFF Long
' auto enum bits starts with 0 and then sets the right bit and shifts 1 left
' for example
Enum Bits FileAttributes As Byte
'starts like
FileNone 0b00000000
FileReadOnly 0b00000001
FileHidden 0b00000010
FileArchive 0b00000100

oops - Do we have that?

&H, &B i know... but shouldnt there be 0o (octal) and 0b (binary) as well since we have 0x (hex) ?


It's not possible to Dim something as Furniture - and there's only 1 Furniture-enumeration
But now you know what this "Enum" or "typedef" in other languages is in reality derived from.
Even when you type "Furniture." - at the typed dot autocomplete suggests the members

-and even from now - would be nice if thinAir - which is the official thinBasic-programming-editor replaces deprecated syntax automagic. thinAir is shipped with the language - so who ever uses thinBasic has thinAir available.

If the user takes advantage of it - or uses any other texteditor? -
is a question thats answer we can force to be the one we like to get...
The editor is mostly used to type in code.
I am already on something that is just like a tools-addOn but actually thinAir should provide it. You know the issue about sample scripts if running them from C:\thinBasic\sample-scripts-path that they get saved automatic, i.e. original sample is overwritten.
Simple solution
Ship all the samples within a compressed archive (f.e. zip or .cab-file, but not as .tBasic-files. Even camouflaged as ".bin", ".dat", ".tBArchive" or something so users will not modify it.

Add a submenu-popup "Samples" or "Examples" at Help-menu of thinAir that will display the archive content.

if user opens a sample script it will be extracted from the archive and saved to a Samples-subfolder at the users preferred thinBasic-Scripts location. The user can play, test and modify- the original sample remains in the archive.
Multiple users on the same pc will no longer start killing each other because they found samples that no more run because another user modified it or "samples" that are actually not really samples but accidentely saved user scripts at the wrong location

But Back to the equates/ ReInvention of thinBasic / other major changes that would endanger backward-compatibility:

When a *-tBasic*-file is loaded into thinAir and it would encounter like "%WM_" when it is deprecated but still valid to use
thinAir can give a notification to the user

"that syntax is no more up to date and will be replaced in the future- would you like to replace it now ?"

(auto-backup the original into a new subfolder at scripts directory) - perform the change on the current script.
it could ask the user thereafter if the associated option in future thinAirs settings was not checked yet:

"Would you like thinAir at idle times to check your scripts and apply new syntax or to inform you about deprecated content? Please specify folder(s) where to check."


if a *.tBasic* script is launched from system shell but not from inside thinAir then thinBasic.exe could after script execution inform and ask if to open the script in thinAir now.

Enough. Enjoy, happy coding. good night