in reply to Quick User Existance Check?

Don't know why everyone is so biased aganst posters that ask for a speed tip -- not everyone sits here and trys to pre-optimize every line of code -- as for the posters post how do you know that he does not have a finnished app in which he seems the tight inner loop running too slow for his needs?

Anyways, if the unix host has a shadowed passeord file most of the time normal users are allowed to read the /etc/passwd file as it has no password entries in it. you can slurp that file before you tight loop and make a hash of usernames then just check if they are defined. This will save you (on most systems getpwnam is not cached ...) the load /etc/passwd, search /etc/passwd steps per user lookup. I had this same problem at a site that had a very large password file where getpwnam was taking 10 seconds to run per call -- it speeded up my tight loop by 20 minutes.

-Waswas

Edited: one note with this method, there is a chance for a race condition if the password file is not sorda static -- if a user is added after you make the hash and before your tight loop is done the check with give you a false negitive match or if the user is deleted before the tight loop finishes a false positive.

Replies are listed 'Best First'.
Re: Re: Quick User Existance Check?
by sgifford (Prior) on Jun 25, 2003 at 18:47 UTC
    This will work if the system is guaranteed to use /etc/passwd, or if it's your system and it does use /etc/passwd, but it's not portable. It won't work if the system uses NIS, LDAP, a database-backed PAM module, etc. A more portable way of doing the same thing is getpwent.

    And with either of these techniques, which is faster is determined by the speed of the backend, the number of users, and how many you're looking up. For example, it's probably not worth reading a 50,000-user remote NIS database to find 50 users, but it may be worth reading a 50,000-user /etc/passwd for 50 users.

    As usual, the best overall answer is try a few things and benchmark.

Re: Re: Quick User Existance Check?
by tilly (Archbishop) on Jun 26, 2003 at 06:11 UTC
    The general tone of responses is based on an educated guess of real needs judging from past experience with other people asking those questions.

    As for the past experience with people trying to speed things up, well the following essay on code tuning is exactly accurate. Yes, there are times that you need to tune code. I have been there, and done that. But most of the people who think they need that are just shooting themselves in the foot. What they really need to do is learn to write clearer code, get it working, and then be able to step back and do the really valuable kinds of analysis on the overall project.

    The image that comes to mind is someone who is so focussed on the need to be moving really fast that they are tripping over their own two feet. Get them to focus on effective walking motions, and they get farther immediately, plus are going to be in a better position to learn how to run later...

      I can see your point Tilly, I guess I did not even think about that viewpoint when I posted because I have dealt with the exact same speed issue the person was posting about before. Where a batch check of many names to a silly large /etc/passwd file was taking way to long using getpwnam(). In my instance it changed my average runtime from 14 minutes down to <15 seconds to use my method instead of getpwnam()

      BTW, it is great having you back! I missed your insightful posts while you were kept from us.

      -Waswas
        From the description it could well be a justified speedup to look for. People tend to have knee-jerk responses, and sometimes those responses are inappropriate. With 500,000 users on the system, scanning it each time could quickly become prohibitive.

        About being back. I also like being back. Interacting here forces me to constantly rethink what I think I know, and exposes me to things that I didn't know. This is very valuable for me. (Not to mention fun!) Plus there are all of the people I know and enjoy talking to...

Re: Re: Quick User Existance Check?
by Anonymous Monk on Jun 25, 2003 at 19:58 UTC
    Hi There, I indeed do have a finished app, but am concerned about how long it is going to take for my script to detect if a user exists on the system when there are 500,000+ users on it.

    On a seperate note, how long would you say that 500,000 lines of usernames in a file would take to search?
      If it is a local password file you will run into about the same issue I described above where the search can take like 10 seconds per name with the std lib getpwnam. building a hash will take about that same amount of time but the time will be preloaded -- meaning getpwnam of 1000 users against a 500,000 user /etc/passwd file you get 1000 x N (N = number of secs to search the file) hash method you get N + (Y x 1000) where Y is the time it takes to lookup a hash entry in perl (not very long)

      -Waswas