From b54c1273728e73bbec02f3300552151ce9ce72d1 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Tue, 24 Dec 2019 14:48:49 +1000 Subject: [PATCH] switch to Google's (C++) Protobuf implementation Brings the 100 field test down from 3 secs to 0.15 secs. "betterproto" indeed! --- .gitignore | 2 +- Makefile | 2 +- anki/rsbridge.py | 22 +++++++++++----------- requirements.txt | 3 ++- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index d82845757..37d4319f9 100644 --- a/.gitignore +++ b/.gitignore @@ -10,7 +10,7 @@ .pytype __pycache__ anki/buildhash.py -anki/proto +anki/bridge_pb2.py aqt/forms locale rs/ankirs/src/proto.rs diff --git a/Makefile b/Makefile index ed9edf687..79d4aab0d 100644 --- a/Makefile +++ b/Makefile @@ -130,7 +130,7 @@ BUILDDEPS := .build/ui .build/js .build/rs .build/py-proto @touch $@ .build/py-proto: $(RUNREQS) $(PROTODEPS) - protoc -I proto --python_betterproto_out=anki/proto proto/bridge.proto + protoc --proto_path=proto --python_out=anki proto/bridge.proto @touch $@ .PHONY: build clean diff --git a/anki/rsbridge.py b/anki/rsbridge.py index e7e4574c7..9e294c4f8 100644 --- a/anki/rsbridge.py +++ b/anki/rsbridge.py @@ -1,9 +1,8 @@ from typing import Dict, List import _ankirs # pytype: disable=import-error -import betterproto -from anki.proto import proto as pb +import anki.bridge_pb2 as pb from .types import AllTemplateReqs @@ -11,13 +10,13 @@ from .types import AllTemplateReqs class BridgeException(Exception): def __str__(self) -> str: err: pb.BridgeError = self.args[0] # pylint: disable=unsubscriptable-object - (kind, obj) = betterproto.which_one_of(err, "value") + kind = err.WhichOneof("value") if kind == "invalid_input": - return f"invalid input: {obj.info}" + return f"invalid input: {err.invalid_input.info}" elif kind == "template_parse": - return f"template parse: {obj.info}" + return f"template parse: {err.template_parse.info}" else: - return f"unhandled error: {err} {obj}" + return f"unhandled error: {err}" def proto_template_reqs_to_legacy( @@ -25,7 +24,7 @@ def proto_template_reqs_to_legacy( ) -> AllTemplateReqs: legacy_reqs = [] for (idx, req) in enumerate(reqs): - (kind, val) = betterproto.which_one_of(req, "value") + kind = req.WhichOneof("value") # fixme: sorting is for the unit tests - should check if any # code depends on the order if kind == "any": @@ -43,12 +42,13 @@ class RSBridge: self._bridge = _ankirs.Bridge() def _run_command(self, input: pb.BridgeInput) -> pb.BridgeOutput: - input_bytes = bytes(input) + input_bytes = input.SerializeToString() output_bytes = self._bridge.command(input_bytes) - output = pb.BridgeOutput().parse(output_bytes) - (kind, obj) = betterproto.which_one_of(output, "value") + output = pb.BridgeOutput() + output.ParseFromString(output_bytes) + kind = output.WhichOneof("value") if kind == "error": - raise BridgeException(obj) + raise BridgeException(output.error) else: return output diff --git a/requirements.txt b/requirements.txt index 4788d0bb2..b045feb3a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,4 +8,5 @@ jsonschema psutil; sys_platform == "win32" distro; sys_platform != "win32" and sys_platform != "darwin" typing -betterproto[compiler] +protobuf +