Skip to content

Commit

Permalink
Port Address to Rust (pantsbuild#18989)
Browse files Browse the repository at this point in the history
Profiles on pantsbuild#18911 show that a large fraction of our runtime is in the
"core" rules in `graph.py`. In order to ease porting some of the
`@rules` in `graph.py` to intrinsics, this change ports `Address` (and
`AddressInput`) to rust.

Although `Address` itself rarely shows up in profiles, it is one of the
core datastructures "below" `graph.py` (along with `Field` and `Target`,
which are the next subjects for porting). It would also be possible to
port `graph.py` to intrinsics _without_ porting the datastructures that
it consumes, but working with native datatypes significantly improves
the ergonomics of working with these types from rust (since after being
automatically converted at method boundaries, or converted explicitly
with `any.extract::<Address>()?`, rust methods can be called directly
rather than going through Python method dispatch).

Improves performance of `./pants --no-pantsd dependencies ::` by about
2%.
  • Loading branch information
stuhood authored May 16, 2023
1 parent eea1fdd commit d12d7e2
Show file tree
Hide file tree
Showing 12 changed files with 1,077 additions and 708 deletions.
6 changes: 4 additions & 2 deletions src/python/pants/backend/project_info/peek.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import collections
import json
from dataclasses import asdict, dataclass, is_dataclass
from dataclasses import dataclass, fields, is_dataclass
from typing import Any, Iterable, Mapping

from typing_extensions import Protocol, runtime_checkable
Expand Down Expand Up @@ -134,7 +134,9 @@ def default(self, o):
if o is None:
return o
if is_dataclass(o):
return asdict(o)
# NB: `dataclasses.asdict` creates a deep copy by default, which is unnecessary for
# this case.
return {field.name: getattr(o, field.name) for field in fields(o)}
if isinstance(o, collections.abc.Mapping):
return dict(o)
if (
Expand Down
2 changes: 1 addition & 1 deletion src/python/pants/base/specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def to_address(self) -> Address:
self.path_component,
target_name=self.target_component,
generated_name=self.generated_component,
parameters=self.parameters,
parameters=dict(self.parameters),
)


Expand Down
Loading

0 comments on commit d12d7e2

Please sign in to comment.