<br><br><div class="gmail_quote">On Fri, Feb 10, 2012 at 15:07, PJ Eby <span dir="ltr">&lt;<a href="mailto:pje@telecommunity.com">pje@telecommunity.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="gmail_quote"><div class="im">On Fri, Feb 10, 2012 at 1:05 PM, Brett Cannon <span dir="ltr">&lt;<a href="mailto:brett@python.org" target="_blank">brett@python.org</a>&gt;</span> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<br><br><div class="gmail_quote"><div><div class="im">On Thu, Feb 9, 2012 at 17:00, PJ Eby <span dir="ltr">&lt;<a href="mailto:pje@telecommunity.com" target="_blank">pje@telecommunity.com</a>&gt;</span> wrote:<br></div><div class="im">

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div class="gmail_quote"><div>I did some crude timeit tests on frozenset(listdir()) and trapping failed stat calls.  It looks like, for a Windows directory the size of the 2.7 stdlib, you need about four *failed* import attempts to overcome the initial caching cost, or about 8 successful bytecode imports.  (For Linux, you might need to double these numbers; my tests showed a different ratio there, perhaps due to the Linux stdib I tested having nearly twice as many directory entries as the directory I tested on Windows!)</div>


</div></blockquote></div></div><div class="im"><div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_quote">


<div><br></div><div>However, the numbers are much better for application directories than for the stdlib, since they are located earlier on sys.path.  Every successful stdlib import in an application is equal to one failed import attempt for every preceding directory on sys.path, so as long as the average directory on sys.path isn&#39;t vastly larger than the stdlib, and the average application imports at least four modules from the stdlib (on Windows, or 8 on Linux), there would be a net performance gain for the application as a whole.  (That is, there&#39;d be an improved per-sys.path entry import time for stdlib modules, even if not for any application modules.)</div>




</div></blockquote><div><br></div></div><div>Does this comment take into account the number of modules required to load the interpreter to begin with? That&#39;s already like 48 modules loaded by Python 3.2 as it is.</div>


</div></div></blockquote><div><br></div><div>I didn&#39;t count those, no.  So, if they&#39;re loaded from disk *after* importlib is initialized, then they should pay off the cost of caching even fairly large directories that appear earlier on sys.path than the stdlib.  We still need to know about NFS and other ratios, though...  I still worry that people with more extreme directory sizes or slow-access situations will run into even worse trouble than they have now.</div>

</div></blockquote><div><br></div><div>It&#39;s possible. No way to make it work for everyone. This is why I didn&#39;t worry about some crazy perf optimization.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="gmail_quote"><div class="im">
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_quote"><div>First is that if this were used on Windows or OS X (i.e. the OSs we support that typically have case-insensitive filesystems), then this approach would be a massive gain as we already call os.listdir() when PYTHONCASEOK isn&#39;t defined to check case-sensitivity; take your 5 stat calls and add in 5 listdir() calls and that&#39;s what you get on Windows and OS X right now. Linux doesn&#39;t have this check so you would still be potentially paying a penalty there.</div>


</div></blockquote><div><br></div></div><div>Wow.  That means it&#39;d always be a win for pre-stdlib sys.path entries, because any successful stdlib import equals a failed pre-stdlib lookup.  (Of course, that&#39;s just saving some of the overhead that&#39;s been *added* by importlib, not a new gain, but still...)</div>

</div></blockquote><div><br></div><div>How so? import.c does a listdir() as well (this is not special to importlib).</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div class="gmail_quote"><div class="im">
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_quote"><div>Second is variance in filesystems. Are we guaranteed that the stat of a directory is updated before a file change is made?</div>


</div></blockquote><div><br></div></div><div>Not quite sure what you mean here.  The directory stat is used to ensure that new files haven&#39;t been added, old ones removed, or existing ones renamed.  Changes to the files themselves shouldn&#39;t factor in, should they?</div>

</div></blockquote><div><br></div><div>Changes in any fashion to the directory. Do filesystems atomically update the mtime of a directory when they commit a change? Otherwise we have a potential race condition.</div><div>

 </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_quote"><div class="im">
<div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_quote"><div> Else there is a small race condition there which would suck. We also have the issue of granularity; Antoine has already had to add the source file size to .pyc files in Python 3.3 to combat crappy mtime granularity when generating bytecode. If we get file mod -&gt; import -&gt; file mod -&gt; import, are we guaranteed that the second import will know there was a modification if the first three steps occur fast enough to fit within the granularity of an mtime value?</div>


</div></blockquote><div><br></div></div><div>Again, I&#39;m not sure how this relates.  Automatic code reloaders monitor individual files that have been previously imported, so the directory timestamps aren&#39;t relevant.</div>


<div><br></div></div></blockquote><div><br></div><div>Don&#39;t care about automatic reloaders. I&#39;m just asking about the case where the mtime granularity is coarse enough to allow for a directory change, an import to execute, and then another directory change to occur all within a single mtime increment. That would lead to the set cache to be out of date.</div>

<div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_quote"><div></div><div>Of course, I could be confused here.  Are you saying that if somebody makes a new .py file and saves it, that it&#39;ll be possible to import it before it&#39;s finished being written?  If so, that could happen already, and again caching the directory doesn&#39;t make any difference.</div>


<div><br></div><div>Alternately, you could have a situation where the file is deleted after we load the listdir(), but in that case the open will fail and we can fall back...  heck, we can even force resetting the cache in that event.</div>

<div class="im">
<div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="gmail_quote"><div>I was going to say something about __pycache__, but it actually doesn&#39;t affect this. Since you would have to stat the directory anyway, you might as well just stat directory for the file you want to keep it simple. Only if you consider __pycache__ to be immutable except for what the interpreter puts in that directory during execution could you optimize that step (in which case you can stat the directory once and never care again as the set would be just updated by import whenever a new .pyc file was written).</div>




<div><br></div><div>Having said all of this, implementing this idea would be trivial using importlib if you don&#39;t try to optimize the __pycache__ case. It&#39;s just a question of whether people are comfortable with the semantic change to import. This could also be made into something that was in importlib for people to use when desired if we are too worried about semantic changes.</div>




</div>
</blockquote></div></div><br><div>Yep.  I was actually thinking this could be backported to 2.x, even without importlib, as a module to be imported in sitecustomize or via a .pth file.  All it needs is a path hook, after all, and a subclass of the pkgutil importer to test it.  And if we can get some people with huge NFS libraries and/or zillions of .egg directories on sys.path to test it, we could find out whether it&#39;s a win, lose, or draw for those scenarios.</div>

</blockquote><div><br></div><div>You can do that if you want, obviously I don&#39;t want to bother since it won&#39;t make it into Python 2.7. </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

<div> </div></blockquote></div>