mIRC Home    About    Download    Register    News    Help

Print Thread
#51214 24/09/03 10:52 PM
Joined: Dec 2002
Posts: 37
O
oracel Offline OP
Ameglian cow
OP Offline
Ameglian cow
O
Joined: Dec 2002
Posts: 37
I would like to suggest constants to be implemented in a feature version of mIRC. For those of you who don't know what constants are, I would like to point you to google.com. For those of you who are going to suggest that variables using /set can replace this, I would also like to point you to the same site to learn the difference between variables and constants.

Here are some example usages for constants:
- Configuration of very small addons, where using dialogs would be overkill
- Fast access to values that will not change in run-time, but might change later (when I say fast I mean faster than using %variables and hashtables. Constants should be faster than both of these, given that they are implemented correctly)

Here are some benefits:
- Eased modification of scripts
- scalability
- Better code structure
- Less use of global variables
- Improved speed in script execution

These constants should be defined OUTSIDE any scripts, such as aliases or events. They should as well be defined within the first few lines of a script file.

Here is a concrete example, with pseudo-code for the constant:

Code:
const £CHUNK_SIZE = 4096

Alias SendFile {
  var %filename = $+(",$1-,"), %chunksize = £CHUNK_SIZE
  ;do stuff here
}


So what do you think? (I would like to remind you that there is a distinct difference between variables and constants.)

Edit: added code tags

Last edited by oracel; 25/09/03 12:09 AM.

Bloop
#51215 24/09/03 11:17 PM
Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
A good idea.

Alternatly, I'd like to see Script Variables that are local to a script file, or Static Variables local to an alias/event that do not expire. These could double in functionality as "Constants" and gives you the option to modify them by script.
Code:
(beginning of file)
static %settings = settingsfile.ini
static %botname = TrivServ
 
Alias something {
  static %calls = %calls + 1
  writeini %settings Something Calls %calls
}

Unlike global variables, the values for these variables wouldn't be stored in a file, but simply kept in memory like hashtables are. If mIRC restarts, their values are reset.

There are some similar ideas posted here aswell.

- Raccoon

PS. Yes, yes, codemastr... Constants are not Variables are not Constants. Our disagreements have been dualy noted in the above link.

Last edited by Raccoon; 24/09/03 11:45 PM.

Well. At least I won lunch.
Good philosophy, see good in bad, I like!
#51216 25/09/03 12:23 AM
Joined: Dec 2002
Posts: 37
O
oracel Offline OP
Ameglian cow
OP Offline
Ameglian cow
O
Joined: Dec 2002
Posts: 37
Great ideas, Raccoon! Static variables would be very handy! However, it is important that both constants and statics are (as you stated) kept in memory, and not in files. The keyword here is improved speed and efficiency.

By your example, statics declared in the global scope (outside any alias or event) could easily replace constants, and still have almost the same functionality.

However, as I mentioned earlier, using constants would speed up script execution significantly, since it won't change at all during execution.

Imagine this benchmark:

Code:
Alias benchmark1 {
  ;Loop number one, using global variables
  set %i 1 | set %t 50000
  while (%i <= %t) {
    inc %i
  }
}
Alias benchmark2 {
  ;Loop number two, using constants and statics
  const %t = 50000 | static %i = 1
  while (%i <= %t) {
    inc %i
  }
}


When compared, the latter alias should execute alot faster than the first one.

(Notice that the constant is declared inside the alias in the above example, this should normally not be done, but it should still be a possibility)


Bloop
#51217 25/09/03 04:19 AM
Joined: Mar 2003
Posts: 1,271
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Mar 2003
Posts: 1,271
Maybe a dumb thing to say, but isn't that what the identifiers are for? Like $version, or a simple $myidentifier that does nothing else but return a value ?


DALnet #Helpdesk
I hear and I forget. I see and I remember. I do and I understand. -Confucius
#51218 25/09/03 06:56 AM
Joined: Dec 2002
Posts: 196
T
Vogon poet
Offline
Vogon poet
T
Joined: Dec 2002
Posts: 196
Well he wanted something that's local.

Anyway, to maintain consistency, instead of having const or static, have something like

var -s

and set -s

etc


trenzterra
AustNet #trenzterra and #w
Head Scripter @ http://trenzterra.uni.cc
#51219 25/09/03 06:58 AM
Joined: Aug 2003
Posts: 314
S
Fjord artisan
Offline
Fjord artisan
S
Joined: Aug 2003
Posts: 314
Yes but those are considerably slower, especially when you call the alias numerous times. Also, the nature of custom identifiers is that commands (loops and whatever else) are usually run before returning a value. A custom identifiers whose only function is to return, for example, a script's logo is not very efficient since global variables are more suited to this purpose and would work faster. Having never used constants/statics, I can only imagine their benefit in situations such as, an alias that calls another alias, using constants may save us having to pass parameters to the second alias. If this is made to improve speed, that would be good too

#51220 25/09/03 09:01 PM
Joined: Dec 2002
Posts: 2,809
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 2,809
/var -s and /set -s already exist.

#51221 26/09/03 02:43 AM
Joined: Dec 2002
Posts: 37
O
oracel Offline OP
Ameglian cow
OP Offline
Ameglian cow
O
Joined: Dec 2002
Posts: 37
This is mIRC-U-Topia, common rules of mIRC physics do not apply when doing abstract discussions wink


Bloop
#51222 26/09/03 08:28 AM
Joined: Dec 2002
Posts: 196
T
Vogon poet
Offline
Vogon poet
T
Joined: Dec 2002
Posts: 196
I didn't bother to check, sorry. Anyway there are about 20 alphabets to choose from.

What's the difference between a constant and a local variable that you don't change, anyway. You can choose to leave it alone.


trenzterra
AustNet #trenzterra and #w
Head Scripter @ http://trenzterra.uni.cc
#51223 26/09/03 10:12 AM
Joined: Feb 2003
Posts: 2,812
Hoopy frood
Offline
Hoopy frood
Joined: Feb 2003
Posts: 2,812
There would only be a performance difference in a compiled language, because it would compile the program differently by hardcoding that value into the software so it doesn't have to query a memory address each time it wants that value. It can even pre-calculate math formulas containing that constant, so it doesn't have to recalculate the formula each time. (eg: 0.6 is much faster than 3/5 or %const / 5)

In mIRC however, nothing is compiled and nothing is pre-calculated. Any constant would essentially be a read-only variable, accessing the memory address just as slowly as any other variable. To make such a variable read-only would simply cripple its full potential, offering absolutly no speed improvements. (unless significant changes were made to the script engine that would probably negativly impact many scripting methods)

That's why I offered the alternative terminology 'static variable'.

- Raccoon


Well. At least I won lunch.
Good philosophy, see good in bad, I like!
#51224 26/09/03 06:11 PM
Joined: Dec 2002
Posts: 2,809
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 2,809
Quote:

There would only be a performance difference in a compiled language, because it would compile the program differently by hardcoding that value into the software so it doesn't have to query a memory address each time it wants that value.

LOL good one. Again I bring up the "Racoon knows nothing about programming" comment. If the constant is not stored in memory, then pray tell where is it stored? A computer has two storage locations, memory and registers. You would never store a constant in a register because registers are used for arithmetic, and you can't perform arithmetic (changes a value) on something that's value can't be changed. Stop making up stuff when you don't know a damn thing about what you are saying. A CONSTANT IS STORED IN MEMORY.

#51225 26/09/03 06:15 PM
Joined: Dec 2002
Posts: 2,809
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 2,809
He wanted something local? Did you look at his example?
Code:
const £CHUNK_SIZE = 4096
Alias SendFile {  
var %filename = $+(",$1-,"), %chunksize = £CHUNK_SIZE  ;do stuff here
}

That doesn't look local to me. Constants are almost never local. Think about it. You probably want a SCRIPT_VERSION constant right? You want to have to do const SCRIPT_VERSION = 1.0 in EVERY alias? If you had to do that, it defeats the purpose of a constant (change one value and it changes everywhere it is used). SCRIPT_VERSION would likely be defined at the top of your script and then all aliases have access to it. There is virtually no reason to have a local constant.

#51226 26/09/03 07:56 PM
Joined: Dec 2002
Posts: 37
O
oracel Offline OP
Ameglian cow
OP Offline
Ameglian cow
O
Joined: Dec 2002
Posts: 37
Quote:

In mIRC however, nothing is compiled and nothing is pre-calculated. Any constant would essentially be a read-only variable, accessing the memory address just as slowly as any other variable. To make such a variable read-only would simply cripple its full potential, offering absolutly no speed improvements.


You are partially right, but keep in mind that even though nothing is compiled, the language is still interpreted. Values defined as non-changing throughout execution, would not require any further evalution as the script executes. This is what could/should improve execution time.

However, don't forget the positive effects it could have on the language/code structure.


Bloop
#51227 30/09/03 12:55 PM
Joined: Dec 2002
Posts: 39
V
Ameglian cow
Offline
Ameglian cow
V
Joined: Dec 2002
Posts: 39
While all correct, it's only so on a von Neuman architechture :-) It's not too hard to imagine a conceptual barrier between code memory and data memory, in which case a 'constant' (processor ISA allowing) could be inlined into the code memory rather than be located in the data memory (registers, stack, heap). Self modifying code isn't very common these days and the unified memory given by the von Neuman arch. is most often not very interesting in practise.

#51228 30/09/03 06:07 PM
Joined: Dec 2002
Posts: 2,809
C
Hoopy frood
Offline
Hoopy frood
C
Joined: Dec 2002
Posts: 2,809
You're right, there is such a thing as immediate addressing mode. However, in compiled languages I'm familiar with constants aren't really constant. For example, C++ has an operator called const_cast<>() that exists almost completely for the purpose of allowing you to change a constant's value. That makes it impossible to use an immediate value simply because it can change. Also strings can never be immediate values. The largest immediate value you can have (on a 32bit machine) is a 32bit value. That means you have to pass the address of the offset of the string in memory and then read memory to actually get the string's contents. Of course, with optimizing compilers it may be able to use immediate values _sometimes_ but it most certainly can not do it _all times_.

#51229 05/10/03 12:01 PM
Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
mirc interprets the text of scripts right?

So what ever the value name is %blah !blah &blah etc for what ever type of thing it is variable static constant, mirc would have to read that name then go look the value up somewhere, i cant see how any one would be much faster than another, i did some quick loop tests (see below) i made %i a var %i then i made it a set %i, var seemed o run a little faster, but where talking about 100 ticks at the best more
var %i = 100000 | while (%i > 0) { dec %i }
Did this 3 times heres the number of ticks each time 6985, 6922, 6957
set %i 100000 | while (%i > 0) { dec %i }
Did this 3 times heres the number of ticks each time 7023, 7081, 7019
one last test was this,
var %z = 0 | var %i = 100000 | while (%i > %z) { dec %i }
Did this 3 times heres the number of ticks each time 7103, 7099, 7134
Looks like using variables increases the time a script takes, which would be logical to me, as reading %z and finding its value of 0 takes longer than reading 0
Sets must take longer than Var as it must be adding it to some global list for storing to the remote.ini SET -u20 took a little longer again

So the fastest constants are real constant values ie the actual value...
You could be real rude, and during testing and debugging use a bunch of SETs and when all is ready to go, backup the script and do a search and replace on the constants replacing them with the constant value, as far as i know thats what compilers do (although seeing the stuff about C being able to change its constants who knwos) I know assembler compilers constants were just replaced with the constant value during compile



Link Copied to Clipboard