Skip to content

Commit

Permalink
fix: pickleability of CompilerData (vyperlang#3803)
Browse files Browse the repository at this point in the history
InputBundle has the `_cache` attribute, which was implemented using
an anonymous function, which is not pickleable. Change `_cache` to use a
dedicated object type.

also refactors `timeit()` to be a contextmanager instead of just a
function decorator.
  • Loading branch information
charles-cooper authored Feb 22, 2024
1 parent f8edd29 commit 977ad79
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
11 changes: 8 additions & 3 deletions vyper/compiler/input_bundle.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class _NotFound(Exception):
pass


# an opaque object which consumers can get/set attributes on
class _Cache(object):
pass


# an "input bundle" to the compiler, representing the files which are
# available to the compiler. it is useful because it parametrizes I/O
# operations over different possible input types. you can think of it
Expand All @@ -66,9 +71,9 @@ def __init__(self, search_paths):
self._source_id_counter = 0
self._source_ids: dict[PathLike, int] = {}

# this is a little bit cursed, but it allows consumers to cache data that
# share the same lifetime as this input bundle.
self._cache = lambda: None
# this is a little bit cursed, but it allows consumers to cache data
# that share the same lifetime as this input bundle.
self._cache = _Cache()

def _normalize_path(self, path):
raise NotImplementedError(f"not implemented! {self.__class__}._normalize_path()")
Expand Down
19 changes: 7 additions & 12 deletions vyper/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import contextlib
import decimal
import enum
import functools
import sys
import time
import traceback
Expand Down Expand Up @@ -435,17 +434,13 @@ def indent(text: str, indent_chars: Union[str, List[str]] = " ", level: int = 1)
return "".join(indented_lines)


def timeit(func):
@functools.wraps(func)
def timeit_wrapper(*args, **kwargs):
start_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()
total_time = end_time - start_time
print(f"Function {func.__name__} Took {total_time:.4f} seconds")
return result

return timeit_wrapper
@contextlib.contextmanager
def timeit(msg):
start_time = time.perf_counter()
yield
end_time = time.perf_counter()
total_time = end_time - start_time
print(f"{msg}: Took {total_time:.4f} seconds")


@contextlib.contextmanager
Expand Down

0 comments on commit 977ad79

Please sign in to comment.