Skip to content

Commit

Permalink
stop clone-ing serializers (#1402)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhewitt authored Aug 13, 2024
1 parent fd81a75 commit 7368c1f
Show file tree
Hide file tree
Showing 32 changed files with 62 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/serializers/computed_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::tools::SchemaDict;
use super::errors::py_err_se_err;
use super::Extra;

#[derive(Debug, Clone)]
#[derive(Debug)]
pub(super) struct ComputedFields(Vec<ComputedField>);

impl ComputedFields {
Expand Down Expand Up @@ -109,7 +109,7 @@ impl ComputedFields {
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
struct ComputedField {
property_name: String,
property_name_py: Py<PyString>,
Expand Down
4 changes: 2 additions & 2 deletions src/serializers/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::shared::PydanticSerializer;
use super::shared::{CombinedSerializer, TypeSerializer};

/// representation of a field for serialization
#[derive(Debug, Clone)]
#[derive(Debug)]
pub(super) struct SerField {
pub key_py: Py<PyString>,
pub alias: Option<String>,
Expand Down Expand Up @@ -93,7 +93,7 @@ pub(super) enum FieldsMode {
}

/// General purpose serializer for fields - used by dataclasses, models and typed_dicts
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct GeneralFieldsSerializer {
fields: AHashMap<String, SerField>,
computed_fields: Option<ComputedFields>,
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ pub(crate) fn infer_to_python_known(
ObType::Generator => {
let iter = super::type_serializers::generator::SerializationIterator::new(
value.downcast()?,
super::type_serializers::any::AnySerializer.into(),
super::type_serializers::any::AnySerializer::get(),
SchemaFilter::default(),
include,
exclude,
Expand Down
4 changes: 2 additions & 2 deletions src/serializers/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ macro_rules! combined_serializer {
find_only: {$($builder:path;)*}
both: {$($b_key:ident: $b_serializer:path;)*}
) => {
#[derive(Debug, Clone)]
#[derive(Debug)]
#[enum_dispatch]
pub enum CombinedSerializer {
$($e_key($e_serializer),)*
Expand Down Expand Up @@ -256,7 +256,7 @@ impl PyGcTraverse for CombinedSerializer {
}

#[enum_dispatch(CombinedSerializer)]
pub(crate) trait TypeSerializer: Send + Sync + Clone + Debug {
pub(crate) trait TypeSerializer: Send + Sync + Debug {
fn to_python(
&self,
value: &Bound<'_, PyAny>,
Expand Down
12 changes: 11 additions & 1 deletion src/serializers/type_serializers/any.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::borrow::Cow;
use std::{
borrow::Cow,
sync::{Arc, OnceLock},
};

use pyo3::prelude::*;
use pyo3::types::PyDict;
Expand All @@ -14,6 +17,13 @@ use super::{
#[derive(Debug, Clone, Default)]
pub struct AnySerializer;

impl AnySerializer {
pub fn get() -> &'static Arc<CombinedSerializer> {
static ANY_SERIALIZER: OnceLock<Arc<CombinedSerializer>> = OnceLock::new();
ANY_SERIALIZER.get_or_init(|| Arc::new(Self.into()))
}
}

impl BuildSerializer for AnySerializer {
const EXPECTED_TYPE: &'static str = "any";

Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{
TypeSerializer,
};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct BytesSerializer {
bytes_mode: BytesMode,
}
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/dataclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl BuildSerializer for DataclassArgsBuilder {
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct DataclassSerializer {
class: Py<PyType>,
serializer: Box<CombinedSerializer>,
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/datetime_etc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn downcast_date_reject_datetime<'a, 'py>(py_date: &'a Bound<'py, PyAny>) -> PyR

macro_rules! build_serializer {
($struct_name:ident, $expected_type:literal, $downcast:path, $convert_func:ident $(, $json_check_func:ident)?) => {
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct $struct_name;

impl BuildSerializer for $struct_name {
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{
infer_json_key, infer_serialize, infer_to_python, BuildSerializer, CombinedSerializer, Extra, TypeSerializer,
};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct DecimalSerializer {}

impl BuildSerializer for DecimalSerializer {
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::tools::SchemaDict;

use super::{py_err_se_err, BuildSerializer, CombinedSerializer, Extra, TypeSerializer};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct DefinitionsSerializerBuilder;

impl BuildSerializer for DefinitionsSerializerBuilder {
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::{
SchemaFilter, SerMode, TypeSerializer,
};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct DictSerializer {
key_serializer: Box<CombinedSerializer>,
value_serializer: Box<CombinedSerializer>,
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/enum_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::simple::IntSerializer;
use super::string::StrSerializer;
use super::{BuildSerializer, CombinedSerializer, Extra, TypeSerializer};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct EnumSerializer {
class: Py<PyType>,
serializer: Option<Box<CombinedSerializer>>,
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::{
SerMode, TypeSerializer,
};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct FloatSerializer {
inf_nan_mode: InfNanMode,
}
Expand Down
4 changes: 2 additions & 2 deletions src/serializers/type_serializers/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl WhenUsed {
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct FormatSerializer {
format_func: PyObject,
formatting_string: Py<PyString>,
Expand Down Expand Up @@ -161,7 +161,7 @@ impl TypeSerializer for FormatSerializer {
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct ToStringSerializer {
when_used: WhenUsed,
}
Expand Down
18 changes: 9 additions & 9 deletions src/serializers/type_serializers/function.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::sync::Arc;

use pyo3::exceptions::{PyAttributeError, PyRecursionError, PyRuntimeError};
use pyo3::gc::PyVisit;
Expand Down Expand Up @@ -71,7 +72,7 @@ impl BuildSerializer for FunctionPlainSerializerBuilder {
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct FunctionPlainSerializer {
func: PyObject,
name: String,
Expand Down Expand Up @@ -314,13 +315,13 @@ impl BuildSerializer for FunctionWrapSerializerBuilder {
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct FunctionWrapSerializer {
serializer: Box<CombinedSerializer>,
serializer: Arc<CombinedSerializer>,
func: PyObject,
name: String,
function_name: String,
return_serializer: Box<CombinedSerializer>,
return_serializer: Arc<CombinedSerializer>,
when_used: WhenUsed,
is_field_serializer: bool,
info_arg: bool,
Expand Down Expand Up @@ -358,11 +359,11 @@ impl BuildSerializer for FunctionWrapSerializer {

let name = format!("wrap_function[{function_name}, {}]", serializer.get_name());
Ok(Self {
serializer: Box::new(serializer),
serializer: Arc::new(serializer),
func: function.into_py(py),
function_name,
name,
return_serializer: Box::new(return_serializer),
return_serializer: Arc::new(return_serializer),
when_used: WhenUsed::new(&ser_schema, WhenUsed::Always)?,
is_field_serializer,
info_arg,
Expand Down Expand Up @@ -419,10 +420,9 @@ impl_py_gc_traverse!(FunctionWrapSerializer {
function_type_serializer!(FunctionWrapSerializer);

#[pyclass(module = "pydantic_core._pydantic_core")]
#[derive(Clone)]
#[cfg_attr(debug_assertions, derive(Debug))]
pub(crate) struct SerializationCallable {
serializer: CombinedSerializer,
serializer: Arc<CombinedSerializer>,
extra_owned: ExtraOwned,
filter: AnyFilter,
include: Option<PyObject>,
Expand All @@ -432,7 +432,7 @@ pub(crate) struct SerializationCallable {
impl SerializationCallable {
pub fn new(
py: Python,
serializer: &CombinedSerializer,
serializer: &Arc<CombinedSerializer>,
include: Option<&Bound<'_, PyAny>>,
exclude: Option<&Bound<'_, PyAny>>,
extra: &Extra,
Expand Down
16 changes: 8 additions & 8 deletions src/serializers/type_serializers/generator.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::sync::Arc;

use pyo3::gc::PyVisit;
use pyo3::intern;
Expand All @@ -17,9 +18,9 @@ use super::{
PydanticSerializer, SchemaFilter, SerMode, TypeSerializer,
};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct GeneratorSerializer {
item_serializer: Box<CombinedSerializer>,
item_serializer: Arc<CombinedSerializer>,
filter: SchemaFilter<usize>,
}

Expand All @@ -37,7 +38,7 @@ impl BuildSerializer for GeneratorSerializer {
None => AnySerializer::build(schema, config, definitions)?,
};
Ok(Self {
item_serializer: Box::new(item_serializer),
item_serializer: Arc::new(item_serializer),
filter: SchemaFilter::from_schema(schema)?,
}
.into())
Expand Down Expand Up @@ -82,7 +83,7 @@ impl TypeSerializer for GeneratorSerializer {
_ => {
let iter = SerializationIterator::new(
py_iter,
self.item_serializer.as_ref().clone(),
&self.item_serializer,
self.filter.clone(),
include,
exclude,
Expand Down Expand Up @@ -152,13 +153,12 @@ impl TypeSerializer for GeneratorSerializer {
}

#[pyclass(module = "pydantic_core._pydantic_core")]
#[derive(Clone)]
#[cfg_attr(debug_assertions, derive(Debug))]
pub(crate) struct SerializationIterator {
iterator: Py<PyIterator>,
#[pyo3(get)]
index: usize,
item_serializer: CombinedSerializer,
item_serializer: Arc<CombinedSerializer>,
extra_owned: ExtraOwned,
filter: SchemaFilter<usize>,
include: Option<PyObject>,
Expand All @@ -168,7 +168,7 @@ pub(crate) struct SerializationIterator {
impl SerializationIterator {
pub fn new(
py_iter: &Bound<'_, PyIterator>,
item_serializer: CombinedSerializer,
item_serializer: &Arc<CombinedSerializer>,
filter: SchemaFilter<usize>,
include: Option<&Bound<'_, PyAny>>,
exclude: Option<&Bound<'_, PyAny>>,
Expand All @@ -177,7 +177,7 @@ impl SerializationIterator {
Self {
iterator: py_iter.clone().into(),
index: 0,
item_serializer,
item_serializer: item_serializer.clone(),
extra_owned: ExtraOwned::new(extra),
filter,
include: include.map(|v| v.clone().into()),
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{
TypeSerializer,
};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct JsonSerializer {
serializer: Box<CombinedSerializer>,
}
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/json_or_python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{BuildSerializer, CombinedSerializer, Extra, TypeSerializer};
use crate::definitions::DefinitionsBuilder;
use crate::tools::SchemaDict;

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct JsonOrPythonSerializer {
json: Box<CombinedSerializer>,
python: Box<CombinedSerializer>,
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use super::{
SchemaFilter, TypeSerializer,
};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct ListSerializer {
item_serializer: Box<CombinedSerializer>,
filter: SchemaFilter<usize>,
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{
SerMode, TypeSerializer,
};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct LiteralSerializer {
expected_int: AHashSet<i64>,
expected_str: AHashSet<String>,
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl BuildSerializer for ModelFieldsBuilder {
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct ModelSerializer {
class: Py<PyType>,
serializer: Box<CombinedSerializer>,
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/nullable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::tools::SchemaDict;

use super::{infer_json_key_known, BuildSerializer, CombinedSerializer, Extra, IsType, ObType, TypeSerializer};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct NullableSerializer {
serializer: Box<CombinedSerializer>,
}
Expand Down
2 changes: 1 addition & 1 deletion src/serializers/type_serializers/set_frozenset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use super::{

macro_rules! build_serializer {
($struct_name:ident, $expected_type:literal, $py_type:ty) => {
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct $struct_name {
item_serializer: Box<CombinedSerializer>,
name: String,
Expand Down
4 changes: 2 additions & 2 deletions src/serializers/type_serializers/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::{
SerCheck, SerMode, TypeSerializer,
};

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct NoneSerializer;

impl BuildSerializer for NoneSerializer {
Expand Down Expand Up @@ -87,7 +87,7 @@ impl TypeSerializer for NoneSerializer {

macro_rules! build_simple_serializer {
($struct_name:ident, $expected_type:literal, $rust_type:ty, $ob_type:expr, $key_method:ident, $subtypes_allowed:expr) => {
#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct $struct_name;

impl $struct_name {
Expand Down
Loading

0 comments on commit 7368c1f

Please sign in to comment.