Repository: MystenLabs/sui
File: external-crates/move/crates/move-vm-runtime/src/cache/identifier_interner.rs (Rust)

Code before:
            Err(
                PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
                    .with_message(format!("Failed to find {key_type} key in ident interner.")),
            )
        }
    }

    /// Get the interned identifier value. This may raise an invariant error if `try_get_or_intern`
    /// fails, but that's likely a serious OOM issue.
    pub(crate) fn intern_identifier(&self, ident: &Identifier) -> PartialVMResult<IdentifierKey> {
        self.get_or_intern_str_internal(ident.borrow_str())
    }

    /// Get the interned identifier string value. This may raise an invariant error if
    /// `get_or_intern` fails, but that's likely a serious OOM issue.
    pub(crate) fn intern_ident_str(&self, ident_str: &IdentStr) -> PartialVMResult<IdentifierKey> {
        self.get_or_intern_str_internal(ident_str.borrow_str())
    }

    /// Get the interned address value. This may raise an invariant error if `try_get_or_intern`
    /// fails, but that's likely a serious OOM issue.
    pub(crate) fn intern_address(
        &self,
        address: &AccountAddress,
    ) -> PartialVMResult<IdentifierKey> {
        self.get_or_intern_str_internal(&address.short_str_lossless())
    }

    /// Get the interned identifier value, using `key_type` in the error case. This may raise an
    /// invariant error if `try_get_or_intern` fails, but that's likely a serious OOM issue.
    pub(crate) fn intern_identifier_with_msg(
        &self,
        ident: &Identifier,
        key_type: &str,
    ) -> PartialVMResult<IdentifierKey> {
        self.get_or_intern_str_internal(ident.borrow_str())
            .map_err(|err| err.with_message(format!("While attempting to intern {key_type}")))
    }

    fn get_or_intern_str_internal(&self, string: &str) -> PartialVMResult<IdentifierKey> {
        match self.0.try_get_or_intern(string) {
            Ok(result) => Ok(IdentifierKey(result)),
            Err(err) => Err(PartialVMError::new(StatusCode::INTERNER_LIMIT_REACHED)
                .with_message(format!("Failed to intern {string} ident; error: {err:?}."))),
        }
    }

    /// Get the current size of the interner in bytes.
    pub(crate) fn size(&self) -> usize {
        self.0.current_memory_usage()
    }

Review comment (bug):
I'd probably use `address.to_hex()` or `address.into()` (which will call `hex::encode` on the address) here instead of `short_str_lossless`. 

Main thing is that `short_str_lossless` tries to shorten the address. It's unlikely to have any issues since the function is pretty simple, but I don't want a bug in that to cause possible issues for keying and looking up the vtables. 

(I also realize this will be somewhat of a moot point after your next PR, so if you'd prefer to just move ahead with this since it's going to be replaced soon anyways that's fine).

Code after:
                PartialVMError::new(StatusCode::UNKNOWN_INVARIANT_VIOLATION_ERROR)
                    .with_message(format!("Failed to find {key_type} key in ident interner.")),
            )
        }
    }

    /// Get the interned identifier value. This may raise an invariant error if `try_get_or_intern`
    /// fails, but that's likely a serious OOM issue.
    pub(crate) fn intern_identifier(&self, ident: &Identifier) -> PartialVMResult<IdentifierKey> {
        self.get_or_intern_str_internal(ident.borrow_str())
    }

    /// Get the interned identifier string value. This may raise an invariant error if
    /// `get_or_intern` fails, but that's likely a serious OOM issue.
    pub(crate) fn intern_ident_str(&self, ident_str: &IdentStr) -> PartialVMResult<IdentifierKey> {
        self.get_or_intern_str_internal(ident_str.borrow_str())
    }

    /// Get the interned identifier value, using `key_type` in the error case. This may raise an
    /// invariant error if `try_get_or_intern` fails, but that's likely a serious OOM issue.
    pub(crate) fn intern_identifier_with_msg(
        &self,
        ident: &Identifier,
        key_type: &str,
    ) -> PartialVMResult<IdentifierKey> {
        self.get_or_intern_str_internal(ident.borrow_str())
            .map_err(|err| err.with_message(format!("While attempting to intern {key_type}")))
    }

    fn get_or_intern_str_internal(&self, string: &str) -> PartialVMResult<IdentifierKey> {
        match self.0.try_get_or_intern(string) {
            Ok(result) => Ok(IdentifierKey(result)),
            Err(err) => Err(PartialVMError::new(StatusCode::INTERNER_LIMIT_REACHED)
                .with_message(format!("Failed to intern {string} ident; error: {err:?}."))),
        }
    }

    /// Get the current size of the interner in bytes.
    pub(crate) fn size(&self) -> usize {
        self.0.current_memory_usage()
    }
}