Browse Source

small update

index
Avril 2 years ago
parent
commit
2eefaef0ae
Signed by: flanchan
GPG Key ID: 284488987C31F630
  1. 7
      Cargo.toml
  2. 10
      README.md
  3. 24
      build.rs
  4. 48
      src/lib.rs

7
Cargo.toml

@ -1,9 +1,12 @@
[package]
name = "smallmap"
description = "Small byte-sized generic key-value map type"
keywords = ["map", "table", "small", "key", "value"]
version = "0.1.0"
authors = ["Avril <flanchan@cumallover.me>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
[build-dependencies]
rustc_version = "0.2"

10
README.md

@ -1,9 +1,11 @@
# smallmap
A small byte sized table map.
A small byte sized table map. (Currently *requires* nightly).
Designed for instances where you want a map with small keys (e.g. primitive).
Designed for instances where you want a small map with relatively trivial keys (e.g. primitive type).
Performance greately outpaces hash-based maps in these cases.
# Benchmarks
Some rudamentary benchmarks
@ -18,10 +20,10 @@ Some rudamentary benchmarks
| Which | ns/iter |
|-----------------|---------|
| `HashMap` | 126,418 |
| `HashMap` | 65,418 |
| `smallmap::Map` | 9,416 |
## u8
## u8 (single table)
| Which | ns/iter |
|-----------------|---------|
| `HashMap` | 15 |

24
build.rs

@ -0,0 +1,24 @@
extern crate rustc_version;
use rustc_version::{version, version_meta, Channel};
fn main() {
// Assert we haven't travelled back in time
assert!(version().unwrap().major >= 1);
// Set cfg flags depending on release channel
match version_meta().unwrap().channel {
Channel::Stable => {
println!("cargo:rustc-cfg=stable");
}
Channel::Beta => {
println!("cargo:rustc-cfg=beta");
}
Channel::Nightly => {
println!("cargo:rustc-cfg=nightly");
}
Channel::Dev => {
println!("cargo:rustc-cfg=dev");
}
}
}

48
src/lib.rs

@ -1,10 +1,11 @@
#![feature(const_in_array_repeat_expressions)]
#![feature(const_fn)]
#![feature(test)]
#![cfg_attr(nightly, feature(test))]
#![allow(dead_code)]
extern crate test;
#[cfg(nightly)] extern crate test;
const MAX: usize = 256;
@ -186,6 +187,24 @@ where K: Collapsible
}
None
}
pub fn remove<Q: ?Sized>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>,
Q: Collapsible + Eq
{
for page in self.0.iter_mut()
{
let v = page.search_mut(key);
match v {
Some((ref ok, _)) if key.eq(ok.borrow()) => {
return v.take().map(|(_, v)| v);
},
_ => (),
}
}
None
}
pub fn insert(&mut self, key: K, value: V) -> Option<V>
{
@ -251,7 +270,7 @@ pub fn collapse<T: AsRef<[u8]>>(bytes: T) -> u8
mod tests
{
use super::*;
use test::{Bencher, black_box};
#[cfg(nightly)] use test::{Bencher, black_box};
#[test]
fn it_works()
@ -276,7 +295,7 @@ mod tests
let mut small = Map::new();
let mut hash = HashMap::new();
let input = r#"Bludgeoning clsql and mariadb until they kind of work
let input = r#"Bludgeoning clsql and mariadb until they kind of work
There are no good mysql connectors for Common Lisp, whatever you decide to use, you're not going to have a good time. clsql is pretty much the best we've got, and since I had such a delightful experience configuring it myself, I hope to smooth the process of getting this shitware up and running with my own little blogpost here.
connecting to a database
@ -397,6 +416,7 @@ Now the charset for our initial connection should be set to utf8, and we're free
assert_eq!(&op1[..], &op2[..]);
}
#[cfg(nightly)]
#[bench]
fn char_smallmap(b: &mut Bencher)
{
@ -421,7 +441,7 @@ stream"#.chars().collect();
}
})
}
#[cfg(nightly)]
#[bench]
fn u8_smallmap(b: &mut Bencher)
{
@ -439,7 +459,8 @@ stream"#.chars().collect();
}
use std::collections::HashMap;
#[cfg(nightly)]
#[bench]
fn char_map(b: &mut Bencher)
{
@ -465,6 +486,7 @@ stream"#.chars().collect();
})
}
#[cfg(nightly)]
#[bench]
fn u8_map(b: &mut Bencher)
{
@ -480,13 +502,13 @@ stream"#.chars().collect();
}
})
}
#[cfg(nightly)]
#[bench]
fn smap_bench(b: &mut Bencher)
{
let mut small = Map::new();
let input = r#"Bludgeoning clsql and mariadb until they kind of work
let input = r#"Bludgeoning clsql and mariadb until they kind of work
There are no good mysql connectors for Common Lisp, whatever you decide to use, you're not going to have a good time. clsql is pretty much the best we've got, and since I had such a delightful experience configuring it myself, I hope to smooth the process of getting this shitware up and running with my own little blogpost here.
connecting to a database
@ -595,12 +617,13 @@ Now the charset for our initial connection should be set to utf8, and we're free
}
})
}
#[cfg(nightly)]
#[bench]
fn hmap_bench(b: &mut Bencher)
{
let mut small = HashMap::new();
let input = r#"Bludgeoning clsql and mariadb until they kind of work
let input = r#"Bludgeoning clsql and mariadb until they kind of work
There are no good mysql connectors for Common Lisp, whatever you decide to use, you're not going to have a good time. clsql is pretty much the best we've got, and since I had such a delightful experience configuring it myself, I hope to smooth the process of getting this shitware up and running with my own little blogpost here.
connecting to a database
@ -701,11 +724,8 @@ Now the charset for our initial connection should be set to utf8, and we're free
b.iter(|| {
for ch in input.chars()
{
if !small.contains_key(&ch) {
small.insert(ch, 0);
} else {
*small.get_mut(&ch).unwrap() += 1;
}
let counter = small.entry(ch).or_insert(0);
*counter +=1;
}
})
}

Loading…
Cancel
Save