[sane-devel] sane_get_devices() is very slow

Derry Bryson dbryson@techass.com
Sat, 15 Feb 2003 15:13:14 -0800


Henning Meier-Geinitz wrote:
> 
> Hi,
> 
> On Thu, Feb 13, 2003 at 02:49:40PM -0800, Derry Bryson wrote:
> > I am thinking along the lines of keeping a list around, but allowing the
> > user the option to detect devices when selecting a source.  This I can do
> > in my code without needing to change the sane API.
> 
> I don't really understand. What source are you talking about? So you
> mean the scanner (or other image application device)? "source" in SANE
> ususally means "flatbed", "ADF" or "Transparency adapter".
> 

Sorry, I'm doing both TWAIN and SANE at the same time and mixing up my
terms.  For SANE this would be a 'device'. 

> > If you were to want to consider changing the API,
> 
> There will be an API change in SANE2, but I don't think it's necessary
> in the case we are talking about.
> 
> > I might suggest adding a sane_list_devices() function with the same
> > signature as sane_get_devices() that would be implemented in most
> > backends as a call to sane_get_devices(), but in the dll and net
> > backends would return the list of devices cached from the last call
> > to sane_get_devices().  The cached list would saved to a file when
> > the backend is unloaded and read when the backend is loaded.
> 
> Why? In this case, new devices aren't found and unplugged or powered
> off devices are still displayed as "live". I think that's broken.
> 

First, a front-end could choose to always use sane_get_devices() to
force a check.  Second, a program could be written to update the
cache file that can be run at system startup, at anytime by the
user after adding or removing a device, and whenever a usb device
is added or removed.  Third, the front-end could be written to allow 
the user to select redetection when selecting a device (i.e. an 'Update 
Devices' button on the device selection dialog).

It would allow the front-end to allow the user to determine whether
or not to spend time looking for new devices.  Most people probably
don't plug and unplug their scanner often and should not have to
wait for it to be redetected everytime they want to scan.

> During runtime the list of devices is "cached", i.e. sane_init of a
> backend is only called when necessary.
> 
> Here is some simple test code:
> 
>   stopwatch (sane_init (&version_code, NULL));
>   stopwatch (sane_get_devices (&device_list, SANE_TRUE));
>   stopwatch (sane_get_devices (&device_list, SANE_TRUE));
>   stopwatch (sane_get_devices (&device_list, SANE_FALSE));
>   stopwatch (sane_get_devices (&device_list, SANE_FALSE));
>   stopwatch (sane_exit ());
> 
> The macro "stopwatch" times the function that's given as argument.
> So we call sane_init, get the device list twice, and then get the
> device list including the devices not locally connected. I have one
> USB and one SCSI scanner connected. Result:
> 
> sane_init (&version_code, NULL) took 0.000 seconds
> sane_get_devices (&device_list, SANE_TRUE) took 0.134 seconds
> sane_get_devices (&device_list, SANE_TRUE) took 0.014 seconds
> sane_get_devices (&device_list, SANE_FALSE) took 0.221 seconds
> sane_get_devices (&device_list, SANE_FALSE) took 0.029 seconds
> sane_exit () took 0.003 seconds
> 
> So the second call of sane_get_devices is much faster than the first
> one (as intended). The third one also checks for network scanners and
> take a bit longer therefore. But the forth one now knows the network
> scanners and is faster again.
> 
> This is 0.221 seconds (221 miliseconds) we are talking about and this
> time is only necessary once per sane_init (program start, not scan).
> 

Certainly you are correct once the front-end has called sane_get_devices() the
first time, calls after that will be fast.  But this is irrelevant if you
want to detect new devices everytime, correct?  Also, each front-end must do 
the same thing and each must wait for devices to be detected.  

I upgraded to 1.0.11 and it is much better at just over 7 seconds (vs. 36 seconds 
with 1.0.5).  Obviously your computer is much faster than mine.

Derry