gfx-rs / rspirv

Rust implementation of SPIR-V module processing functionalities
https://docs.rs/rspirv
Apache License 2.0
437 stars 56 forks source link

Threaded code generation with mr::Builder #23

Open MaikKlein opened 6 years ago

MaikKlein commented 6 years ago

I currently want to multi-thread my code generation, but Builder is inherently mutable. The simple solution would be to just wrap it in a mutex, but that would kill the performance.

One solution that I am thinking of is to create a separate builder object for every node that I traverse, then I would return the builder object and merge them together in the correct order.

The only problem that I see are the ids. If I would want to split the builder object into many small builder objects I would need to keep track of the correct IDs.

#[derive(Default)]
pub struct Builder {
    module: mr::Module,
    next_id: Arc<AtomicUsize>, // <= from u32 to Arc<AtomicUsize>,
    function: Option<mr::Function>,
    basic_block: Option<mr::BasicBlock>,
}

One solution would be to use an Arc<AtomicUsize>, this could also made generic.

And and append function to merge them together. builder.append(other_builder);

What are your thoughts?

antiagainst commented 6 years ago

The above sounds good to me. Please feel free to modify and send pull requests! :)

Just keep it as Arc<AtomicUsize> would be fine for me. Let's change it to a more generic solution when demanded.

Nit: append() sounds more like attaching at the end of the builder; we actually want merge().