Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Documentation Make tcc great again - what's the benefit of LIBRARY?

Dec
73
1
The tcc22 announcement states: "We added library functions to TCC. Library function syntax is similar to batch files, but they're loaded into RAM and called as if they are internal commands. This allows very fast common subroutines, or a powerful multi-line alias capability."

What I do understand is the part about library being less tiresome than multi-line aliases. I'm wondering about the "very fast" part - esp. in comparison to btm files which are loaded into RAM too, aren't they? And how about the possibility to call a batch file from a library function?

Or are libraries the replacement for the crawling speed of GOSUB, which needs to search through the whole batch file to find the matching :function marker? If so, it would be convenient if inline library functions could be read from the top of a batch file and then auto-discarded after the batch file is finished.
 
BTMs are loaded into memory when you call them. Library functions are loaded when TCC starts. You already mentioned other advantages. They're easier to deal with than multi-line aliases. TCC doesn't have to read through a file looking for subroutine labels.

Yes, you can call a BTM from a library function.

Code:
v:\> library /f lipc
lipc {
call v:\ipreg\country\iptoc.btm %1
echo done
}


v:\> lipc 128.230.13.36
US
done
 
There's seems to be considerable advantage to not having to read a BTM. (Rex, is this a fair test?)
Code:
v:\> type nooperation.btm
echos

v:\> timer & do i=1 to 10000 ( nooperation.btm ) & timer
Timer 1 on: 00:32:09
Timer 1 off: 00:32:21  Elapsed: 0:00:11.92

v:\> library /f nooperation
nooperation {
echos
}


v:\> timer & do i=1 to 10000 ( nooperation ) & timer
Timer 1 on: 00:32:34
Timer 1 off: 00:32:36  Elapsed: 0:00:02.39
 
There's seems to be considerable advantage to not having to read a BTM.

Sure, if you happen to call a .btm a *lot* of times, which some people might do (though I don't) the LIBRARY is a nice solution for not using multi-lined aliases as the call times do seem to sum up.

For my use case - replacing GOSUB - the current approach is a hassle, I'd need to be able to call *temporary* library functions that are are inlined in the *same* .btm and wrapped in SETLOCAL and ENDLOCAL to do this...

Code:
@echo off
setlocal

library testing123 {
echo %1
}

testing123 "hello, world"

endlocal
quit 0

... instead of this ...

Code:
@echo off
setlocal

goto skipgosub
:testing123 [string]
echo %string
return
:skipgosub

gosub testing123 "hello, world"

endlocal
quit 0
 
I imagine libraries serve the same function as libraries in C. That is, you can write a function, and run it anywhere.

It's also useful if you have changing data, such as different locations for the apps directory. Currently on my system, it is d:\newin, but on the previous computer, it was h:\mswin. I do a lot of this by using a registry key hklm\software\wendy\folders . I can change directly to any shell folder, or any of these folders, just as eg 'cdf setup'

It's something i would have liked in REXX, or something i could implement in my weave program, but i have not got to it yet.
 
Reading through the (in-memory) BTM to find a subroutine doesn't seem to be the problem you (and I) thought it would be. A library function does have a significant advantage over a subroutine. That can be seen in this mosestly-sized BTM.
Code:
v:\> library /f nooperation
nooperation {
echos
}

v:\> type sublib.btm
iff "%1" == "sub" then
        echo Using the subroutine ...
        timer
        do i=1 to 100000
                gosub nooperation
        enddo
        timer
elseiff "%1" == "lib" then
        echo Using the library function ...
        timer
        do i=1 to 100000
                nooperation
        enddo
        timer
endiff
quit

:nooperation
        echos
return

v:\> sublib.btm sub
Using the subroutine ...
Timer 1 on: 11:28:41
Timer 1 off: 11:29:11  Elapsed: 0:00:30.09

v:\> sublib.btm lib
Using the library function ...
Timer 1 on: 11:29:21
Timer 1 off: 11:29:42  Elapsed: 0:00:21.09

I expected the time to find the subroutine to go up if the BTM were made bigger, so I added 500 lines (all "echos") at the beginning of SUBLIB.BTM and called the new file SUBLIB2.BTM. But,
Code:
v:\> echo %@lines[sublib2.btm]
519

v:\> sublib2.btm sub
Using the subroutine ...
Timer 1 on: 11:33:13
Timer 1 off: 11:33:43  Elapsed: 0:00:30.11

v:\> sublib2.btm lib
Using the library function ...
Timer 1 on: 11:33:51
Timer 1 off: 11:34:12  Elapsed: 0:00:21.11

Here it is again, this time with 5000 extraneous "echos" lines. TCC must already have an efficient mechanism for finding subroutines.
Code:
v:\> echo %@lines[sublib2.btm]
5019

v:\> sublib2.btm sub
Using the subroutine ...
Timer 1 on: 11:39:03
Timer 1 off: 11:39:33  Elapsed: 0:00:30.26

v:\> sublib2.btm lib
Using the library function ...
Timer 1 on: 11:39:39
Timer 1 off: 11:40:01  Elapsed: 0:00:21.23
 
The simple answer: don't use wrong tools. If you want a GOSUB, use GOSUB, don't try to use anything else, you just confuse yourself and everybody else.
 

Similar threads

Back
Top