Skip to content

Commit 11d6f83

Browse files
authored
Merge pull request #11 from HadrienG2/slices
Finally implement Pessimize for slices on stable
2 parents 573aa16 + 0006203 commit 11d6f83

File tree

5 files changed

+55
-5
lines changed

5 files changed

+55
-5
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ concurrency:
2121
env:
2222
RUSTFLAGS: -D warnings
2323
RUSTDOCFLAGS: -D warnings
24-
MINIMAL_RUST: 1.63.0 # Minimal Supported Rust Version
24+
MINIMAL_RUST: 1.79.0 # Minimal Supported Rust Version
2525

2626
jobs:
2727
# Workaround for github CI dropping env var expansion in matrix strategy

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111
_There are no unreleased changes in the pipeline at the moment._
1212

1313

14+
## [2.0.0] - 2024-06-14
15+
16+
### Added
17+
18+
- Implement Pessimize for slices.
19+
20+
### Changed
21+
22+
- Bumped MSRV to 1.79.0.
23+
24+
1425
## [1.0.1] - 2024-02-12
1526

1627
### Fixed

Cargo.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ name = "pessimize"
1111
# - Roll an annotated git tag
1212
# - Add a github release
1313
#
14-
version = "1.0.1"
14+
version = "2.0.0"
1515
authors = ["Hadrien G. <[email protected]>"]
1616
description = "More efficient Rust compiler optimization barriers"
1717
keywords = [ "optimization", "barrier", "black-box", "efficient", "benchmarking" ]
1818
categories = [ "development-tools", "hardware-support", "no-std", "rust-patterns" ]
1919
license = "MPL-2.0"
2020
edition = "2021"
21-
rust-version = "1.63.0"
21+
rust-version = "1.79.0"
2222

2323
[features]
2424
default = ["std"]

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![Continuous
77
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)
88
![Requires rustc
9-
1.63.0+](https://img.shields.io/badge/rustc-1.63.0+-lightgray.svg)
9+
1.79.0+](https://img.shields.io/badge/rustc-1.79.0+-lightgray.svg)
1010

1111
Microbenchmarking is a subtle exercise to begin with, and the lack of
1212
lightweight optimization barriers on stable Rust makes it even more difficult.

src/ptr.rs

+40-1
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,11 @@ fn assume_accessed_thin_ptr<T: Sized>(x: *mut T) {
3232
unsafe { core::arch::asm!("/* {0} */", in(reg) x, options(preserves_flags, nostack)) }
3333
}
3434

35-
// Implementation of Pessimize for thin *const T
35+
// Implementation of Pessimize for thin *const T and *const [T]
3636
#[cfg(not(feature = "nightly"))]
3737
mod thin_pointers {
3838
use super::*;
39+
use core::ptr;
3940

4041
// Safe because the functions above are implemented as expected
4142
//
@@ -62,6 +63,44 @@ mod thin_pointers {
6263
assume_accessed_thin_ptr(*self as *mut T);
6364
}
6465
}
66+
67+
// Safe because the functions above are implemented as expected
68+
//
69+
// This is one of the primitive Pessimize impls on which the
70+
// PessimizeCast/BorrowPessimize stack is built
71+
unsafe impl<T: Sized> Pessimize for *const [T] {
72+
#[inline]
73+
fn hide(self) -> Self {
74+
let (data, len) = slice_to_raw_parts(self);
75+
ptr::slice_from_raw_parts(hide_thin_ptr(data), crate::hide(len))
76+
}
77+
78+
#[inline]
79+
fn assume_read(&self) {
80+
let (data, len) = slice_to_raw_parts(*self);
81+
assume_read_thin_ptr(data.cast::<()>());
82+
crate::consume(len)
83+
}
84+
85+
#[inline]
86+
fn assume_accessed(&mut self) {
87+
let (data, mut len) = slice_to_raw_parts(*self);
88+
assume_accessed_thin_ptr(data.cast::<()>().cast_mut());
89+
assume_accessed(&mut len);
90+
// Correct because hide is identity and assume_accessed doesn't modify
91+
*self = ptr::slice_from_raw_parts(data, len)
92+
}
93+
94+
#[inline]
95+
fn assume_accessed_imut(&self) {
96+
assume_accessed_thin_ptr(self.cast::<()>().cast_mut());
97+
// NOTE: It's not legal to change the length of a slice from &self
98+
}
99+
}
100+
101+
fn slice_to_raw_parts<T>(slice: *const [T]) -> (*const T, usize) {
102+
(slice.cast::<T>(), slice.len())
103+
}
65104
}
66105
//
67106
#[cfg(feature = "nightly")]

0 commit comments

Comments
 (0)