Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add #[inline] and #[cold] in far more places #834

Merged
merged 1 commit into from Nov 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/backup.rs
Expand Up @@ -180,6 +180,7 @@ impl Backup<'_, '_> {
///
/// Will return `Err` if the underlying `sqlite3_backup_init` call returns
/// `NULL`.
#[inline]
pub fn new<'a, 'b>(from: &'a Connection, to: &'b mut Connection) -> Result<Backup<'a, 'b>> {
Backup::new_with_names(from, DatabaseName::Main, to, DatabaseName::Main)
}
Expand Down Expand Up @@ -225,6 +226,7 @@ impl Backup<'_, '_> {
}

/// Gets the progress of the backup as of the last call to `step`.
#[inline]
pub fn progress(&self) -> Progress {
unsafe {
Progress {
Expand All @@ -246,6 +248,7 @@ impl Backup<'_, '_> {
/// an error code other than `DONE`, `OK`, `BUSY`, or `LOCKED`. `BUSY` and
/// `LOCKED` are transient errors and are therefore returned as possible
/// `Ok` values.
#[inline]
pub fn step(&self, num_pages: c_int) -> Result<StepResult> {
use self::StepResult::{Busy, Done, Locked, More};

Expand Down Expand Up @@ -298,6 +301,7 @@ impl Backup<'_, '_> {
}

impl Drop for Backup<'_, '_> {
#[inline]
fn drop(&mut self) {
unsafe { ffi::sqlite3_backup_finish(self.b) };
}
Expand Down
13 changes: 13 additions & 0 deletions src/blob/mod.rs
Expand Up @@ -214,6 +214,7 @@ impl Connection {
/// Will return `Err` if `db`/`table`/`column` cannot be converted to a
/// C-compatible string or if the underlying SQLite BLOB open call
/// fails.
#[inline]
pub fn blob_open<'a>(
&'a self,
db: DatabaseName<'_>,
Expand Down Expand Up @@ -252,6 +253,7 @@ impl Blob<'_> {
/// # Failure
///
/// Will return `Err` if the underlying SQLite BLOB reopen call fails.
#[inline]
pub fn reopen(&mut self, row: i64) -> Result<()> {
let rc = unsafe { ffi::sqlite3_blob_reopen(self.blob, row) };
if rc != ffi::SQLITE_OK {
Expand All @@ -262,17 +264,20 @@ impl Blob<'_> {
}

/// Return the size in bytes of the BLOB.
#[inline]
pub fn size(&self) -> i32 {
unsafe { ffi::sqlite3_blob_bytes(self.blob) }
}

/// Return the current size in bytes of the BLOB.
#[inline]
pub fn len(&self) -> usize {
use std::convert::TryInto;
self.size().try_into().unwrap()
}

/// Return true if the BLOB is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.size() == 0
}
Expand All @@ -286,10 +291,12 @@ impl Blob<'_> {
/// # Failure
///
/// Will return `Err` if the underlying SQLite close call fails.
#[inline]
pub fn close(mut self) -> Result<()> {
self.close_()
}

#[inline]
fn close_(&mut self) -> Result<()> {
let rc = unsafe { ffi::sqlite3_blob_close(self.blob) };
self.blob = ptr::null_mut();
Expand All @@ -304,6 +311,7 @@ impl io::Read for Blob<'_> {
/// # Failure
///
/// Will return `Err` if the underlying SQLite read call fails.
#[inline]
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
let max_allowed_len = (self.size() - self.pos) as usize;
let n = min(buf.len(), max_allowed_len) as i32;
Expand Down Expand Up @@ -334,6 +342,7 @@ impl io::Write for Blob<'_> {
/// # Failure
///
/// Will return `Err` if the underlying SQLite write call fails.
#[inline]
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
let max_allowed_len = (self.size() - self.pos) as usize;
let n = min(buf.len(), max_allowed_len) as i32;
Expand All @@ -350,13 +359,15 @@ impl io::Write for Blob<'_> {
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
}

#[inline]
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}

impl io::Seek for Blob<'_> {
/// Seek to an offset, in bytes, in BLOB.
#[inline]
fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
let pos = match pos {
io::SeekFrom::Start(offset) => offset as i64,
Expand All @@ -383,6 +394,7 @@ impl io::Seek for Blob<'_> {

#[allow(unused_must_use)]
impl Drop for Blob<'_> {
#[inline]
fn drop(&mut self) {
self.close_();
}
Expand All @@ -398,6 +410,7 @@ impl Drop for Blob<'_> {
pub struct ZeroBlob(pub i32);

impl ToSql for ZeroBlob {
#[inline]
fn to_sql(&self) -> Result<ToSqlOutput<'_>> {
let ZeroBlob(length) = *self;
Ok(ToSqlOutput::ZeroBlob(length))
Expand Down
1 change: 1 addition & 0 deletions src/busy.rs
Expand Up @@ -69,6 +69,7 @@ impl Connection {
}

impl InnerConnection {
#[inline]
fn busy_timeout(&mut self, timeout: c_int) -> Result<()> {
let r = unsafe { ffi::sqlite3_busy_timeout(self.db, timeout) };
self.decode_result(r)
Expand Down
11 changes: 11 additions & 0 deletions src/cache.rs
Expand Up @@ -34,6 +34,7 @@ impl Connection {
///
/// Will return `Err` if `sql` cannot be converted to a C-compatible string
/// or if the underlying SQLite call fails.
#[inline]
pub fn prepare_cached(&self, sql: &str) -> Result<CachedStatement<'_>> {
self.cache.get(self, sql)
}
Expand All @@ -43,11 +44,13 @@ impl Connection {
/// number of cached statements. If you need more, or know that you
/// will not use cached statements, you
/// can set the capacity manually using this method.
#[inline]
pub fn set_prepared_statement_cache_capacity(&self, capacity: usize) {
self.cache.set_capacity(capacity)
}

/// Remove/finalize all prepared statements currently in the cache.
#[inline]
pub fn flush_prepared_statement_cache(&self) {
self.cache.flush()
}
Expand All @@ -69,19 +72,22 @@ pub struct CachedStatement<'conn> {
impl<'conn> Deref for CachedStatement<'conn> {
type Target = Statement<'conn>;

#[inline]
fn deref(&self) -> &Statement<'conn> {
self.stmt.as_ref().unwrap()
}
}

impl<'conn> DerefMut for CachedStatement<'conn> {
#[inline]
fn deref_mut(&mut self) -> &mut Statement<'conn> {
self.stmt.as_mut().unwrap()
}
}

impl Drop for CachedStatement<'_> {
#[allow(unused_must_use)]
#[inline]
fn drop(&mut self) {
if let Some(stmt) = self.stmt.take() {
self.cache.cache_stmt(unsafe { stmt.into_raw() });
Expand All @@ -90,6 +96,7 @@ impl Drop for CachedStatement<'_> {
}

impl CachedStatement<'_> {
#[inline]
fn new<'conn>(stmt: Statement<'conn>, cache: &'conn StatementCache) -> CachedStatement<'conn> {
CachedStatement {
stmt: Some(stmt),
Expand All @@ -99,17 +106,20 @@ impl CachedStatement<'_> {

/// Discard the statement, preventing it from being returned to its
/// `Connection`'s collection of cached statements.
#[inline]
pub fn discard(mut self) {
self.stmt = None;
}
}

impl StatementCache {
/// Create a statement cache.
#[inline]
pub fn with_capacity(capacity: usize) -> StatementCache {
StatementCache(RefCell::new(LruCache::new(capacity)))
}

#[inline]
fn set_capacity(&self, capacity: usize) {
self.0.borrow_mut().set_capacity(capacity)
}
Expand Down Expand Up @@ -155,6 +165,7 @@ impl StatementCache {
}
}

#[inline]
fn flush(&self) {
let mut cache = self.0.borrow_mut();
cache.clear()
Expand Down
4 changes: 4 additions & 0 deletions src/collation.rs
Expand Up @@ -15,6 +15,7 @@ unsafe extern "C" fn free_boxed_value<T>(p: *mut c_void) {

impl Connection {
/// `feature = "collation"` Add or modify a collation.
#[inline]
pub fn create_collation<'c, C>(&'c self, collation_name: &str, x_compare: C) -> Result<()>
where
C: Fn(&str, &str) -> Ordering + Send + UnwindSafe + 'c,
Expand All @@ -25,6 +26,7 @@ impl Connection {
}

/// `feature = "collation"` Collation needed callback
#[inline]
pub fn collation_needed(
&self,
x_coll_needed: fn(&Connection, &str) -> Result<()>,
Expand All @@ -33,6 +35,7 @@ impl Connection {
}

/// `feature = "collation"` Remove collation.
#[inline]
pub fn remove_collation(&self, collation_name: &str) -> Result<()> {
self.db.borrow_mut().remove_collation(collation_name)
}
Expand Down Expand Up @@ -139,6 +142,7 @@ impl InnerConnection {
self.decode_result(r)
}

#[inline]
fn remove_collation(&mut self, collation_name: &str) -> Result<()> {
let c_name = str_to_cstring(collation_name)?;
let r = unsafe {
Expand Down
16 changes: 16 additions & 0 deletions src/column.rs
Expand Up @@ -11,11 +11,13 @@ pub struct Column<'stmt> {

impl Column<'_> {
/// Returns the name of the column.
#[inline]
pub fn name(&self) -> &str {
self.name
}

/// Returns the type of the column (`None` for expression).
#[inline]
pub fn decl_type(&self) -> Option<&str> {
self.decl_type
}
Expand All @@ -35,10 +37,12 @@ impl Statement<'_> {

/// Return the number of columns in the result set returned by the prepared
/// statement.
#[inline]
pub fn column_count(&self) -> usize {
self.stmt.column_count()
}

#[inline]
pub(super) fn column_name_unwrap(&self, col: usize) -> &str {
// Just panic if the bounds are wrong for now, we never call this
// without checking first.
Expand All @@ -54,6 +58,7 @@ impl Statement<'_> {
/// column range for this row.
///
/// Panics when column name is not valid UTF-8.
#[inline]
pub fn column_name(&self, col: usize) -> Result<&str> {
self.stmt
.column_name(col)
Expand All @@ -72,6 +77,7 @@ impl Statement<'_> {
///
/// Will return an `Error::InvalidColumnName` when there is no column with
/// the specified `name`.
#[inline]
pub fn column_index(&self, name: &str) -> Result<usize> {
let bytes = name.as_bytes();
let n = self.column_count();
Expand Down Expand Up @@ -104,26 +110,31 @@ impl Statement<'_> {

impl<'stmt> Rows<'stmt> {
/// Get all the column names.
#[inline]
pub fn column_names(&self) -> Option<Vec<&str>> {
self.stmt.map(Statement::column_names)
}

/// Return the number of columns.
#[inline]
pub fn column_count(&self) -> Option<usize> {
self.stmt.map(Statement::column_count)
}

/// Return the name of the column.
#[inline]
pub fn column_name(&self, col: usize) -> Option<Result<&str>> {
self.stmt.map(|stmt| stmt.column_name(col))
}

/// Return the index of the column.
#[inline]
pub fn column_index(&self, name: &str) -> Option<Result<usize>> {
self.stmt.map(|stmt| stmt.column_index(name))
}

/// Returns a slice describing the columns of the Rows.
#[inline]
#[cfg(feature = "column_decltype")]
pub fn columns(&self) -> Option<Vec<Column>> {
self.stmt.map(Statement::columns)
Expand All @@ -132,26 +143,31 @@ impl<'stmt> Rows<'stmt> {

impl<'stmt> Row<'stmt> {
/// Get all the column names of the Row.
#[inline]
pub fn column_names(&self) -> Vec<&str> {
self.stmt.column_names()
}

/// Return the number of columns in the current row.
#[inline]
pub fn column_count(&self) -> usize {
self.stmt.column_count()
}

/// Return the name of the column.
#[inline]
pub fn column_name(&self, col: usize) -> Result<&str> {
self.stmt.column_name(col)
}

/// Return the index of the column.
#[inline]
pub fn column_index(&self, name: &str) -> Result<usize> {
self.stmt.column_index(name)
}

/// Returns a slice describing the columns of the Row.
#[inline]
#[cfg(feature = "column_decltype")]
pub fn columns(&self) -> Vec<Column> {
self.stmt.columns()
Expand Down
2 changes: 2 additions & 0 deletions src/config.rs
Expand Up @@ -74,6 +74,7 @@ impl Connection {
/// whether the QPSG is disabled or enabled
/// - SQLITE_DBCONFIG_TRIGGER_EQP: return `false` to indicate
/// output-for-trigger are not disabled or `true` if it is
#[inline]
pub fn db_config(&self, config: DbConfig) -> Result<bool> {
let c = self.db.borrow();
unsafe {
Expand Down Expand Up @@ -102,6 +103,7 @@ impl Connection {
/// enable QPSG
/// - SQLITE_DBCONFIG_TRIGGER_EQP: `false` to disable output for trigger
/// programs, `true` to enable it
#[inline]
pub fn set_db_config(&self, config: DbConfig, new_val: bool) -> Result<bool> {
let c = self.db.borrow_mut();
unsafe {
Expand Down
4 changes: 4 additions & 0 deletions src/context.rs
Expand Up @@ -12,6 +12,10 @@ use crate::types::{ToSqlOutput, ValueRef};
#[cfg(feature = "array")]
use crate::vtab::array::{free_array, ARRAY_TYPE};

// This function is inline despite it's size because what's in the ToSqlOutput
// is often known to the compiler, and thus const prop/DCE can substantially
// simplify the function.
#[inline]
pub(super) unsafe fn set_result(ctx: *mut sqlite3_context, result: &ToSqlOutput<'_>) {
let value = match *result {
ToSqlOutput::Borrowed(v) => v,
Expand Down