diff --git a/.github/workflows/perf.yml b/.github/workflows/perf.yml new file mode 100644 index 000000000..30ac5570c --- /dev/null +++ b/.github/workflows/perf.yml @@ -0,0 +1,50 @@ +name: Run Performance tests + +on: [pull_request] + +jobs: + bench: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v2 + + - name: Set up Python ${{ matrix.python-version }} + uses: actions/setup-python@v2 + + - name: Get full Python version + id: full-python-version + shell: bash + run: echo ::set-output name=version::$(python -c "import sys; print('-'.join(str(v) for v in sys.version_info))") + + - name: Install poetry + shell: bash + run: | + python -m pip install poetry + echo "$HOME/.poetry/bin" >> $GITHUB_PATH + + - name: Configure poetry + shell: bash + run: poetry config virtualenvs.in-project true + + - name: Set up cache + uses: actions/cache@v2 + id: cache + with: + path: .venv + key: venv-${{ runner.os }}-${{ steps.full-python-version.outputs.version }}-${{ hashFiles('**/poetry.lock') }} + + - name: Ensure cache is healthy + if: steps.cache.outputs.cache-hit == 'true' + shell: bash + run: poetry run pip --version >/dev/null 2>&1 || rm -rf .venv + + - name: Install dependencies + shell: bash + run: | + poetry run python -m pip install pip -U + poetry install + + - name: Run benchmarks + run: + poetry run python -m asv continuous master ${{ github.ref }} diff --git a/src/betterproto/__init__.py b/src/betterproto/__init__.py index 3cee8887e..f562b664c 100644 --- a/src/betterproto/__init__.py +++ b/src/betterproto/__init__.py @@ -724,11 +724,13 @@ def _betterproto(self) -> ProtoClassMetadata: It may be initialized multiple times in a multi-threaded environment, but that won't affect the correctness. """ - meta = getattr(self.__class__, "_betterproto_meta", None) - if not meta: + cls = self.__class__ + try: + return cls._betterproto_meta + except AttributeError: meta = ProtoClassMetadata(self.__class__) - self.__class__._betterproto_meta = meta # type: ignore - return meta + cls._betterproto_meta = meta # type: ignore + return meta def __bytes__(self) -> bytes: """ @@ -846,7 +848,7 @@ def _type_hint(cls, field_name: str) -> Type: @classmethod def _type_hints(cls) -> Dict[str, Type]: module = sys.modules[cls.__module__] - return get_type_hints(cls, vars(module)) + return get_type_hints(cls, module.__dict__, {}) @classmethod def _cls_for(cls, field: dataclasses.Field, index: int = 0) -> Type: