Skip to content

Commit

Permalink
Lazy load unnecessary modules to improve start speed (#525)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaogaotiantian authored Nov 19, 2024
1 parent d8df684 commit 3eac6ab
Show file tree
Hide file tree
Showing 28 changed files with 82 additions and 45 deletions.
3 changes: 2 additions & 1 deletion docs/source/custom_event.rst
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ Custom Event

.. code-block:: python
from viztracer import VizTracer, VizCounter
from viztracer import VizTracer
from viztracer.vizcounter import VizCounter
tracer = VizTracer()
tracer.start()
counter = VizCounter(tracer, "counter name")
Expand Down
3 changes: 2 additions & 1 deletion docs/source/extra_log.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ data as Instant Events.

.. code-block:: python
from viztracer import VizTracer, VizLoggingHandler
from viztracer import VizTracer
from viztracer.vizlogging import VizLoggingHandler
tracer = VizTracer()
handler = VizLoggingHandler()
Expand Down
6 changes: 4 additions & 2 deletions docs/source/global_tracer.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ You can do things like:

.. code-block:: python
from viztracer import VizLoggingHandler, get_tracer
from viztracer import get_tracer
from viztracer.vizlogging import VizLoggingHandler
handler = VizLoggingHandler()
handler.setTracer(get_tracer())
.. code-block:: python
from viztracer import get_tracer, VizObject
from viztracer import get_tracer
from viztracer.vizobject import VizObject
obj = VizObject(get_tracer(), "my variable")
2 changes: 1 addition & 1 deletion example/json/async_simple.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/json/different_sorts.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/json/function_args_return.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/json/gradient_descent.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/json/logging_integration.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/json/mcts_game.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/json/multi_process_pool.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion example/json/multithread.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion example/src/gradient_descent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import numpy

from viztracer import VizCounter, VizTracer
from viztracer import VizTracer
from viztracer.vizcounter import VizCounter

# List of input, output pairs
train_data = (
Expand Down
3 changes: 2 additions & 1 deletion example/src/logging_integration.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging

from viztracer import VizLoggingHandler, get_tracer
from viztracer import get_tracer
from viztracer.vizlogging import VizLoggingHandler


def fib(n):
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Documentation = "https://viztracer.readthedocs.io"
full = ["orjson"]

[project.scripts]
viztracer = "viztracer:main"
vizviewer = "viztracer:viewer_main"
viztracer = "viztracer.main:main"
vizviewer = "viztracer.viewer:viewer_main"

[tool.setuptools.dynamic]
version = {attr = "viztracer.__version__"}
10 changes: 0 additions & 10 deletions src/viztracer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,14 @@

from .cellmagic import load_ipython_extension
from .decorator import ignore_function, log_sparse, trace_and_save
from .main import main
from .viewer import viewer_main
from .vizcounter import VizCounter
from .vizlogging import VizLoggingHandler
from .vizobject import VizObject
from .viztracer import VizTracer, get_tracer

__all__ = [
"__version__",
"main",
"viewer_main",
"VizTracer",
"ignore_function",
"trace_and_save",
"log_sparse",
"get_tracer",
"VizCounter",
"VizObject",
"VizLoggingHandler",
"load_ipython_extension",
]
7 changes: 5 additions & 2 deletions src/viztracer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import atexit
import base64
import builtins
import configparser
import io
import json
import multiprocessing.util # type: ignore
Expand All @@ -23,7 +22,6 @@
from typing import Any, Optional, Union

from . import __version__
from .attach_process.add_code_to_python_process import run_python_code # type: ignore
from .code_monkey import CodeMonkey
from .patch import install_all_hooks
from .report_builder import ReportBuilder
Expand Down Expand Up @@ -166,6 +164,7 @@ def create_parser(self) -> argparse.ArgumentParser:
def load_config_file(self, filename: str = ".viztracerrc") -> argparse.Namespace:
ret = argparse.Namespace()
if os.path.exists(filename):
import configparser
cfg_parser = configparser.ConfigParser()
cfg_parser.read(filename)
if "default" not in cfg_parser:
Expand Down Expand Up @@ -531,6 +530,8 @@ def _check_attach_availability(self) -> tuple[bool, Optional[str]]:
return True, None

def attach(self) -> VizProcedureResult:
from .attach_process.add_code_to_python_process import run_python_code # type: ignore

pid = self.options.attach
interval = self.options.t

Expand Down Expand Up @@ -570,6 +571,8 @@ def attach(self) -> VizProcedureResult:
return True, None

def uninstall(self) -> VizProcedureResult:
from .attach_process.add_code_to_python_process import run_python_code # type: ignore

pid = self.options.uninstall

success, err_msg = self._check_attach_availability()
Expand Down
7 changes: 1 addition & 6 deletions src/viztracer/modules/snaptrace.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ PyObject* trio_lowlevel_module = NULL;
PyObject* sys_module = NULL;
PyObject* sys_monitoring_missing = NULL;

static PyObject* curr_task_getters[2] = {0};
PyObject* curr_task_getters[2] = {0};

// =============================================================================
// Utility function
Expand Down Expand Up @@ -2142,11 +2142,6 @@ PyInit_snaptrace(void)

threading_module = PyImport_ImportModule("threading");
multiprocessing_module = PyImport_ImportModule("multiprocessing");
asyncio_module = PyImport_ImportModule("asyncio");
asyncio_tasks_module = PyImport_AddModule("asyncio.tasks");
if (PyObject_HasAttrString(asyncio_tasks_module, "current_task")) {
curr_task_getters[0] = PyObject_GetAttrString(asyncio_tasks_module, "current_task");
}
if ((trio_module = PyImport_ImportModule("trio"))) {
trio_lowlevel_module = PyImport_AddModule("trio.lowlevel");
curr_task_getters[1] = PyObject_GetAttrString(trio_lowlevel_module, "current_task");
Expand Down
15 changes: 15 additions & 0 deletions src/viztracer/modules/snaptrace_member.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
// Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
// For details: https://github.com/gaogaotiantian/viztracer/blob/master/NOTICE.txt

#include "pythoncapi_compat.h"
#include "snaptrace.h"

extern PyObject* asyncio_module;
extern PyObject* asyncio_tasks_module;
extern PyObject* curr_task_getters[2];

// ================================================================
// Tracer members
// ================================================================
Expand Down Expand Up @@ -368,6 +375,14 @@ Tracer_log_async_setter(TracerObject* self, PyObject* value, void* closure)
}

if (value == Py_True) {
// Lazy import asyncio because it's slow
if (asyncio_module == NULL) {
asyncio_module = PyImport_ImportModule("asyncio");
asyncio_tasks_module = PyImport_AddModule("asyncio.tasks");
if (PyObject_HasAttrString(asyncio_tasks_module, "current_task")) {
curr_task_getters[0] = PyObject_GetAttrString(asyncio_tasks_module, "current_task");
}
}
SET_FLAG(self->check_flags, SNAPTRACE_LOG_ASYNC);
} else {
UNSET_FLAG(self->check_flags, SNAPTRACE_LOG_ASYNC);
Expand Down
Loading

0 comments on commit 3eac6ab

Please sign in to comment.