Skip to content

Commit

Permalink
Add string encode caching
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jopel committed Nov 20, 2024
1 parent b67548b commit 8f35616
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def calculate_size(self) -> int:
size += len(b"\x08") + Varint.size_varint_i64(self.rejected_log_records)
if self.error_message:
v = self.error_message.encode("utf-8")
self._marshaler_cache[b"\x12"] = v
size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v)
return size

Expand All @@ -105,7 +106,7 @@ def write_to(self, out: bytearray) -> None:
out += b"\x08"
Varint.write_varint_i64(out, self.rejected_log_records)
if self.error_message:
v = self.error_message.encode("utf-8")
v = self._marshaler_cache[b"\x12"]
out += b"\x12"
Varint.write_varint_u32(out, len(v))
out += v
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def calculate_size(self) -> int:
size += len(b"\x08") + Varint.size_varint_i64(self.rejected_data_points)
if self.error_message:
v = self.error_message.encode("utf-8")
self._marshaler_cache[b"\x12"] = v
size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v)
return size

Expand All @@ -105,7 +106,7 @@ def write_to(self, out: bytearray) -> None:
out += b"\x08"
Varint.write_varint_i64(out, self.rejected_data_points)
if self.error_message:
v = self.error_message.encode("utf-8")
v = self._marshaler_cache[b"\x12"]
out += b"\x12"
Varint.write_varint_u32(out, len(v))
out += v
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def calculate_size(self) -> int:
size += len(b"\x08") + Varint.size_varint_i64(self.rejected_spans)
if self.error_message:
v = self.error_message.encode("utf-8")
self._marshaler_cache[b"\x12"] = v
size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v)
return size

Expand All @@ -105,7 +106,7 @@ def write_to(self, out: bytearray) -> None:
out += b"\x08"
Varint.write_varint_i64(out, self.rejected_spans)
if self.error_message:
v = self.error_message.encode("utf-8")
v = self._marshaler_cache[b"\x12"]
out += b"\x12"
Varint.write_varint_u32(out, len(v))
out += v
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def calculate_size(self) -> int:
size = 0
if self.string_value is not None:
v = self.string_value.encode("utf-8")
self._marshaler_cache[b"\n"] = v
size += len(b"\n") + Varint.size_varint_u32(len(v)) + len(v)
if self.bool_value is not None:
size += len(b"\x10") + 1
Expand Down Expand Up @@ -85,7 +86,7 @@ def calculate_size(self) -> int:

def write_to(self, out: bytearray) -> None:
if self.string_value is not None:
v = self.string_value.encode("utf-8")
v = self._marshaler_cache[b"\n"]
out += b"\n"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down Expand Up @@ -200,6 +201,7 @@ def calculate_size(self) -> int:
size = 0
if self.key:
v = self.key.encode("utf-8")
self._marshaler_cache[b"\n"] = v
size += len(b"\n") + Varint.size_varint_u32(len(v)) + len(v)
if self._value is not None:
size += (
Expand All @@ -211,7 +213,7 @@ def calculate_size(self) -> int:

def write_to(self, out: bytearray) -> None:
if self.key:
v = self.key.encode("utf-8")
v = self._marshaler_cache[b"\n"]
out += b"\n"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down Expand Up @@ -250,9 +252,11 @@ def calculate_size(self) -> int:
size = 0
if self.name:
v = self.name.encode("utf-8")
self._marshaler_cache[b"\n"] = v
size += len(b"\n") + Varint.size_varint_u32(len(v)) + len(v)
if self.version:
v = self.version.encode("utf-8")
self._marshaler_cache[b"\x12"] = v
size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v)
if self._attributes:
size += sum(
Expand All @@ -267,12 +271,12 @@ def calculate_size(self) -> int:

def write_to(self, out: bytearray) -> None:
if self.name:
v = self.name.encode("utf-8")
v = self._marshaler_cache[b"\n"]
out += b"\n"
Varint.write_varint_u32(out, len(v))
out += v
if self.version:
v = self.version.encode("utf-8")
v = self._marshaler_cache[b"\x12"]
out += b"\x12"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def calculate_size(self) -> int:
)
if self.schema_url:
v = self.schema_url.encode("utf-8")
self._marshaler_cache[b"\x1a"] = v
size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v)
return size

Expand All @@ -138,7 +139,7 @@ def write_to(self, out: bytearray) -> None:
Varint.write_varint_u32(out, v._get_size())
v.write_to(out)
if self.schema_url:
v = self.schema_url.encode("utf-8")
v = self._marshaler_cache[b"\x1a"]
out += b"\x1a"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down Expand Up @@ -187,6 +188,7 @@ def calculate_size(self) -> int:
)
if self.schema_url:
v = self.schema_url.encode("utf-8")
self._marshaler_cache[b"\x1a"] = v
size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v)
return size

Expand All @@ -201,7 +203,7 @@ def write_to(self, out: bytearray) -> None:
Varint.write_varint_u32(out, v._get_size())
v.write_to(out)
if self.schema_url:
v = self.schema_url.encode("utf-8")
v = self._marshaler_cache[b"\x1a"]
out += b"\x1a"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down Expand Up @@ -266,6 +268,7 @@ def calculate_size(self) -> int:
size += len(b"\x10") + Varint.size_varint_u32(v)
if self.severity_text:
v = self.severity_text.encode("utf-8")
self._marshaler_cache[b"\x1a"] = v
size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v)
if self._body is not None:
size += (
Expand Down Expand Up @@ -311,7 +314,7 @@ def write_to(self, out: bytearray) -> None:
out += b"\x10"
Varint.write_varint_u32(out, v)
if self.severity_text:
v = self.severity_text.encode("utf-8")
v = self._marshaler_cache[b"\x1a"]
out += b"\x1a"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def calculate_size(self) -> int:
)
if self.schema_url:
v = self.schema_url.encode("utf-8")
self._marshaler_cache[b"\x1a"] = v
size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v)
return size

Expand All @@ -116,7 +117,7 @@ def write_to(self, out: bytearray) -> None:
Varint.write_varint_u32(out, v._get_size())
v.write_to(out)
if self.schema_url:
v = self.schema_url.encode("utf-8")
v = self._marshaler_cache[b"\x1a"]
out += b"\x1a"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down Expand Up @@ -165,6 +166,7 @@ def calculate_size(self) -> int:
)
if self.schema_url:
v = self.schema_url.encode("utf-8")
self._marshaler_cache[b"\x1a"] = v
size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v)
return size

Expand All @@ -179,7 +181,7 @@ def write_to(self, out: bytearray) -> None:
Varint.write_varint_u32(out, v._get_size())
v.write_to(out)
if self.schema_url:
v = self.schema_url.encode("utf-8")
v = self._marshaler_cache[b"\x1a"]
out += b"\x1a"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down Expand Up @@ -253,12 +255,15 @@ def calculate_size(self) -> int:
size = 0
if self.name:
v = self.name.encode("utf-8")
self._marshaler_cache[b"\n"] = v
size += len(b"\n") + Varint.size_varint_u32(len(v)) + len(v)
if self.description:
v = self.description.encode("utf-8")
self._marshaler_cache[b"\x12"] = v
size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v)
if self.unit:
v = self.unit.encode("utf-8")
self._marshaler_cache[b"\x1a"] = v
size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v)
if self._gauge is not None:
size += (
Expand Down Expand Up @@ -301,17 +306,17 @@ def calculate_size(self) -> int:

def write_to(self, out: bytearray) -> None:
if self.name:
v = self.name.encode("utf-8")
v = self._marshaler_cache[b"\n"]
out += b"\n"
Varint.write_varint_u32(out, len(v))
out += v
if self.description:
v = self.description.encode("utf-8")
v = self._marshaler_cache[b"\x12"]
out += b"\x12"
Varint.write_varint_u32(out, len(v))
out += v
if self.unit:
v = self.unit.encode("utf-8")
v = self._marshaler_cache[b"\x1a"]
out += b"\x1a"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ def calculate_size(self) -> int:
)
if self.schema_url:
v = self.schema_url.encode("utf-8")
self._marshaler_cache[b"\x1a"] = v
size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v)
return size

Expand All @@ -112,7 +113,7 @@ def write_to(self, out: bytearray) -> None:
Varint.write_varint_u32(out, v._get_size())
v.write_to(out)
if self.schema_url:
v = self.schema_url.encode("utf-8")
v = self._marshaler_cache[b"\x1a"]
out += b"\x1a"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down Expand Up @@ -161,6 +162,7 @@ def calculate_size(self) -> int:
)
if self.schema_url:
v = self.schema_url.encode("utf-8")
self._marshaler_cache[b"\x1a"] = v
size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v)
return size

Expand All @@ -175,7 +177,7 @@ def write_to(self, out: bytearray) -> None:
Varint.write_varint_u32(out, v._get_size())
v.write_to(out)
if self.schema_url:
v = self.schema_url.encode("utf-8")
v = self._marshaler_cache[b"\x1a"]
out += b"\x1a"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down Expand Up @@ -276,6 +278,7 @@ def calculate_size(self) -> int:
)
if self.trace_state:
v = self.trace_state.encode("utf-8")
self._marshaler_cache[b"\x1a"] = v
size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v)
if self.parent_span_id:
size += (
Expand All @@ -285,6 +288,7 @@ def calculate_size(self) -> int:
)
if self.name:
v = self.name.encode("utf-8")
self._marshaler_cache[b"*"] = v
size += len(b"*") + Varint.size_varint_u32(len(v)) + len(v)
if self.kind:
v = self.kind
Expand Down Expand Up @@ -342,7 +346,7 @@ def write_to(self, out: bytearray) -> None:
Varint.write_varint_u32(out, len(self.span_id))
out += self.span_id
if self.trace_state:
v = self.trace_state.encode("utf-8")
v = self._marshaler_cache[b"\x1a"]
out += b"\x1a"
Varint.write_varint_u32(out, len(v))
out += v
Expand All @@ -351,7 +355,7 @@ def write_to(self, out: bytearray) -> None:
Varint.write_varint_u32(out, len(self.parent_span_id))
out += self.parent_span_id
if self.name:
v = self.name.encode("utf-8")
v = self._marshaler_cache[b"*"]
out += b"*"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down Expand Up @@ -438,6 +442,7 @@ def calculate_size(self) -> int:
size += len(b"\t") + 8
if self.name:
v = self.name.encode("utf-8")
self._marshaler_cache[b"\x12"] = v
size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v)
if self._attributes:
size += sum(
Expand All @@ -457,7 +462,7 @@ def write_to(self, out: bytearray) -> None:
out += b"\t"
out += struct.pack("<Q", self.time_unix_nano)
if self.name:
v = self.name.encode("utf-8")
v = self._marshaler_cache[b"\x12"]
out += b"\x12"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down Expand Up @@ -517,6 +522,7 @@ def calculate_size(self) -> int:
)
if self.trace_state:
v = self.trace_state.encode("utf-8")
self._marshaler_cache[b"\x1a"] = v
size += len(b"\x1a") + Varint.size_varint_u32(len(v)) + len(v)
if self._attributes:
size += sum(
Expand All @@ -543,7 +549,7 @@ def write_to(self, out: bytearray) -> None:
Varint.write_varint_u32(out, len(self.span_id))
out += self.span_id
if self.trace_state:
v = self.trace_state.encode("utf-8")
v = self._marshaler_cache[b"\x1a"]
out += b"\x1a"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down Expand Up @@ -577,6 +583,7 @@ def calculate_size(self) -> int:
size = 0
if self.message:
v = self.message.encode("utf-8")
self._marshaler_cache[b"\x12"] = v
size += len(b"\x12") + Varint.size_varint_u32(len(v)) + len(v)
if self.code:
v = self.code
Expand All @@ -587,7 +594,7 @@ def calculate_size(self) -> int:

def write_to(self, out: bytearray) -> None:
if self.message:
v = self.message.encode("utf-8")
v = self._marshaler_cache[b"\x12"]
out += b"\x12"
Varint.write_varint_u32(out, len(v))
out += v
Expand Down
5 changes: 4 additions & 1 deletion src/snowflake/telemetry/_internal/serialize/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ def size_sfixed64(self, TAG: bytes, FIELD_ATTR: int) -> int:
def size_bytes(self, TAG: bytes, FIELD_ATTR: bytes) -> int:
return len(TAG) + Varint.size_varint_u32(len(FIELD_ATTR)) + len(FIELD_ATTR)

# This function should not be used for repeated strings due to caching by tag
def size_string(self, TAG: bytes, FIELD_ATTR: str) -> int:
v = FIELD_ATTR.encode("utf-8")
self._marshaler_cache[TAG] = v
return len(TAG) + Varint.size_varint_u32(len(v)) + len(v)

def size_message(self, TAG: bytes, FIELD_ATTR: MessageMarshaler) -> int:
Expand Down Expand Up @@ -252,8 +254,9 @@ def serialize_bytes(self, out: bytearray, TAG: bytes, FIELD_ATTR: bytes) -> None
Varint.write_varint_u32(out, len(FIELD_ATTR))
out += FIELD_ATTR

# This function should not be used for repeated strings due to caching by tag
def serialize_string(self, out: bytearray, TAG: bytes, FIELD_ATTR: str) -> None:
v = FIELD_ATTR.encode("utf-8")
v = self._marshaler_cache[TAG]
out += TAG
Varint.write_varint_u32(out, len(v))
out += v
Expand Down

0 comments on commit 8f35616

Please sign in to comment.