Skip to content

Commit cc47dc5

Browse files
committed
rustc_metadata: store dense indexes in little-endian instead of big.
1 parent ef4352f commit cc47dc5

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

src/librustc_metadata/index.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl Index {
4444
debug!("lookup_item: index={:?} words.len={:?}",
4545
index, words.len());
4646

47-
let position = u32::from_be(words[index]);
47+
let position = u32::from_le(words[index]);
4848
if position == u32::MAX {
4949
debug!("lookup_item: position=u32::MAX");
5050
None
@@ -61,7 +61,7 @@ impl Index {
6161
if position == u32::MAX {
6262
None
6363
} else {
64-
Some((DefIndex::new(index), u32::from_be(position)))
64+
Some((DefIndex::new(index), u32::from_le(position)))
6565
}
6666
})
6767
}
@@ -100,13 +100,11 @@ impl IndexData {
100100
"recorded position for item {:?} twice, first at {:?} and now at {:?}",
101101
item, self.positions[item], position);
102102

103-
self.positions[item] = position;
103+
self.positions[item] = position.to_le();
104104
}
105105

106106
pub fn write_index(&self, buf: &mut Cursor<Vec<u8>>) {
107-
for &position in &self.positions {
108-
write_be_u32(buf, position);
109-
}
107+
buf.write_all(words_to_bytes(&self.positions)).unwrap();
110108
}
111109
}
112110

@@ -120,7 +118,7 @@ pub struct DenseIndex {
120118
impl DenseIndex {
121119
pub fn lookup(&self, buf: &[u8], ix: u32) -> Option<u32> {
122120
let data = bytes_to_words(&buf[self.start..self.end]);
123-
data.get(ix as usize).map(|d| u32::from_be(*d))
121+
data.get(ix as usize).map(|d| u32::from_le(*d))
124122
}
125123
pub fn from_buf(buf: &[u8], start: usize, end: usize) -> Self {
126124
assert!((end-start)%4 == 0 && start <= end && end <= buf.len());
@@ -135,23 +133,16 @@ pub fn write_dense_index(entries: Vec<u32>, buf: &mut Cursor<Vec<u8>>) {
135133
let elen = entries.len();
136134
assert!(elen < u32::MAX as usize);
137135

138-
for entry in entries {
139-
write_be_u32(buf, entry);
140-
}
136+
buf.write_all(words_to_bytes(&entries)).unwrap();
141137

142138
info!("write_dense_index: {} entries", elen);
143139
}
144140

145-
fn write_be_u32<W: Write>(w: &mut W, u: u32) {
146-
let _ = w.write_all(&[
147-
(u >> 24) as u8,
148-
(u >> 16) as u8,
149-
(u >> 8) as u8,
150-
(u >> 0) as u8,
151-
]);
152-
}
153-
154141
fn bytes_to_words(b: &[u8]) -> &[u32] {
155142
assert!(b.len() % 4 == 0);
156143
unsafe { slice::from_raw_parts(b.as_ptr() as *const u32, b.len()/4) }
157144
}
145+
146+
fn words_to_bytes(w: &[u32]) -> &[u8] {
147+
unsafe { slice::from_raw_parts(w.as_ptr() as *const u8, w.len()*4) }
148+
}

0 commit comments

Comments
 (0)