Closed Bug 1008943 Opened 10 years ago Closed 10 years ago

mozinfo should be cheaper to import

Categories

(Testing :: Mozbase, defect)

defect
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: ahal, Assigned: parkouss)

Details

Attachments

(2 files, 1 obsolete file)

On Windows, switching mozprocess to use platform instead of mozinfo reduced the build time by 2 minutes:
https://bugzilla.mozilla.org/show_bug.cgi?id=933558#c24

This is because we were spawning thousands of subprocesses per build, each of which are importing a lot of unnecessary libraries and doing unnecessary computation. A profile showed that importing mozfile (and subsequently urllib2) was taking up the majority of this time.

In general it is good to have imports at the top, but for small utility modules like this where performance is a real concern, I think we should do as little processing at import time as possible. Instead we should lazy load all mozinfo attributes. In order to accomplish this without breaking the API, I think we need to have a class instance disguised as a module. For example, see the accepted answer in:
http://stackoverflow.com/questions/1462986/lazy-module-variables-can-it-be-done
It seems like it's mozfile (imported in mozinfo) that does all the heavy imports (tarfile, zipfile, urllib2, ...), which is only used in mozinfo for its "load" function (that uses urllib2, and none of the other libs). So maybe we should make that lazy instead.

We could just put the import statements inside the functions that use them, that should do the trick.

Otherwise if you'd still like to make the other vars lazy, we'd have to do some refactoring because of the way those variables are generated ("dynamically" generated by adding values to globals()).

btw, what do you think about implementing it this way instead?

http://stackoverflow.com/a/880751/1334771
Flags: needinfo?(ahalberstadt)
Attached patch lazy-mozfile.patch (obsolete) — Splinter Review
Lazy imports in mozfile
Assignee: nobody → akachkach
Attachment #8421420 - Flags: review?(ahalberstadt)
Comment on attachment 8421420 [details] [diff] [review]
lazy-mozfile.patch

Review of attachment 8421420 [details] [diff] [review]:
-----------------------------------------------------------------

Thanks! Let's just take this for now since we have it, but I think we should leave the bug open to do lazy loading for the rest of mozinfo.

Though please add a comment to the top of the module explaining why we don't import everything at the top with a reference to this bug number.
Attachment #8421420 - Flags: review?(ahalberstadt) → review+
carry forward r+
Attachment #8421420 - Attachment is obsolete: true
Attachment #8421880 - Flags: review+
Keywords: checkin-needed
Whiteboard: [leave-open]
We're going to take this for now because it will help, but I think it would still be good to do lazy loading in mozinfo in general. Probably not a very high priority though.
Flags: needinfo?(ahalberstadt)
Keywords: checkin-needed
Whiteboard: [leave-open]
Keywords: checkin-needed
Whiteboard: [leave-open]
Assignee: akachkach → nobody
Assignee: nobody → j.parkouss
I have moved some import statements to be lazier.

I ran mozinfo unit tests with success locally.
Attachment #8507770 - Flags: review?(ahalberstadt)
Comment on attachment 8507770 [details] [diff] [review]
mozinfo should be cheaper to import

Review of attachment 8507770 [details] [diff] [review]:
-----------------------------------------------------------------

Thanks, this looks reasonable to me! Though I'm not sure how much of an improvement lazy loading json, errno and ctypes is since those are system modules. But oh well, doesn't hurt I guess.
Attachment #8507770 - Flags: review?(ahalberstadt) → review+
Sigh, ignore my last comment, I'm tired :).. Unconditional r+.
Keywords: checkin-needed
I'm closing it because I believe there is no need to keep the [leave-open] whiteboard entry no that the work is done. Please tell me If I done something wrong here.
Status: NEW → RESOLVED
Closed: 10 years ago
Resolution: --- → FIXED
Whiteboard: [leave-open]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: