pub struct ConnectionCostMatrix {
pub forward_size: u32,
pub backward_size: u32,
/* private fields */
}Expand description
The connection cost matrix, in transposed layout
(costs[forward_id + backward_id * forward_size]).
The values are borrowed from their backing bytes whenever possible (see
ConnectionCostMatrix::is_zero_copy); for UniDic that avoids a 71 MB
copy and 71 MB of anonymous RSS on every load.
#[repr(C)] is load-bearing: it pins the four fields the Viterbi inner
loop touches into the first 24 bytes, i.e. a single cache line, and keeps
the much larger storage – which is read exactly once, at construction –
behind them. Letting the default layout interleave them measured as a
~4.7% tokenize regression on IPADIC.
Fields§
§forward_size: u32Number of forward (left-context) ids, i.e. the length of one row.
backward_size: u32Number of backward (right-context) ids, i.e. the number of rows.
Implementations§
Source§impl ConnectionCostMatrix
impl ConnectionCostMatrix
Sourcepub fn load(conn_data: impl Into<Data>) -> LinderaResult<ConnectionCostMatrix>
pub fn load(conn_data: impl Into<Data>) -> LinderaResult<ConnectionCostMatrix>
Load a ConnectionCostMatrix from raw binary data.
Supports the transposed format (header marker -1) and the legacy
format. The transposed format is borrowed in place when the host is
little-endian and the payload is i16-aligned; otherwise, and always
for the legacy format, the values are decoded into an owned buffer.
§Arguments
conn_data- Raw binary data for the connection cost matrix.
§Returns
A ConnectionCostMatrix, or an error if the data is too short, the
header is malformed, or the axis sizes do not fit the payload.
Sourcepub fn costs(&self) -> &[i16]
pub fn costs(&self) -> &[i16]
Returns the whole cost table as a flat slice in transposed layout.
Replaces the former costs_data field, which forced every load to
materialize an owned Vec<i16>.
§Returns
The cost values, indexed by forward_id + backward_id * forward_size.
Sourcepub fn is_zero_copy(&self) -> bool
pub fn is_zero_copy(&self) -> bool
Whether the cost values are read in place from the backing bytes, i.e. no copy was made at load time.
Exposed so tests and diagnostics can assert that the mmap and embedded paths actually take the zero-copy branch.
§Returns
true when the matrix borrows its values, false when it owns a
decoded copy.
Sourcepub fn row(&self, backward_id: u32) -> &[i16]
pub fn row(&self, backward_id: u32) -> &[i16]
Returns the contiguous cost row for a fixed backward (right-context) id, so callers relaxing many forward ids against the same backward id pay the offset computation and bounds check once.
§Arguments
backward_id- The backward context id selecting the row.
§Returns
A forward_size-long slice indexed directly by forward context id.
pub fn cost(&self, forward_id: u32, backward_id: u32) -> i32
Trait Implementations§
Source§impl Clone for ConnectionCostMatrix
impl Clone for ConnectionCostMatrix
Source§fn clone(&self) -> Self
fn clone(&self) -> Self
Clones the matrix, re-deriving the cached costs pointer from the cloned storage.
A derived Clone would copy costs_ptr verbatim. For
CostStorage::Borrowed(Data::Static) and Data::Map that would happen
to be sound (the pointee is 'static or Arc-shared), but for
Data::Vec and CostStorage::Owned the clone allocates a fresh buffer
while the copied pointer still refers to the source’s – a
use-after-free as soon as the source is dropped. Deriving Clone on
this type is therefore forbidden.
Alignment is handled one level down, in CostStorage’s own Clone:
a reallocated Data::Vec may no longer be i16-aligned, so the
borrow is re-validated there and falls back to an owned buffer.
§Returns
An independent ConnectionCostMatrix holding the same costs.
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for ConnectionCostMatrix
impl RefUnwindSafe for ConnectionCostMatrix
impl Send for ConnectionCostMatrix
impl Sync for ConnectionCostMatrix
impl Unpin for ConnectionCostMatrix
impl UnsafeUnpin for ConnectionCostMatrix
impl UnwindSafe for ConnectionCostMatrix
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.