Skip to content

Commit

Permalink
Revert "Remove cache of fields to improve perf"
Browse files Browse the repository at this point in the history
This reverts commit c5793a2.
  • Loading branch information
sfc-gh-jopel committed Nov 20, 2024
1 parent c5793a2 commit 335a46b
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 34 deletions.
3 changes: 0 additions & 3 deletions scripts/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ def inline_init() -> str:
function_definition = function_definition.splitlines()[1:]
function_definition = "\n".join(function_definition)
function_definition = dedent(function_definition)
# If __init__ is empty, return an empty string
if function_definition == "pass":
return ""
return function_definition

# Add a presence check to a function definition
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(
resource_logs: List[ResourceLogs] = None,
):
self._resource_logs: List[ResourceLogs] = resource_logs
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand Down Expand Up @@ -58,6 +59,7 @@ def __init__(
partial_success: ExportLogsPartialSuccess = None,
):
self._partial_success: ExportLogsPartialSuccess = partial_success
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand Down Expand Up @@ -87,13 +89,15 @@ def __init__(
):
self.rejected_log_records: int = rejected_log_records
self.error_message: str = error_message
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
if self.rejected_log_records:
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 @@ -102,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 @@ -26,6 +26,7 @@ def __init__(
resource_metrics: List[ResourceMetrics] = None,
):
self._resource_metrics: List[ResourceMetrics] = resource_metrics
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand Down Expand Up @@ -58,6 +59,7 @@ def __init__(
partial_success: ExportMetricsPartialSuccess = None,
):
self._partial_success: ExportMetricsPartialSuccess = partial_success
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand Down Expand Up @@ -87,13 +89,15 @@ def __init__(
):
self.rejected_data_points: int = rejected_data_points
self.error_message: str = error_message
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
if self.rejected_data_points:
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 @@ -102,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 @@ -26,6 +26,7 @@ def __init__(
resource_spans: List[ResourceSpans] = None,
):
self._resource_spans: List[ResourceSpans] = resource_spans
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand Down Expand Up @@ -58,6 +59,7 @@ def __init__(
partial_success: ExportTracePartialSuccess = None,
):
self._partial_success: ExportTracePartialSuccess = partial_success
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand Down Expand Up @@ -87,13 +89,15 @@ def __init__(
):
self.rejected_spans: int = rejected_spans
self.error_message: str = error_message
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
if self.rejected_spans:
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 @@ -102,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 @@ -50,11 +50,13 @@ def __init__(
self._array_value: ArrayValue = array_value
self._kvlist_value: KeyValueList = kvlist_value
self.bytes_value: bytes = bytes_value
self._marshaler_cache = {}

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 @@ -84,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 @@ -123,6 +125,7 @@ def __init__(
values: List[AnyValue] = None,
):
self._values: List[AnyValue] = values
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand Down Expand Up @@ -155,6 +158,7 @@ def __init__(
values: List[KeyValue] = None,
):
self._values: List[KeyValue] = values
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand Down Expand Up @@ -191,11 +195,13 @@ def __init__(
):
self.key: str = key
self._value: AnyValue = value
self._marshaler_cache = {}

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 @@ -207,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 @@ -240,14 +246,17 @@ def __init__(
self.version: str = version
self._attributes: List[KeyValue] = attributes
self.dropped_attributes_count: int = dropped_attributes_count
self._marshaler_cache = {}

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 @@ -262,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 @@ -60,6 +60,7 @@ def __init__(
resource_logs: List[ResourceLogs] = None,
):
self._resource_logs: List[ResourceLogs] = resource_logs
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand Down Expand Up @@ -104,6 +105,7 @@ def __init__(
self._resource: Resource = resource
self._scope_logs: List[ScopeLogs] = scope_logs
self.schema_url: str = schema_url
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand All @@ -122,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 @@ -136,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 @@ -166,6 +169,7 @@ def __init__(
self._scope: InstrumentationScope = scope
self._log_records: List[LogRecord] = log_records
self.schema_url: str = schema_url
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand All @@ -184,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 @@ -198,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 @@ -250,6 +255,7 @@ def __init__(
self.trace_id: bytes = trace_id
self.span_id: bytes = span_id
self.observed_time_unix_nano: int = observed_time_unix_nano
self._marshaler_cache = {}

def calculate_size(self) -> int:
size = 0
Expand All @@ -262,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 @@ -307,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
Loading

0 comments on commit 335a46b

Please sign in to comment.