Skip to main content

ConnectionCostMatrix

Struct ConnectionCostMatrix 

Source
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: u32

Number of forward (left-context) ids, i.e. the length of one row.

§backward_size: u32

Number of backward (right-context) ids, i.e. the number of rows.

Implementations§

Source§

impl ConnectionCostMatrix

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn cost(&self, forward_id: u32, backward_id: u32) -> i32

Trait Implementations§

Source§

impl Clone for ConnectionCostMatrix

Source§

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)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.