Esempio n. 1
0
def optimizeUncompiledPythonModule(module):
    full_name = module.getFullName()
    progress_logger.info(
        "Doing module dependency considerations for '{module_name}':".format(
            module_name=full_name
        )
    )

    for used_module_name, used_module_path in module.getUsedModules():
        used_module = ImportCache.getImportedModuleByNameAndPath(
            used_module_name, used_module_path
        )
        ModuleRegistry.addUsedModule(used_module)

    package_name = full_name.getPackageName()

    if package_name is not None:
        # TODO: It's unclear why, but some standard library modules on older Python3
        # seem to not have parent packages after the scan.
        try:
            used_module = ImportCache.getImportedModuleByName(package_name)
        except KeyError:
            pass
        else:
            ModuleRegistry.addUsedModule(used_module)

    Plugins.considerImplicitImports(module=module, signal_change=signalChange)
Esempio n. 2
0
def optimizeUncompiledPythonModule(module):
    full_name = module.getFullName()
    progress_logger.info(
        "Doing module dependency considerations for '{module_name}':".format(
            module_name=full_name))

    # Pick up parent package if any.
    module.attemptRecursion()

    considerUsedModules(module=module, signal_change=signalChange)

    Plugins.considerImplicitImports(module=module, signal_change=signalChange)
Esempio n. 3
0
def _traceProgress(current_module):
    output = """\
Optimizing module '{module_name}', {remaining:d} more modules to go \
after that.""".format(
        module_name=current_module.getFullName(),
        remaining=ModuleRegistry.remainingCount(),
    )
    progress_logger.info(output)

    if Options.isShowMemory():
        output = "Memory usage {memory}:".format(
            memory=MemoryUsage.getHumanReadableProcessMemoryUsage())

        memory_logger.info(output)
Esempio n. 4
0
def optimizeUncompiledPythonModule(module):
    full_name = module.getFullName()
    progress_logger.info(
        "Doing module dependency considerations for '{module_name}':".format(
            module_name=full_name))

    for used_module_name, used_module_path in module.getUsedModules():
        used_module = ImportCache.getImportedModuleByNameAndPath(
            used_module_name, used_module_path)
        ModuleRegistry.addUsedModule(used_module)

    package_name = full_name.getPackageName()

    if package_name is not None:
        used_module = ImportCache.getImportedModuleByName(package_name)
        ModuleRegistry.addUsedModule(used_module)

    Plugins.considerImplicitImports(module=module, signal_change=signalChange)
Esempio n. 5
0
def _traceProgress(current_module):
    if _progress:
        output = """\
Optimizing module '{module_name}', {remaining:d} more modules to go \
after that.""".format(
            module_name=current_module.getFullName(),
            remaining=ModuleRegistry.getRemainingModulesCount(),
        )
        progress_logger.info(output)

    reportProgressBar(
        stage="Optimization",
        unit=" modules",
        item=current_module.getFullName(),
        total=ModuleRegistry.getRemainingModulesCount() +
        ModuleRegistry.getDoneModulesCount(),
    )

    if _progress and Options.isShowMemory():
        output = "Memory usage {memory}:".format(
            memory=getHumanReadableProcessMemoryUsage())

        memory_logger.info(output)
Esempio n. 6
0
def optimizeCompiledPythonModule(module):
    if _progress:
        progress_logger.info(
            "Doing module local optimizations for '{module_name}'.".format(
                module_name=module.getFullName()))

    touched = False

    if _progress and Options.isShowMemory():
        memory_watch = MemoryUsage.MemoryWatch()

    while True:
        tag_set.clear()

        try:
            # print("Compute module")
            module.computeModule()
        except BaseException:
            general.info("Interrupted while working on '%s'." % module)
            raise

        Graphs.onModuleOptimizationStep(module)

        # Search for local change tags.
        for tag in tag_set:
            if tag == "new_code":
                continue

            break
        else:
            if _progress:
                progress_logger.info("Finished with the module.")
            break

        if _progress:
            if "new_code" in tag_set:
                tag_set.remove("new_code")

            progress_logger.info(
                "Not finished with the module due to following change kinds: %s"
                % ",".join(sorted(tag_set)))

        # Otherwise we did stuff, so note that for return value.
        touched = True

    if _progress and Options.isShowMemory():
        memory_watch.finish()

        memory_logger.info(
            "Memory usage changed during optimization of '%s': %s" %
            (module.getFullName(), memory_watch.asStr()))

    Plugins.considerImplicitImports(module=module, signal_change=signalChange)

    return touched
Esempio n. 7
0
def optimize(output_filename):
    Graphs.startGraph()

    # First pass.
    if _progress:
        progress_logger.info("PASS 1:")

    makeOptimizationPass()
    Variables.complete = True

    if _progress:
        progress_logger.info("PASS 2:")

    finished = makeOptimizationPass()

    if Options.isExperimental("check_xml_persistence"):
        _checkXMLPersistence()

    # Demote compiled modules to bytecode, now that imports had a chance to be resolved, and
    # dependencies were handled.
    for module in ModuleRegistry.getDoneModules():
        if (module.isCompiledPythonModule()
                and module.getCompilationMode() == "bytecode"):
            demoteCompiledModuleToBytecode(module)

    pass_count = 2
    # Second, "endless" pass.
    while not finished:
        pass_count += 1

        if _progress:
            progress_logger.info("PASS %d:" % pass_count)

        finished = makeOptimizationPass()

    Graphs.endGraph(output_filename)