[Solved] Reload a library; use isLibraryLoaded?

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Locked
User avatar
Ms Ruth
Posts: 6
Joined: Thu Mar 19, 2020 9:00 pm

[Solved] Reload a library; use isLibraryLoaded?

Post by Ms Ruth »

Does it hurt anything or waste any resources to load a library that is already loaded? I have searched for an answer to no avail. I know I may use the isLibraryLoaded function, but is it necessary? Can anyone enlighten me?
Last edited by Ms Ruth on Fri Aug 23, 2024 3:00 am, edited 3 times in total.
Apache OpenOffice 4.1.15
LibreOffice 7.1.8.1 (x64)
Windows 10
JeJe
Volunteer
Posts: 3127
Joined: Wed Mar 09, 2016 2:40 pm

Re: Loading a Library

Post by JeJe »

According to the documentation

https://www.openoffice.org/api/docs/com ... oveLibrary
loadLibrary
Causes the accessed library to be loaded from its storage if it hasn't already been loaded.
So presumably you're calling a function which itself runs isLibraryLoaded, or similar code, as its first step, and then exits if loaded, meaning little cost of omitting it if loaded (edit: and using LoadLibrary). You'd have to check the source code though.

The most efficient way might be a global variable which is set by a sub which loads all the libraries you need. Then you just need to test that variable instead of calling isLibraryLoaded and run the sub if not.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
User avatar
Ms Ruth
Posts: 6
Joined: Thu Mar 19, 2020 9:00 pm

Re: [Solved] Reload a library; use isLibraryLoaded?

Post by Ms Ruth »

I'm surprised this was marked Solved. The reply did not really answer my question. I was already aware of the quoted definition. What I am trying to determine is what, if any, resources are used if I use loadLibrary for a library that is already loaded; in other words without calling isLibraryLoaded, or testing a variable, for that matter.
Apache OpenOffice 4.1.15
LibreOffice 7.1.8.1 (x64)
Windows 10
User avatar
MrProgrammer
Moderator
Posts: 5373
Joined: Fri Jun 04, 2010 7:57 pm
Location: Wisconsin, USA

Re: [Solved] Reload a library; use isLibraryLoaded?

Post by MrProgrammer »

Ms Ruth wrote: Fri Aug 16, 2024 8:37 pm I'm surprised this was marked Solved
You received an immediate reply and for two weeks asked no further questions. This suggested that the response provided the information you wanted and hence the topic was solved. I removed the [Solved] tag.

Ms Ruth wrote: Fri Aug 16, 2024 8:37 pm What I am trying to determine is what, if any, resources are used if I use loadLibrary for a library that is already loaded; in other words without calling isLibraryLoaded, or testing a variable, for that matter.
This would be a question for the developers, who don't visit this forum. We are all users here, just like you. Or look at the source code yourself.

Modern hardware is so fast, and the relationship between your macro code and its execution speed is so remote, that is is pointless to try to determine what method would be faster. Optimize your time, not the computer's. Structure your program so that the intent is clear and it's easy to maintain. Don't bother with details like this. You would spend far more hours testing optimizations than you could ever hope to save (milliseconds) in execution time. If it were my program I would just call loadLibrary unconditionally because that makes the simplest program. You can add a comment which reminds you that it isn't necessary to test if the library has been loaded previously. Does that answer your question?
Mr. Programmer
AOO 4.1.7 Build 9800, MacOS 13.7.8, iMac Intel.   The locale for any menus or Calc formulas in my posts is English (USA).
User avatar
Ms Ruth
Posts: 6
Joined: Thu Mar 19, 2020 9:00 pm

Re: Reload a library; use isLibraryLoaded?

Post by Ms Ruth »

Thank you for your thorough explanation, Mr. Programmer. My code DOES call loadLibrary unconditionally, and I was hoping that would not have negative consequences. I wondered since example code seems to always test for it first. I was away from home and my computer, so I could not catch up for a while. Now that you admitted you would handle loading libraries the same way, I feel better about it.

I probably would not understand the source code, even if I knew where to find it. So many forum users are so knowledgeable, I thought perhaps someone could provide a definitive answer to my question. I am satisfied now that this post is Solved.
Apache OpenOffice 4.1.15
LibreOffice 7.1.8.1 (x64)
Windows 10
JeJe
Volunteer
Posts: 3127
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Reload a library; use isLibraryLoaded?

Post by JeJe »

Here's a test. Change the library name to one of your own.

What you have to worry about is not just speed but a memory leak - if so you call the same function a lot of times and you get an out of memory message (edit: or worse). That doesn't happen for me with my test and the speed of loading a library is fast.

Still, the most efficient, more reliable method is mine - load all your libraries in a sub and just test a global boolean. With more than one library its also the simplest - you can put "if not libsloaded then loadlibs" at the top of your other subs and not need islibaryloaded or loadlibrary for each library all through your subs.

Code: Select all


REM  *****  BASIC  *****

Option explicit

global libsloaded as boolean



Sub Text1
dim a as long,b as long,c as long,d as long, i as long,e as long,f as long

e= getsystemticks
globalscope.basiclibraries.loadlibrary("JeNote")
f = getsystemticks

a= getsystemticks

for i = 1 to 10000
if not libsloaded then loadlibs
next
b= getsystemticks

c= getsystemticks

for i = 1 to 10000
globalscope.basiclibraries.loadlibrary("JeNote")
next
d= getsystemticks

msgbox "load one library " & f- e & chr(10) & "test global boolean " & b -a   & chr(10) & "test loadlibrary " & d-c 

End Sub

sub loadlibs
with globalscope.basiclibraries
if not .islibraryloaded("JeNote") then .loadlibrary("JeNote")
libsloaded = true
end with
end sub
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
Locked