Skip to content
Snippets Groups Projects
Commit 2d65a24a authored by Pierre-Yves David's avatar Pierre-Yves David
Browse files

model: inline the call to `_check_swhid`

This reduce the number of function call and should be faster.

The mashup of blind optimisation in the previous changeset yield some
interesting results in total.

It would be insightful to measure them individually, but that would
take more time than we currently have.

When testing all the validator changes on our previous "benchmark" we
see quite interesting improvement.

    swh loader run mercurial https://foss.heptapod.net/mercurial/mercurial-devel directory=/data/repos/mercurial-devel

= Median time of 3 run =
base:   17 minutes 48 seconds
before: 11 minutes 50 seconds
after:  11 minutes 11 seconds

On a profile of the same run, the `to_model` call of the from_disk's `Directory` class took the following percentage:
base:   43%
before: 15%
after:  11%
parent 3608271a
No related branches found
No related tags found
No related merge requests found
......@@ -1689,7 +1689,11 @@ class RawExtrinsicMetadata(HashableObject, BaseModel):
f"{self.target.object_type.name.lower()} object: {value}"
)
self._check_swhid(SwhidObjectType.SNAPSHOT, value)
if value.object_type != SwhidObjectType.SNAPSHOT:
raise ValueError(
f"Expected SWHID type 'snapshot', "
f"got '{value.object_type.name.lower()}' in {value}"
)
@release.validator
def check_release(self, attribute, value):
......@@ -1709,7 +1713,11 @@ class RawExtrinsicMetadata(HashableObject, BaseModel):
f"{self.target.object_type.name.lower()} object: {value}"
)
self._check_swhid(SwhidObjectType.RELEASE, value)
if value.object_type != SwhidObjectType.RELEASE:
raise ValueError(
f"Expected SWHID type 'release', "
f"got '{value.object_type.name.lower()}' in {value}"
)
@revision.validator
def check_revision(self, attribute, value):
......@@ -1729,7 +1737,11 @@ class RawExtrinsicMetadata(HashableObject, BaseModel):
f"{self.target.object_type.name.lower()} object: {value}"
)
self._check_swhid(SwhidObjectType.REVISION, value)
if value.object_type != SwhidObjectType.REVISION:
raise ValueError(
f"Expected SWHID type 'revision', "
f"got '{value.object_type.name.lower()}' in {value}"
)
@path.validator
def check_path(self, attribute, value):
......@@ -1763,16 +1775,10 @@ class RawExtrinsicMetadata(HashableObject, BaseModel):
f"{self.target.object_type.name.lower()} object: {value}"
)
self._check_swhid(SwhidObjectType.DIRECTORY, value)
def _check_swhid(self, expected_object_type, swhid):
if swhid.__class__ is not CoreSWHID:
raise ValueError(f"Expected SWHID, got a {swhid.__class__}: {swhid}")
if swhid.object_type != expected_object_type:
if value.object_type != SwhidObjectType.DIRECTORY:
raise ValueError(
f"Expected SWHID type '{expected_object_type.name.lower()}', "
f"got '{swhid.object_type.name.lower()}' in {swhid}"
f"Expected SWHID type 'directory', "
f"got '{value.object_type.name.lower()}' in {value}"
)
def to_dict(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment