Skip to content

Commit

Permalink
fix: improve performance of amber sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Noah Negin-Ulster committed Jan 26, 2023
1 parent 4ca0716 commit 9707a5c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 18 deletions.
12 changes: 9 additions & 3 deletions src/syrupy/extensions/amber/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
Dict,
Generator,
Iterable,
List,
NamedTuple,
Optional,
Set,
Expand Down Expand Up @@ -104,8 +103,15 @@ def __maybe_int(cls, part: str) -> Tuple[int, Union[str, int]]:
@classmethod
def __snapshot_sort_key(
cls, snapshot: "Snapshot"
) -> List[Tuple[int, Union[str, int]]]:
return [cls.__maybe_int(part) for part in snapshot.name.split(".")]
) -> Tuple[Tuple[int, Union[str, int]], ...]:
# we only need to sort the last part of the snapshot name
index = snapshot.name.rfind(".") + 1
if index:
return (
(0, snapshot.name[: index - 1]),
cls.__maybe_int(snapshot.name[index:]),
)
return ((0, snapshot.name),)

@classmethod
def write_file(
Expand Down
6 changes: 3 additions & 3 deletions tests/syrupy/__snapshots__/test_doctest.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
nested_obj_attr='nested doc test class attr',
)
# ---
# name: DocTestClass.NestedDocTestClass.doctest_method
'nested doc test method return value'
# ---
# name: DocTestClass.doctest_method
'doc test method return value'
# ---
Expand All @@ -20,6 +17,9 @@
obj_attr='test class attr',
)
# ---
# name: DocTestClass.NestedDocTestClass.doctest_method
'nested doc test method return value'
# ---
# name: doctest_fn
'doc test fn return value'
# ---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
# serializer version: 1
# name: TestClass.TestNestedClass.test_nested_class_method[x]
'parameterized nested class method x'
# ---
# name: TestClass.TestNestedClass.test_nested_class_method[y]
'parameterized nested class method y'
# ---
# name: TestClass.TestNestedClass.test_nested_class_method[z]
'parameterized nested class method z'
# ---
# name: TestClass.test_class_method_name
'this is in a test class'
# ---
Expand All @@ -20,13 +11,13 @@
# name: TestClass.test_class_method_parametrized[c]
'c'
# ---
# name: TestSubClass.TestNestedClass.test_nested_class_method[x]
# name: TestClass.TestNestedClass.test_nested_class_method[x]
'parameterized nested class method x'
# ---
# name: TestSubClass.TestNestedClass.test_nested_class_method[y]
# name: TestClass.TestNestedClass.test_nested_class_method[y]
'parameterized nested class method y'
# ---
# name: TestSubClass.TestNestedClass.test_nested_class_method[z]
# name: TestClass.TestNestedClass.test_nested_class_method[z]
'parameterized nested class method z'
# ---
# name: TestSubClass.test_class_method_name
Expand All @@ -41,6 +32,15 @@
# name: TestSubClass.test_class_method_parametrized[c]
'c'
# ---
# name: TestSubClass.TestNestedClass.test_nested_class_method[x]
'parameterized nested class method x'
# ---
# name: TestSubClass.TestNestedClass.test_nested_class_method[y]
'parameterized nested class method y'
# ---
# name: TestSubClass.TestNestedClass.test_nested_class_method[z]
'parameterized nested class method z'
# ---
# name: test_bool[False]
False
# ---
Expand Down

1 comment on commit 9707a5c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 9707a5c Previous: 02abef5 Ratio
benchmarks/test_1000x.py::test_1000x_reads 0.612636033734545 iter/sec (stddev: 0.05816742061299388) 0.8381195242511715 iter/sec (stddev: 0.04240394140227035) 1.37
benchmarks/test_1000x.py::test_1000x_writes 0.6123552037515025 iter/sec (stddev: 0.07764945241438873) 0.8626650008455868 iter/sec (stddev: 0.05153168408309042) 1.41
benchmarks/test_standard.py::test_standard 0.5773466184690041 iter/sec (stddev: 0.09848547318948048) 0.7465173870618954 iter/sec (stddev: 0.1502009356924296) 1.29

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.