Merge pull request #11 from urbit/dev/ares-review

Comments and bugfixes from Ares review
This commit is contained in:
Edward Amsden 2023-02-02 11:42:03 -06:00 committed by GitHub
commit a5ff1d31e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 736 additions and 93 deletions

507
rust/ares/Cargo.lock generated
View File

@ -2,11 +2,18 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "anes"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299"
[[package]]
name = "ares"
version = "0.1.0"
dependencies = [
"bitvec",
"criterion",
"either",
"intmap",
"libc",
@ -16,12 +23,29 @@ dependencies = [
"num-traits",
]
[[package]]
name = "atty"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [
"hermit-abi 0.1.19",
"libc",
"winapi",
]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitvec"
version = "1.0.0"
@ -34,6 +58,151 @@ dependencies = [
"wyz",
]
[[package]]
name = "bumpalo"
version = "3.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d261e256854913907f67ed06efbc3338dfe6179796deefc1ff763fc1aee5535"
[[package]]
name = "cast"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "ciborium"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0c137568cc60b904a7724001b35ce2630fd00d5d84805fbb608ab89509d788f"
dependencies = [
"ciborium-io",
"ciborium-ll",
"serde",
]
[[package]]
name = "ciborium-io"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "346de753af073cc87b52b2083a506b38ac176a44cfb05497b622e27be899b369"
[[package]]
name = "ciborium-ll"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213030a2b5a4e0c0892b6652260cf6ccac84827b83a85a534e178e3906c4cf1b"
dependencies = [
"ciborium-io",
"half",
]
[[package]]
name = "clap"
version = "3.2.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71655c45cb9845d3270c9d6df84ebe72b4dad3c2ba3f7023ad47c144e4e473a5"
dependencies = [
"bitflags",
"clap_lex",
"indexmap",
"textwrap",
]
[[package]]
name = "clap_lex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
dependencies = [
"os_str_bytes",
]
[[package]]
name = "criterion"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7c76e09c1aae2bc52b3d2f29e13c6572553b30c4aa1b8a49fd70de6412654cb"
dependencies = [
"anes",
"atty",
"cast",
"ciborium",
"clap",
"criterion-plot",
"itertools",
"lazy_static",
"num-traits",
"oorandom",
"plotters",
"rayon",
"regex",
"serde",
"serde_derive",
"serde_json",
"tinytemplate",
"walkdir",
]
[[package]]
name = "criterion-plot"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6b50826342786a51a89e2da3a28f1c32b06e387201bc2d19791f622c673706b1"
dependencies = [
"cast",
"itertools",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2dd04ddaf88237dc3b8d8f9a3c1004b506b54b3313403944054d23c0870c521"
dependencies = [
"cfg-if",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-deque"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01a9af1f4c2ef74bb8aa1f7e19706bc72d03598c8a570bb5de72243c7a9d9d5a"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4fb766fa798726286dbbb842f174001dab8abc7b627a1dd86e0b7222a95d929f"
dependencies = [
"cfg-if",
]
[[package]]
name = "either"
version = "1.6.1"
@ -46,18 +215,97 @@ version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c"
[[package]]
name = "half"
version = "1.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7"
[[package]]
name = "hashbrown"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]]
name = "hermit-abi"
version = "0.1.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
dependencies = [
"libc",
]
[[package]]
name = "hermit-abi"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
dependencies = [
"libc",
]
[[package]]
name = "indexmap"
version = "1.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885e79c1fc4b10f0e172c475f458b7f7b93061064d98c3293e98c5ba0c8b399"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "intmap"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b357564d111300f8a33b79e06795235529a627a1f7078d2b1db7f7dcdf032874"
[[package]]
name = "itertools"
version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
[[package]]
name = "js-sys"
version = "0.3.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
[[package]]
name = "memmap"
version = "0.7.0"
@ -69,10 +317,18 @@ dependencies = [
]
[[package]]
name = "murmur3"
version = "0.5.1"
name = "memoffset"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ead5388e485d38e622630c6b05afd3761a6701ff15c55b279ea5b31dcb62cff"
checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
dependencies = [
"autocfg",
]
[[package]]
name = "murmur3"
version = "0.5.2"
source = "git+https://github.com/tloncorp/murmur3?branch=eamsden/non_copying#dde46823a87f358eee5a928c27e2731f9a53d616"
[[package]]
name = "num-derive"
@ -94,6 +350,62 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b"
dependencies = [
"hermit-abi 0.2.6",
"libc",
]
[[package]]
name = "once_cell"
version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6f61fba1741ea2b3d6a1e3178721804bb716a68a6aeba1149b5d52e3d464ea66"
[[package]]
name = "oorandom"
version = "11.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
[[package]]
name = "os_str_bytes"
version = "6.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b7820b9daea5457c9f21c69448905d723fbd21136ccf521748f23fd49e723ee"
[[package]]
name = "plotters"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2538b639e642295546c50fcd545198c9d64ee2a38620a628724a3b266d5fbf97"
dependencies = [
"num-traits",
"plotters-backend",
"plotters-svg",
"wasm-bindgen",
"web-sys",
]
[[package]]
name = "plotters-backend"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "193228616381fecdc1224c62e96946dfbc73ff4384fba576e052ff8c1bea8142"
[[package]]
name = "plotters-svg"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9a81d2759aae1dae668f783c308bc5c8ebd191ff4184aaa1b37f65a6ae5a56f"
dependencies = [
"plotters-backend",
]
[[package]]
name = "proc-macro2"
version = "1.0.40"
@ -118,6 +430,95 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
[[package]]
name = "rayon"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db3a213adf02b3bcfd2d3846bb41cb22857d131789e01df434fb7e7bc0759b7"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "356a0625f1954f730c0201cdab48611198dc6ce21f4acff55089b5a78e6e835b"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]]
name = "regex"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48aaa5748ba571fb95cd2c85c09f629215d3a6ece942baa100950af03a34f733"
dependencies = [
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.28"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
[[package]]
name = "ryu"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b4b9743ed687d4b4bcedf9ff5eaa7398495ae14e61cba0a295704edbc7decde"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "serde"
version = "1.0.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d193d69bae983fc11a79df82342761dfbf28a99fc8d203dca4c3c1b590948965"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.147"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4f1d362ca8fc9c3e3a7484440752472d68a6caa98f1ab81d99b5dfe517cec852"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877c235533714907a8c2464236f5c4b2a17262ef1bd71f38f35ea592c8da6883"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "syn"
version = "1.0.98"
@ -135,12 +536,103 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369"
[[package]]
name = "textwrap"
version = "0.16.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d"
[[package]]
name = "tinytemplate"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4d6b5f19ff7664e8c98d03e2139cb510db9b0a60b55f8e8709b689d939b6bc"
dependencies = [
"serde",
"serde_json",
]
[[package]]
name = "unicode-ident"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c"
[[package]]
name = "walkdir"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
[[package]]
name = "web-sys"
version = "0.3.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f"
dependencies = [
"js-sys",
"wasm-bindgen",
]
[[package]]
name = "winapi"
version = "0.3.9"
@ -157,6 +649,15 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"

View File

@ -10,8 +10,26 @@ edition = "2018"
bitvec = "1.0.0"
either = "1.6.1"
libc = "0.2.126"
murmur3 = "0.5.1"
murmur3 = { git = "https://github.com/tloncorp/murmur3", branch = "eamsden/non_copying" }
memmap = "0.7.0"
intmap = "1.1.0"
num-traits = "0.2"
num-derive = "0.3"
criterion = "0.4"
[[bin]]
name = "cue_pill"
path = "benches/cue_pill.rs"
[[bin]]
name = "ares"
path = "src/main.rs"
[profile.dev]
opt-level = 3
[profile.dev.package."*"]
opt-level = 3
[features]
check_acyclic=[]

View File

@ -0,0 +1,75 @@
use ares::mem::NockStack;
use ares::noun::{DirectAtom, IndirectAtom};
use ares::serialization::{cue, jam};
use memmap;
use std::env;
use std::fs::{File, OpenOptions};
use std::io;
use std::mem;
use std::ptr::{copy_nonoverlapping, write_bytes};
use std::time::SystemTime;
fn main() -> io::Result<()> {
let filename = env::args().nth(1).expect("Must provide input filename");
let output_filename = format!("{}.out", filename.clone());
let f = File::open(filename)?;
let in_len = f.metadata()?.len();
let mut stack = NockStack::new(1 << 10 << 10 << 10, 0);
let jammed_input = unsafe {
let in_map = memmap::Mmap::map(&f)?;
let word_len = (in_len + 7) >> 3;
let (mut atom, dest) = IndirectAtom::new_raw_mut(&mut stack, word_len as usize);
write_bytes(dest.add(word_len as usize - 1), 0, 8);
copy_nonoverlapping(in_map.as_ptr(), dest as *mut u8, in_len as usize);
mem::drop(in_map);
atom.normalize_as_atom()
};
let now = SystemTime::now();
let mut i = 0;
let mut input = unsafe { DirectAtom::new_unchecked(0).as_atom().as_noun() };
loop {
if i >= 1 {
break;
};
i += 1;
input = cue(&mut stack, jammed_input);
}
match now.elapsed() {
Ok(elapse) => {
println!("{}", elapse.as_secs_f64());
}
Err(_) => println!("NO TIME FOR YOU!"),
};
let nuw = SystemTime::now();
let jammed_output = jam(&mut stack, input);
match nuw.elapsed() {
Ok(elapse) => {
println!("Jam: {}", elapse.as_secs_f64());
}
Err(_) => println!("NO TIME FOR YOU!"),
};
let f_out = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(output_filename)?;
f_out.set_len((jammed_output.size() << 3) as u64)?;
unsafe {
let mut out_map = memmap::MmapMut::map_mut(&f_out)?;
copy_nonoverlapping(
jammed_output.data_pointer() as *mut u8,
out_map.as_mut_ptr(),
jammed_output.size() << 3,
);
out_map.flush()?;
};
Ok(())
}

View File

@ -1,4 +1,5 @@
use crate::noun::{CellMemory, IndirectAtom, Noun, NounAllocator, Cell};
use crate::assert_acyclic;
use crate::noun::{Cell, CellMemory, IndirectAtom, Noun, NounAllocator};
use either::Either::{self, Left, Right};
use libc::{c_void, memcmp};
use memmap::MmapMut;
@ -13,6 +14,7 @@ pub const fn word_size_of<T>() -> usize {
/** Utility function to compute the raw memory usage of an IndirectAtom */
fn indirect_raw_size(atom: IndirectAtom) -> usize {
debug_assert!(atom.size() > 0);
atom.size() + 2
}
@ -25,9 +27,44 @@ pub enum Polarity {
West,
}
/* XX
* ~master-morzod suggests splitting stack frames:
* when FP-relative slots are on the west stack, allocate on the east stack, and vice versa.
* This would mean that traversal stacks must be allocated adjacent to the current frame's locals,
* rather than being allocated adjacent to the previous frame, which may in fact make more sense.
*
* This would enable completely reliable tail calls. Currently we cannot do tail-call optimization
* reliably for indirect calls, since the called arm might need arbitrarily many stack slots. With
* the proposed "split" layoud we can simply extend the number of slots if a tail-called arm
* requires more.
*
* Unless I'm mistaken this requires a three-pointer stack: the frame pointer is the basis for
* relative-offset locals as per usual, the stack pointer denotes the extent of the current frame,
* and the allocation denotes the current frontier of allocations on the opposite stack.
*
* Allocation means bumping the allocation pointer as per usual. Traversal stacks are implemented
* by saving the stack pointer to a local and then manipulating the stack pointer.
*
* Pushing is implemented by setting the frame pointer to the allocation pointer, the stack pointer
* to the necessary offset from the frame pointer to accomodate the locals, and the allocation
* pointer to the stack pointer. The previous values of all three are saved in the new frame's
* first three locals.
*
* Popping must first save the parent-frame stored values to temporaries. Then a copy is run which
* will, in general, run over the stored locals, and update the temporary for the allocation
* pointer. Finally, the temporaries are restored as the current frame/stack/allocation pointer.
*
* An alternative to this model is to copy anyway when making a tail call, using the tail call's
* parameters as roots. (Notably this requires the copier to take a list of mutable references to
* roots.) This ensures that no allocations are in the way if we need to extend the list of locals,
* at the expense of removing an obvious point of programmer control over the timing of memory
* management.
*/
/** A stack for Nock computation, which supports stack allocation and delimited copying collection
* for returned nouns
*/
#[allow(dead_code)] // We need the memory field to keep our memory from being unmapped
pub struct NockStack {
/** The base pointer */
start: *const u64,
@ -388,6 +425,7 @@ impl NockStack {
}
}
*self.previous_stack_pointer_pointer_east() = other_stack_pointer;
assert_acyclic!(*noun);
}
/** Copy a result noun and its subnouns from a west frame to its parent east frame
@ -498,6 +536,7 @@ impl NockStack {
}
}
*self.previous_stack_pointer_pointer_west() = other_stack_pointer;
assert_acyclic!(*noun);
}
/** Pop a frame from the (east) stack, providing a result, which will be copied to the return target
@ -603,6 +642,9 @@ pub unsafe fn unifying_equality(stack: &mut NockStack, a: *mut Noun, b: *mut Nou
break;
} else {
let (x, y): (*mut Noun, *mut Noun) = *(stack.top_in_previous_frame());
if (*x).raw_equals(*y) {
break;
};
match (
(*x).as_either_direct_allocated(),
(*y).as_either_direct_allocated(),
@ -656,6 +698,7 @@ pub unsafe fn unifying_equality(stack: &mut NockStack, a: *mut Noun, b: *mut Nou
let x_as_ptr = x_cell.to_raw_pointer();
let y_as_ptr = y_cell.to_raw_pointer();
if x_as_ptr == y_as_ptr {
// XX reclaim
continue;
} else {
if x_cell.head().raw_equals(y_cell.head())
@ -668,13 +711,20 @@ pub unsafe fn unifying_equality(stack: &mut NockStack, a: *mut Noun, b: *mut Nou
} else {
*y = *x;
}
stack.pop_no_copy();
stack.reclaim_in_previous_frame::<(*mut Noun, *mut Noun)>();
continue;
} else {
/* THIS ISN'T AN INFINITE LOOP
* If we discover a disequality in either side, we will
* short-circuit the entire loop and reset the work stack.
*
* If both sides are equal, then we will discover pointer
* equality when we return and unify the cell.
*/
*(stack.alloc_in_previous_frame()) =
(x_cell.tail_as_mut(), y_cell.tail_as_mut());
*(stack.alloc_in_previous_frame()) =
(x_cell.head_as_mut(), y_cell.tail_as_mut());
(x_cell.head_as_mut(), y_cell.head_as_mut());
continue;
}
}
@ -692,6 +742,8 @@ pub unsafe fn unifying_equality(stack: &mut NockStack, a: *mut Noun, b: *mut Nou
}
stack.restore_prev_stack_pointer_from_local(0);
stack.pop_no_copy();
assert_acyclic!(*a);
assert_acyclic!(*b);
(*a).raw_equals(*b)
}

View File

@ -1,78 +1,15 @@
use crate::assert_acyclic;
use crate::mem::*;
use crate::noun::{Allocated, Atom, DirectAtom, Noun};
use either::Either::*;
use murmur3::murmur3_32;
use std::cmp::min;
use std::io::{Read, Result};
use std::ptr::{copy_nonoverlapping, write_bytes};
/** A reader for an atom which pads the atom out to a given length */
struct PaddedReadAtom {
atom_bytes: usize, // actual size of the stored atom
atom_base: *const u8, // pointer to the atom data
atom_cursor: usize, // How many bytes we have read
atom_len: usize, // The total padded length
}
impl PaddedReadAtom {
fn new(atom: Atom, len: usize) -> Self {
match atom.as_either() {
Left(direct) => PaddedReadAtom {
atom_bytes: 8,
atom_base: (&direct as *const DirectAtom) as *const u8,
atom_cursor: 0,
atom_len: len,
},
Right(indirect) => PaddedReadAtom {
atom_bytes: indirect.size() << 3, // size is in 64 bit words, multiply by 8 to get bytes
atom_base: indirect.data_pointer() as *const u8, // data pointer, but for bytes
atom_cursor: 0,
atom_len: len,
},
}
}
}
impl Read for PaddedReadAtom {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
if self.atom_cursor >= self.atom_len {
Ok(0) // we are done
} else {
let req = buf.len(); // How many bytes does the reading caller want?
if self.atom_cursor < self.atom_bytes {
// are we still reading bytes from the atom?
let len = min(
self.atom_len - self.atom_cursor,
min(self.atom_bytes - self.atom_cursor, req),
);
// copy out bytes into the buffer, not running over the atom length itself, the
// padded length, or the buffer length
unsafe {
copy_nonoverlapping(
self.atom_base.add(self.atom_cursor),
buf.as_mut_ptr(),
len,
);
}
self.atom_cursor += len;
Ok(len)
} else {
// We are past the atom and into padding
let len = min(self.atom_len - self.atom_cursor, req);
// write 0s until we hit the buffer length or the padded length
unsafe {
write_bytes(buf.as_mut_ptr(), 0, len);
}
self.atom_cursor += len;
Ok(len)
}
}
}
}
use murmur3::murmur3_32_nocopy;
// Murmur3 hash an atom with a given padded length
fn muk_u32(syd: u32, len: usize, key: Atom) -> u32 {
murmur3_32(&mut PaddedReadAtom::new(key, len), syd).expect("Murmur3 hashing failed.")
match key.as_either() {
Left(direct) => murmur3_32_nocopy(&direct.data().to_le_bytes()[0..len], syd),
Right(indirect) => murmur3_32_nocopy(&indirect.as_bytes()[..len], syd),
}
}
/** Byte size of an atom.
@ -177,6 +114,7 @@ pub fn mug_u32_one(noun: Noun) -> Option<u32> {
}
pub fn mug_u32(stack: &mut NockStack, noun: Noun) -> u32 {
assert_acyclic!(noun);
stack.push(1);
unsafe {
stack.save_prev_stack_pointer_to_local(0);

View File

@ -1,5 +1,7 @@
use bitvec::prelude::{BitSlice, Lsb0};
use either::Either;
use intmap::IntMap;
use std::fmt::Debug;
use std::ptr;
use std::slice::{from_raw_parts, from_raw_parts_mut};
@ -30,6 +32,48 @@ const FORWARDING_TAG: u64 = u64::MAX & CELL_MASK;
/** Tag mask for a forwarding pointer */
const FORWARDING_MASK: u64 = CELL_MASK;
#[cfg(feature = "check_acyclic")]
#[macro_export]
macro_rules! assert_acyclic {
( $x:expr ) => {
assert!(acyclic_noun($x));
};
}
#[cfg(not(feature = "check_acyclic"))]
#[macro_export]
macro_rules! assert_acyclic {
( $x:expr ) => {};
}
pub fn acyclic_noun(noun: Noun) -> bool {
let mut seen = IntMap::new();
acyclic_noun_go(noun, &mut seen)
}
fn acyclic_noun_go(noun: Noun, seen: &mut IntMap<()>) -> bool {
match noun.as_either_atom_cell() {
Either::Left(_atom) => true,
Either::Right(cell) => {
if let Some(_) = seen.get(cell.0) {
false
} else {
seen.insert(cell.0, ());
if acyclic_noun_go(cell.head(), seen) {
if acyclic_noun_go(cell.tail(), seen) {
seen.remove(cell.0);
true
} else {
false
}
} else {
false
}
}
}
}
}
/** Test if a noun is a direct atom. */
fn is_direct_atom(noun: u64) -> bool {
noun & DIRECT_MASK == DIRECT_TAG
@ -50,6 +94,7 @@ fn is_cell(noun: u64) -> bool {
* Direct atoms represent an atom up to and including DIRECT_MAX as a machine word.
*/
#[derive(Copy, Clone)]
#[repr(C)]
#[repr(packed(8))]
pub struct DirectAtom(u64);
@ -105,6 +150,7 @@ impl DirectAtom {
* Indirect atoms are always stored in little-endian byte order
*/
#[derive(Copy, Clone)]
#[repr(C)]
#[repr(packed(8))]
pub struct IndirectAtom(u64);
@ -161,6 +207,7 @@ impl IndirectAtom {
* indirect atom, to be written into.
*/
pub unsafe fn new_raw_mut(allocator: &mut dyn NounAllocator, size: usize) -> (Self, *mut u64) {
debug_assert!(size > 0);
let buffer = allocator.alloc_indirect(size);
*buffer = 0;
*buffer.add(1) = size as u64;
@ -176,7 +223,7 @@ impl IndirectAtom {
size: usize,
) -> (Self, *mut u64) {
let allocation = Self::new_raw_mut(allocator, size);
ptr::write_bytes(allocation.1, 0, size << 3);
ptr::write_bytes(allocation.1, 0, size);
allocation
}
@ -208,7 +255,7 @@ impl IndirectAtom {
unsafe { from_raw_parts(self.data_pointer(), self.size()) }
}
pub fn as_byte_size<'a>(&'a self) -> &'a [u8] {
pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
unsafe { from_raw_parts(self.data_pointer() as *const u8, self.size() << 3) }
}
@ -263,7 +310,8 @@ impl IndirectAtom {
* the noun which is the cell's head, and a word describing a noun which is the cell's tail, each
* at a fixed offset.
*/
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Debug)]
#[repr(C)]
#[repr(packed(8))]
pub struct Cell(u64);
@ -341,6 +389,7 @@ impl Cell {
* Memory representation of the contents of a cell
*/
#[derive(Copy, Clone)]
#[repr(C)]
#[repr(packed(8))]
pub struct CellMemory {
pub metadata: u64,
@ -349,6 +398,7 @@ pub struct CellMemory {
}
#[derive(Copy, Clone)]
#[repr(C)]
#[repr(packed(8))]
pub union Atom {
raw: u64,
@ -413,7 +463,7 @@ impl Atom {
pub fn data_pointer(&self) -> *const u64 {
match self.as_either() {
Either::Left(direct) => (self as *const Atom) as *const u64,
Either::Left(_direct) => (self as *const Atom) as *const u64,
Either::Right(indirect) => indirect.data_pointer(),
}
}
@ -432,6 +482,7 @@ impl Atom {
}
#[derive(Copy, Clone)]
#[repr(C)]
#[repr(packed(8))]
pub union Allocated {
raw: u64,
@ -500,6 +551,7 @@ impl Allocated {
}
#[derive(Copy, Clone)]
#[repr(C)]
#[repr(packed(8))]
pub union Noun {
raw: u64,

View File

@ -1,3 +1,4 @@
use crate::assert_acyclic;
use crate::mem::unifying_equality;
use crate::mem::NockStack;
use crate::mug::mug_u32;
@ -34,9 +35,8 @@ pub fn cue(stack: &mut NockStack, buffer: Atom) -> Noun {
loop {
if unsafe { stack.prev_stack_pointer_equals_local(0) } {
let mut result = unsafe { *(stack.local_noun_pointer(1)) };
unsafe {
stack.pop(&mut result);
};
assert_acyclic!(result);
stack.pop(&mut result);
break result;
} else {
let dest_ptr: *mut Noun = unsafe { *(stack.top_in_previous_frame()) };
@ -46,9 +46,15 @@ pub fn cue(stack: &mut NockStack, buffer: Atom) -> Noun {
// 11 bits - cue backreference
cursor += 2;
unsafe {
*dest_ptr = *(backref_map
let reffed_noun = *(backref_map
.get(rub_backref(&mut cursor, buffer_bitslice))
.expect("Invalid backref in cue"));
assert_acyclic!(reffed_noun);
if let Ok(indirect) = reffed_noun.as_indirect() {
debug_assert!(indirect.size() > 0);
}
*dest_ptr = reffed_noun;
assert_acyclic!(reffed_noun);
stack.reclaim_in_previous_frame::<*mut Noun>();
}
continue;
@ -61,6 +67,10 @@ pub fn cue(stack: &mut NockStack, buffer: Atom) -> Noun {
*dest_ptr = cell.as_noun();
backref_map.insert(backref as u64, *dest_ptr);
stack.reclaim_in_previous_frame::<*mut Noun>();
(*cell_mem_ptr).tail =
DirectAtom::new_unchecked(0xEDBEEF).as_atom().as_noun();
(*cell_mem_ptr).head =
DirectAtom::new_unchecked(0xDEBEEF).as_atom().as_noun();
*(stack.alloc_in_previous_frame::<*mut Noun>()) =
&mut ((*cell_mem_ptr).tail);
*(stack.alloc_in_previous_frame::<*mut Noun>()) =
@ -85,15 +95,10 @@ pub fn cue(stack: &mut NockStack, buffer: Atom) -> Noun {
// TODO: use first_zero() on a slice of the buffer
fn get_size(cursor: &mut usize, buffer: &BitSlice<u64, Lsb0>) -> usize {
let mut bitsize: usize = 0;
loop {
if buffer[*cursor + bitsize] {
break;
} else {
bitsize += 1;
continue;
}
}
let buff_at_cursor = &buffer[*cursor..];
let bitsize = buff_at_cursor
.first_one()
.expect("Size encoding must terminate with a 1 bit");
if bitsize == 0 {
*cursor += 1;
0
@ -122,6 +127,8 @@ fn rub_atom(stack: &mut NockStack, cursor: &mut usize, buffer: &BitSlice<u64, Ls
let wordsize = (size + 63) >> 6;
let (atom, slice) = unsafe { IndirectAtom::new_raw_mut_bitslice(stack, wordsize) }; // fast round to wordsize
slice[0..size].copy_from_bitslice(&buffer[*cursor..*cursor + size]);
debug_assert!(atom.size() > 0);
*cursor += size;
atom.as_atom()
}
}