Search code examples
firefox-addonjsctypes

What should we defer in ctypes? ctypes.open? Function defintions?


For performance, efficiency, etc.: Does ctypes.open have a large overhead? Should it be deferred till just before use? Or is it ok if I open like a bunch of dll's right on load of my addon?

Cu.import('resource://gre/modules/ctypes.jsm');

var lib = {
  blah: ctypes.open('blah.dll'),
  ctypes.open('blah2.dll'),
  ctypes.open('blah3.dll'),
  ctypes.open('blah4.dll'),
  ctypes.open('blah5.dll'),
  ctypes.open('blah6.dll'),
};

for (var l in lib) {
  lib[l].close();
}

Should we also defer function defintions or is that light lifting like:

var SetForegroundWindow = lib.user32.declare('SetForegroundWindow', ctypes.winapi_abi, ctypes.bool,
    ctypes.int32_t
);

Thanks


Solution

  • It would make (some) sense to delay the loading of a dll that is not already mapped into the virtual address space of the firefox process.

    But user32.dll is a dependency of xul.dll.

    So when you ctypes.open('user32.dll') all that happens is that the reference counter of the particular dll is increased by one. Hardly the subject of optimization (premature or not).