Skip to content

Commit

Permalink
Merge pull request #11 from HadrienG2/slices
Browse files Browse the repository at this point in the history
Finally implement Pessimize for slices on stable
  • Loading branch information
HadrienG2 authored Jun 14, 2024
2 parents 573aa16 + 0006203 commit 11d6f83
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ concurrency:
env:
RUSTFLAGS: -D warnings
RUSTDOCFLAGS: -D warnings
MINIMAL_RUST: 1.63.0 # Minimal Supported Rust Version
MINIMAL_RUST: 1.79.0 # Minimal Supported Rust Version

jobs:
# Workaround for github CI dropping env var expansion in matrix strategy
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
_There are no unreleased changes in the pipeline at the moment._


## [2.0.0] - 2024-06-14

### Added

- Implement Pessimize for slices.

### Changed

- Bumped MSRV to 1.79.0.


## [1.0.1] - 2024-02-12

### Fixed
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ name = "pessimize"
# - Roll an annotated git tag
# - Add a github release
#
version = "1.0.1"
version = "2.0.0"
authors = ["Hadrien G. <[email protected]>"]
description = "More efficient Rust compiler optimization barriers"
keywords = [ "optimization", "barrier", "black-box", "efficient", "benchmarking" ]
categories = [ "development-tools", "hardware-support", "no-std", "rust-patterns" ]
license = "MPL-2.0"
edition = "2021"
rust-version = "1.63.0"
rust-version = "1.79.0"

[features]
default = ["std"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
[![Continuous
Integration](https://img.shields.io/github/actions/workflow/status/HadrienG2/pessimize/ci.yml?branch=master)](https://github.com/HadrienG2/pessimize/actions?query=workflow%3A%22Continuous+Integration%22)
![Requires rustc
1.63.0+](https://img.shields.io/badge/rustc-1.63.0+-lightgray.svg)
1.79.0+](https://img.shields.io/badge/rustc-1.79.0+-lightgray.svg)

Microbenchmarking is a subtle exercise to begin with, and the lack of
lightweight optimization barriers on stable Rust makes it even more difficult.
Expand Down
41 changes: 40 additions & 1 deletion src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ fn assume_accessed_thin_ptr<T: Sized>(x: *mut T) {
unsafe { core::arch::asm!("/* {0} */", in(reg) x, options(preserves_flags, nostack)) }
}

// Implementation of Pessimize for thin *const T
// Implementation of Pessimize for thin *const T and *const [T]
#[cfg(not(feature = "nightly"))]
mod thin_pointers {
use super::*;
use core::ptr;

// Safe because the functions above are implemented as expected
//
Expand All @@ -62,6 +63,44 @@ mod thin_pointers {
assume_accessed_thin_ptr(*self as *mut T);
}
}

// Safe because the functions above are implemented as expected
//
// This is one of the primitive Pessimize impls on which the
// PessimizeCast/BorrowPessimize stack is built
unsafe impl<T: Sized> Pessimize for *const [T] {
#[inline]
fn hide(self) -> Self {
let (data, len) = slice_to_raw_parts(self);
ptr::slice_from_raw_parts(hide_thin_ptr(data), crate::hide(len))
}

#[inline]
fn assume_read(&self) {
let (data, len) = slice_to_raw_parts(*self);
assume_read_thin_ptr(data.cast::<()>());
crate::consume(len)
}

#[inline]
fn assume_accessed(&mut self) {
let (data, mut len) = slice_to_raw_parts(*self);
assume_accessed_thin_ptr(data.cast::<()>().cast_mut());
assume_accessed(&mut len);
// Correct because hide is identity and assume_accessed doesn't modify
*self = ptr::slice_from_raw_parts(data, len)
}

#[inline]
fn assume_accessed_imut(&self) {
assume_accessed_thin_ptr(self.cast::<()>().cast_mut());
// NOTE: It's not legal to change the length of a slice from &self
}
}

fn slice_to_raw_parts<T>(slice: *const [T]) -> (*const T, usize) {
(slice.cast::<T>(), slice.len())
}
}
//
#[cfg(feature = "nightly")]
Expand Down

0 comments on commit 11d6f83

Please sign in to comment.