|
| 1 | +use rustc_data_structures::owned_slice::OwnedSlice; |
| 2 | +use rustc_hashes::Hash128; |
| 3 | +use rustc_serialize::{Decodable, Decoder, Encodable, Encoder}; |
| 4 | + |
| 5 | +use crate::rmeta::EncodeContext; |
| 6 | +use crate::rmeta::decoder::BlobDecodeContext; |
| 7 | + |
| 8 | +#[derive(Clone, Default)] |
| 9 | +pub(crate) struct ExportedSymbolHashConfig; |
| 10 | + |
| 11 | +impl odht::Config for ExportedSymbolHashConfig { |
| 12 | + type Key = Hash128; |
| 13 | + type Value = (); |
| 14 | + |
| 15 | + type EncodedKey = [u8; 16]; |
| 16 | + type EncodedValue = [u8; 0]; |
| 17 | + |
| 18 | + type H = odht::UnHashFn; |
| 19 | + |
| 20 | + #[inline] |
| 21 | + fn encode_key(k: &Hash128) -> [u8; 16] { |
| 22 | + k.as_u128().to_le_bytes() |
| 23 | + } |
| 24 | + |
| 25 | + #[inline] |
| 26 | + fn encode_value(_v: &()) -> [u8; 0] { |
| 27 | + [] |
| 28 | + } |
| 29 | + |
| 30 | + #[inline] |
| 31 | + fn decode_key(k: &[u8; 16]) -> Hash128 { |
| 32 | + Hash128::new(u128::from_le_bytes(*k)) |
| 33 | + } |
| 34 | + |
| 35 | + #[inline] |
| 36 | + fn decode_value(_v: &[u8; 0]) {} |
| 37 | +} |
| 38 | + |
| 39 | +pub(crate) type ExportedSymbolHashTable = odht::HashTableOwned<ExportedSymbolHashConfig>; |
| 40 | + |
| 41 | +pub(crate) enum ExportedSymbolHashTableRef { |
| 42 | + /// Zero-copy view into metadata bytes. |
| 43 | + OwnedFromMetadata(odht::HashTable<ExportedSymbolHashConfig, OwnedSlice>), |
| 44 | + /// Locally built table, used during encoding. |
| 45 | + Owned(ExportedSymbolHashTable), |
| 46 | +} |
| 47 | + |
| 48 | +impl ExportedSymbolHashTableRef { |
| 49 | + pub(crate) fn keys(&self) -> Vec<Hash128> { |
| 50 | + match self { |
| 51 | + Self::OwnedFromMetadata(table) => table.iter().map(|(k, ())| k).collect(), |
| 52 | + Self::Owned(table) => table.iter().map(|(k, ())| k).collect(), |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExportedSymbolHashTableRef { |
| 58 | + fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) { |
| 59 | + let raw_bytes = match self { |
| 60 | + ExportedSymbolHashTableRef::Owned(table) => table.raw_bytes(), |
| 61 | + ExportedSymbolHashTableRef::OwnedFromMetadata(_) => { |
| 62 | + panic!( |
| 63 | + "ExportedSymbolHashTableRef::OwnedFromMetadata variant \ |
| 64 | + only exists for deserialization" |
| 65 | + ) |
| 66 | + } |
| 67 | + }; |
| 68 | + e.emit_usize(raw_bytes.len()); |
| 69 | + e.emit_raw_bytes(raw_bytes); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl<'a> Decodable<BlobDecodeContext<'a>> for ExportedSymbolHashTableRef { |
| 74 | + fn decode(d: &mut BlobDecodeContext<'a>) -> ExportedSymbolHashTableRef { |
| 75 | + let len = d.read_usize(); |
| 76 | + let pos = d.position(); |
| 77 | + let o = d.blob().bytes().clone().slice(|blob| &blob[pos..pos + len]); |
| 78 | + |
| 79 | + // Advance the decoder's position past the raw bytes we just sliced. |
| 80 | + let _ = d.read_raw_bytes(len); |
| 81 | + |
| 82 | + let inner = odht::HashTable::from_raw_bytes(o).unwrap_or_else(|e| { |
| 83 | + panic!("decode error for ExportedSymbolHashTable: {e}"); |
| 84 | + }); |
| 85 | + ExportedSymbolHashTableRef::OwnedFromMetadata(inner) |
| 86 | + } |
| 87 | +} |
0 commit comments