Skip to content

Commit

Permalink
Add prefetch
Browse files Browse the repository at this point in the history
  • Loading branch information
cevian committed Dec 6, 2023
1 parent f8ba9aa commit 0af170d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions timescale_vector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ rand_chacha = "0.3"
rand_core = "0.6"
rand_xorshift = "0.3"
rayon = "1"
libc = "0.2.150"


[dev-dependencies]
Expand Down
4 changes: 4 additions & 0 deletions timescale_vector/src/access_method/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,10 @@ pub trait Graph {
panic!("Nodes in the list search results that aren't in the builder");
}

for neighbor_index_pointer in &neighbors {
unsafe { (*neighbor_index_pointer).prefetch(index) };
}

for neighbor_index_pointer in &neighbors {
lsr.insert(index, self, *neighbor_index_pointer, query)
}
Expand Down
24 changes: 23 additions & 1 deletion timescale_vector/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ pub mod page;
pub mod ports;
pub mod tape;

use pgrx::PgRelation;
use pgrx::{
error,
pg_sys::{BufferGetPage, ForkNumber_MAIN_FORKNUM, BLCKSZ},
PgRelation,
};
use rkyv::{Archive, Deserialize, Serialize};

use std::io::Error;

use self::{
page::{ReadablePage, WritablePage},
ports::{PageGetItem, PageGetItemId},
Expand Down Expand Up @@ -81,6 +87,22 @@ impl ItemPointer {
pgrx::item_pointer_set_all(ctid, self.block_number, self.offset)
}

pub unsafe fn prefetch(self, index: &PgRelation) {
let res = pgrx::pg_sys::PrefetchBuffer(
index.as_ptr(),
ForkNumber_MAIN_FORKNUM,
self.block_number,
);
if res.recent_buffer > 0 {
let ptr = BufferGetPage(res.recent_buffer - 1);
let res = libc::madvise(ptr as _, BLCKSZ as usize, libc::MADV_WILLNEED);
if res != 0 {
let err = Error::last_os_error();
error!("Error in madvise: {}", err);
}
}
}

pub unsafe fn read_bytes(self, index: &PgRelation) -> ReadableBuffer {
let page = ReadablePage::read(index, self.block_number);
let item_id = PageGetItemId(*page, self.offset);
Expand Down

0 comments on commit 0af170d

Please sign in to comment.