Skip to content

Commit 4090751

Browse files
committed
constant smallvec capacity
1 parent b698317 commit 4090751

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

src/serializers/type_serializers/union.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::borrow::Cow;
66

77
use crate::build_tools::py_schema_err;
88
use crate::definitions::DefinitionsBuilder;
9-
use crate::tools::SchemaDict;
9+
use crate::tools::{SchemaDict, UNION_ERR_SMALLVEC_CAPACITY};
1010
use crate::PydanticSerializationUnexpectedValue;
1111

1212
use super::{
@@ -79,7 +79,7 @@ impl TypeSerializer for UnionSerializer {
7979
// try the serializers in left to right order with error_on fallback=true
8080
let mut new_extra = extra.clone();
8181
new_extra.check = SerCheck::Strict;
82-
let mut errors: SmallVec<[PyErr; 16]> = SmallVec::new();
82+
let mut errors: SmallVec<[PyErr; UNION_ERR_SMALLVEC_CAPACITY]> = SmallVec::new();
8383

8484
for comb_serializer in &self.choices {
8585
match comb_serializer.to_python(value, include, exclude, &new_extra) {
@@ -114,7 +114,7 @@ impl TypeSerializer for UnionSerializer {
114114
fn json_key<'a>(&self, key: &'a Bound<'_, PyAny>, extra: &Extra) -> PyResult<Cow<'a, str>> {
115115
let mut new_extra = extra.clone();
116116
new_extra.check = SerCheck::Strict;
117-
let mut errors: SmallVec<[PyErr; 16]> = SmallVec::new();
117+
let mut errors: SmallVec<[PyErr; UNION_ERR_SMALLVEC_CAPACITY]> = SmallVec::new();
118118

119119
for comb_serializer in &self.choices {
120120
match comb_serializer.json_key(key, &new_extra) {
@@ -157,7 +157,7 @@ impl TypeSerializer for UnionSerializer {
157157
let py = value.py();
158158
let mut new_extra = extra.clone();
159159
new_extra.check = SerCheck::Strict;
160-
let mut errors: SmallVec<[PyErr; 16]> = SmallVec::new();
160+
let mut errors: SmallVec<[PyErr; UNION_ERR_SMALLVEC_CAPACITY]> = SmallVec::new();
161161

162162
for comb_serializer in &self.choices {
163163
match comb_serializer.to_python(value, include, exclude, &new_extra) {

src/tools.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,5 @@ pub(crate) fn new_py_string<'py>(py: Python<'py>, s: &str, cache_str: StringCach
146146
pystring_fast_new(py, s, ascii_only)
147147
}
148148
}
149+
150+
pub(crate) const UNION_ERR_SMALLVEC_CAPACITY: usize = 4;

src/validators/union.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::errors::{ErrorType, ToErrorValue, ValError, ValLineError, ValResult};
1212
use crate::input::{BorrowInput, Input, ValidatedDict};
1313
use crate::lookup_key::LookupKey;
1414
use crate::py_gc::PyGcTraverse;
15-
use crate::tools::SchemaDict;
15+
use crate::tools::{SchemaDict, UNION_ERR_SMALLVEC_CAPACITY};
1616

1717
use super::custom_error::CustomError;
1818
use super::literal::LiteralLookup;
@@ -249,7 +249,7 @@ struct ChoiceLineErrors<'a> {
249249

250250
enum MaybeErrors<'a> {
251251
Custom(&'a CustomError),
252-
Errors(SmallVec<[ChoiceLineErrors<'a>; 4]>),
252+
Errors(SmallVec<[ChoiceLineErrors<'a>; UNION_ERR_SMALLVEC_CAPACITY]>),
253253
}
254254

255255
impl<'a> MaybeErrors<'a> {

0 commit comments

Comments
 (0)