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

Remove byteorder dependency #125

Merged
merged 2 commits into from
Aug 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions analyzeme/src/stringtable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
//! See module-level documentation `measureme::stringtable`.

use byteorder::{BigEndian, ByteOrder, LittleEndian};
use measureme::file_header::{
read_file_header, strip_file_header, CURRENT_FILE_FORMAT_VERSION, FILE_MAGIC_STRINGTABLE_DATA,
FILE_MAGIC_STRINGTABLE_INDEX,
Expand All @@ -10,12 +9,13 @@ use measureme::{Addr, StringId};
use memchr::memchr;
use rustc_hash::FxHashMap;
use std::borrow::Cow;
use std::convert::TryInto;
use std::error::Error;

fn deserialize_index_entry(bytes: &[u8]) -> (StringId, Addr) {
(
StringId::new(LittleEndian::read_u32(&bytes[0..4])),
Addr(LittleEndian::read_u32(&bytes[4..8])),
StringId::new(u32::from_le_bytes(bytes[0..4].try_into().unwrap())),
Addr(u32::from_le_bytes(bytes[4..8].try_into().unwrap())),
)
}

Expand Down Expand Up @@ -138,7 +138,7 @@ fn is_utf8_continuation_byte(byte: u8) -> bool {
// String IDs in the table data are encoded in big endian format, while string
// IDs in the index are encoded in little endian format. Don't mix the two up.
fn decode_string_id_from_data(bytes: &[u8]) -> StringId {
let id = BigEndian::read_u32(&bytes[0..4]);
let id = u32::from_be_bytes(bytes[0..4].try_into().unwrap());
// Mask off the `0b10` prefix
StringId::new(id & STRING_ID_MASK)
}
Expand Down
6 changes: 3 additions & 3 deletions measureme/src/file_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! number.

use crate::serialization::SerializationSink;
use byteorder::{ByteOrder, LittleEndian};
use std::convert::TryInto;
use std::error::Error;

pub const CURRENT_FILE_FORMAT_VERSION: u32 = 5;
Expand All @@ -22,7 +22,7 @@ pub fn write_file_header<S: SerializationSink>(s: &S, file_magic: &[u8; 4]) {

s.write_atomic(FILE_HEADER_SIZE, |bytes| {
bytes[0..4].copy_from_slice(file_magic);
LittleEndian::write_u32(&mut bytes[4..8], CURRENT_FILE_FORMAT_VERSION);
bytes[4..8].copy_from_slice(&CURRENT_FILE_FORMAT_VERSION.to_le_bytes());
});
}

Expand All @@ -44,7 +44,7 @@ pub fn read_file_header(bytes: &[u8], expected_magic: &[u8; 4]) -> Result<u32, B
return Err(From::from(msg));
}

Ok(LittleEndian::read_u32(&bytes[4..8]))
Ok(u32::from_le_bytes(bytes[4..8].try_into().unwrap()))
}

pub fn strip_file_header(data: &[u8]) -> &[u8] {
Expand Down
3 changes: 1 addition & 2 deletions measureme/src/profiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ pub struct Profiler<S: SerializationSink> {
}

impl<S: SerializationSink> Profiler<S> {
pub fn new<P: AsRef<Path>>(path_stem: P)
-> Result<Profiler<S>, Box<dyn Error + Send + Sync>> {
pub fn new<P: AsRef<Path>>(path_stem: P) -> Result<Profiler<S>, Box<dyn Error + Send + Sync>> {
let paths = ProfilerFiles::new(path_stem.as_ref());
let event_sink = Arc::new(S::from_path(&paths.events_file)?);

Expand Down
29 changes: 14 additions & 15 deletions measureme/src/raw_event.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::event_id::EventId;
use crate::stringtable::StringId;
#[cfg(target_endian = "big")]
use std::convert::TryInto;

/// `RawEvent` is how events are stored on-disk. If you change this struct,
/// make sure that you increment `file_header::CURRENT_FILE_FORMAT_VERSION`.
Expand Down Expand Up @@ -117,14 +119,12 @@ impl RawEvent {
{
// We always emit data as little endian, which we have to do
// manually on big endian targets.
use byteorder::{ByteOrder, LittleEndian};

LittleEndian::write_u32(&mut bytes[0..], self.event_kind.as_u32());
LittleEndian::write_u32(&mut bytes[4..], self.event_id.as_u32());
LittleEndian::write_u32(&mut bytes[8..], self.thread_id);
LittleEndian::write_u32(&mut bytes[12..], self.start_time_lower);
LittleEndian::write_u32(&mut bytes[16..], self.end_time_lower);
LittleEndian::write_u32(&mut bytes[20..], self.start_and_end_upper);
bytes[0..4].copy_from_slice(&self.event_kind.as_u32().to_le_bytes());
bytes[4..8].copy_from_slice(&self.event_id.as_u32().to_le_bytes());
bytes[8..12].copy_from_slice(&self.thread_id.to_le_bytes());
bytes[12..16].copy_from_slice(&self.start_time_lower.to_le_bytes());
bytes[16..20].copy_from_slice(&self.end_time_lower.to_le_bytes());
bytes[20..24].copy_from_slice(&self.start_and_end_upper.to_le_bytes());
}
}

Expand All @@ -147,14 +147,13 @@ impl RawEvent {

#[cfg(target_endian = "big")]
{
use byteorder::{ByteOrder, LittleEndian};
RawEvent {
event_kind: StringId::new(LittleEndian::read_u32(&bytes[0..])),
event_id: EventId::from_u32(LittleEndian::read_u32(&bytes[4..])),
thread_id: LittleEndian::read_u32(&bytes[8..]),
start_time_lower: LittleEndian::read_u32(&bytes[12..]),
end_time_lower: LittleEndian::read_u32(&bytes[16..]),
start_and_end_upper: LittleEndian::read_u32(&bytes[20..]),
event_kind: StringId::new(u32::from_le_bytes(bytes[0..4].try_into().unwrap())),
event_id: EventId::from_u32(u32::from_le_bytes(bytes[4..8].try_into().unwrap())),
thread_id: u32::from_le_bytes(bytes[8..12].try_into().unwrap()),
start_time_lower: u32::from_le_bytes(bytes[12..16].try_into().unwrap()),
end_time_lower: u32::from_le_bytes(bytes[16..20].try_into().unwrap()),
start_and_end_upper: u32::from_le_bytes(bytes[20..24].try_into().unwrap()),
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions measureme/src/stringtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ use crate::file_header::{
write_file_header, FILE_MAGIC_STRINGTABLE_DATA, FILE_MAGIC_STRINGTABLE_INDEX,
};
use crate::serialization::{Addr, SerializationSink};
use byteorder::{BigEndian, ByteOrder, LittleEndian};
// use byteorder::{BigEndian, ByteOrder};
workingjubilee marked this conversation as resolved.
Show resolved Hide resolved
use std::sync::Arc;

/// A `StringId` is used to identify a string in the `StringTable`. It is
Expand Down Expand Up @@ -190,7 +190,7 @@ impl<'s> StringComponent<'s> {
assert!(string_id.0 == string_id.0 & STRING_ID_MASK);
let tagged = string_id.0 | (1u32 << 31);

BigEndian::write_u32(&mut bytes[0..4], tagged);
&mut bytes[0..4].copy_from_slice(&tagged.to_be_bytes());
&mut bytes[4..]
}
}
Expand Down Expand Up @@ -253,8 +253,8 @@ impl_serializable_string_for_fixed_size!(16);

fn serialize_index_entry<S: SerializationSink>(sink: &S, id: StringId, addr: Addr) {
sink.write_atomic(8, |bytes| {
LittleEndian::write_u32(&mut bytes[0..4], id.0);
LittleEndian::write_u32(&mut bytes[4..8], addr.0);
bytes[0..4].copy_from_slice(&id.0.to_le_bytes());
bytes[4..8].copy_from_slice(&addr.0.to_le_bytes());
});
}

Expand Down