async-std icon indicating copy to clipboard operation
async-std copied to clipboard

Feature request: Collections from the standard library

Open ckaran opened this issue 5 years ago • 3 comments

First off, THANK YOU for making this crate! The Future-aware synchronization primitives alone are worth their weight in gold! :grinning:

As for my request, is it possible to add in concurrent versions of the standard collections? I'm aware of crates like dash-map, but since async-std is supposed to replace sync-std, it feels like it would make sense for it to have collections in here.

ckaran avatar Dec 10 '20 15:12 ckaran

Hello, that sounds very interesting. What scenarios are you going to use it in? Is it expected to be used in the mode of async/await ?

surechen avatar Dec 29 '20 03:12 surechen

I do expect to use it in the mode of async/await. My hoped-for use is similar to an ECS, or a key-value store like sled. The idea is that the state of my simulations will be globally accessible, with numerous futures each updating different portions of the state concurrently. As long as two (or more) tasks are accessing disjoint sets of keys, then they should all be able to complete concurrently on different threads in a worker pool. Only if the intersection of two sets of keys is non-empty do you need to worry about forcing the futures to serialize.

ckaran avatar Jan 03 '21 16:01 ckaran

Hello, I refer to the idea of dashmap, and try to use async_std::sync::RwLock realize. In my test version only a async_std::sync::RwLock locks a std::collections::HashMap, but not rwlock arrays like dashmap. I want to provide the same api functions with std::collections::HashMap. The only difference is that with async and await, because I'm a rust and async_std novice, I want to confirm whether this job is useful for async_std. If this makes sense, I'll continue to work on it, such as controlling the granularity of locking HashMap to improve performance. Could you give me some advices? @Fishrock123 @yoshuawuyts

//Now My AsynchashMap is:

pub struct AsyncHashMap<K, V, S = RandomState>
	where
		K: Eq + Hash + Send + Sync,
		V: Send + Sync,
		S: BuildHasher + Default + Send + Sync,
{
    data: Arc<Box<RwLock<HashMap<K, V, S>>>>,
}

//My Iter is:

pub struct Iter<'a, K, V, S = RandomState>
    where
        K: 'a + Eq + Hash + Send + Sync,
        V: 'a + Send + Sync,
        S: 'a + BuildHasher + Clone + Default + Send + Sync,
{
     map: &'a AsyncHashMap<K, V, S>,
     rwlock: RwLockReadGuard<'a, HashMap<K, V, S>>,
     iter: Option<SyncIter<'a, K, V>>,
}

//My IterMut is:

pub struct IterMut<'a, K, V, S = RandomState>
    where
        K: 'a + Eq + Hash + Send + Sync,
        V: 'a + Send + Sync,
        S: 'a + BuildHasher + Clone + Default + Send + Sync,
{
    map: &'a AsyncHashMap<K, V, S>,
    rwlock: RwLockWriteGuard<'a, HashMap<K, V, S>>,
    iter: Option<SyncIterMut<'a, K, V>>,
}

//My test function is like:

fn test_async_hashmap() {
    let hashmap: AsyncHashMap<i32, String> = AsyncHashMap::new();
    let hashmap1 = hashmap.clone();
    task::spawn(async move {
        hashmap1.insert(1, "1".to_string()).await;
        hashmap1.insert(2, "2".to_string()).await;
        hashmap1.insert(3, "3".to_string()).await;
        let len = hashmap1.len().await;
        println!("test_insert 1 len:{}", len);
    });
	
    let hashmap3 = hashmap.clone();
    task::spawn(async move {
        for (k, v) in hashmap3.iter().await {
            println!("iter() key:{}, value:{}", k, v);
        }
    });
	
    let hashmap6 = hashmap.clone();
    task::spawn(async move {
        for (_, val) in hashmap6.iter_mut().await {
            *val = "ssss".to_string();
        }
    });
	
    let hashmap4 = hashmap.clone();
    task::spawn(async move {
        for (k) in hashmap4.keys().await {
            println!("keys() key:{}", k);
        }
    });

    let hashmap5 = hashmap.clone();
    task::spawn(async move {
        //
        for (v) in hashmap5.values().await {
            println!("values() value:{}", v);
        }
    });
}

surechen avatar Jan 08 '21 07:01 surechen