fn inline_views(dataflow: &mut DataflowDesc) -> Result<(), TransformError> {
    // We cannot inline anything whose `BuildDesc::id` appears in either the
    // `index_exports` or `sink_exports` of `dataflow`, because we lose our
    // ability to name it.

    // A view can / should be in-lined in another view if it is only used by
    // one subsequent view. If there are two distinct views that have not
    // themselves been merged, then too bad and it doesn't get inlined.

    // Starting from the *last* object to build, walk backwards and inline
    // any view that is neither referenced by a `index_exports` nor
    // `sink_exports` nor more than two remaining objects to build.

    for index in (0..dataflow.objects_to_build.len()).rev() {
        // Capture the name used by others to reference this view.
        let global_id = dataflow.objects_to_build[index].id;
        // Determine if any exports directly reference this view.
        let mut occurs_in_export = false;
        for (_gid, sink_desc) in dataflow.sink_exports.iter() {
            if sink_desc.from == global_id {
                occurs_in_export = true;
            }
        }
        for (_, index_desc, _) in dataflow.index_exports.iter() {
            if index_desc.on_id == global_id {
                occurs_in_export = true;
            }
        }
        // Count the number of subsequent views that reference this view.
        let mut occurrences_in_later_views = Vec::new();
        for other in (index + 1)..dataflow.objects_to_build.len() {
            if dataflow.objects_to_build[other]
                .view
                .global_uses()
                .contains(&global_id)
            {
                occurrences_in_later_views.push(other);
            }
        }
        // Inline if the view is referenced in one view and no exports.
        if !occurs_in_export && occurrences_in_later_views.len() == 1 {
            let other = occurrences_in_later_views[0];
            // We can remove this view and insert it in the later view,
            // but are not able to relocate the later view `other`.

            // When splicing in the `index` view, we need to create disjoint
            // identifiers for the Let's `body` and `value`, as well as a new
            // identifier for the binding itself. Following `UpdateLet`, we
            // go with the binding first, then the value, then the body.
            let update_let = crate::update_let::UpdateLet::default();
            let mut id_gen = crate::IdGen::default();
            let new_local = LocalId::new(id_gen.allocate_id());
            // Use the same `id_gen` to assign new identifiers to `index`.
            update_let.action(
                dataflow.objects_to_build[index].view.as_inner_mut(),
                &mut HashMap::new(),
                &mut id_gen,
            )?;
            // Assign new identifiers to the other relation.
            update_let.action(
                dataflow.objects_to_build[other].view.as_inner_mut(),
                &mut HashMap::new(),
                &mut id_gen,
            )?;
            // Install the `new_local` name wherever `global_id` was used.
            dataflow.objects_to_build[other]
                .view
                .as_inner_mut()
                .visit_mut_post(&mut |expr| {
                    if let MirRelationExpr::Get { id, .. } = expr {
                        if id == &Id::Global(global_id) {
                            *id = Id::Local(new_local);
                        }
                    }
                });

            // With identifiers rewritten, we can replace `other` with
            // a `MirRelationExpr::Let` binding, whose value is `index` and
            // whose body is `other`.
            let body = dataflow.objects_to_build[other]
                .view
                .as_inner_mut()
                .take_dangerous();
            let value = dataflow.objects_to_build[index]
                .view
                .as_inner_mut()
                .take_dangerous();
            *dataflow.objects_to_build[other].view.as_inner_mut() = MirRelationExpr::Let {
                id: new_local,
                value: Box::new(value),
                body: Box::new(body),
            };
            dataflow.objects_to_build.remove(index);
        }
    }

    Ok(())
}
/// Inline views used in one other view, and in no exported objects.
Inline views used in one other view, and in no exported objects.
Inline views used in one other view and in no exported objects .
fn inline_views(dataflow: &mut DataflowDesc) -> Result<(), TransformError> {
    // We cannot inline anything whose `BuildDesc::id` appears in either the
    // `index_exports` or `sink_exports` of `dataflow`, because we lose our
    // ability to name it.
    // A view can / should be in-lined in another view if it is only used by
    // one subsequent view. If there are two distinct views that have not
    // themselves been merged, then too bad and it doesn't get inlined.
    // Starting from the *last* object to build, walk backwards and inline
    // any view that is neither referenced by a `index_exports` nor
    // `sink_exports` nor more than two remaining objects to build.
    for index in (0..dataflow.objects_to_build.len()).rev() {
        // Capture the name used by others to reference this view.
        let global_id = dataflow.objects_to_build[index].id;
        // Determine if any exports directly reference this view.
        let mut occurs_in_export = false;
        for (_gid, sink_desc) in dataflow.sink_exports.iter() {
            if sink_desc.from == global_id {
                occurs_in_export = true;
            }
        }
        for (_, index_desc, _) in dataflow.index_exports.iter() {
            if index_desc.on_id == global_id {
                occurs_in_export = true;
            }
        }
        // Count the number of subsequent views that reference this view.
        let mut occurrences_in_later_views = Vec::new();
        for other in (index + 1)..dataflow.objects_to_build.len() {
            if dataflow.objects_to_build[other]
                .view
                .global_uses()
                .contains(&global_id)
            {
                occurrences_in_later_views.push(other);
            }
        }
        // Inline if the view is referenced in one view and no exports.
        if !occurs_in_export && occurrences_in_later_views.len() == 1 {
            let other = occurrences_in_later_views[0];
            // We can remove this view and insert it in the later view,
            // but are not able to relocate the later view `other`.
            // When splicing in the `index` view, we need to create disjoint
            // identifiers for the Let's `body` and `value`, as well as a new
            // identifier for the binding itself. Following `UpdateLet`, we
            // go with the binding first, then the value, then the body.
            let update_let = crate::update_let::UpdateLet::default();
            let mut id_gen = crate::IdGen::default();
            let new_local = LocalId::new(id_gen.allocate_id());
            // Use the same `id_gen` to assign new identifiers to `index`.
            update_let.action(
                dataflow.objects_to_build[index].view.as_inner_mut(),
                &mut HashMap::new(),
                &mut id_gen,
            )?;
            // Assign new identifiers to the other relation.
            update_let.action(
                dataflow.objects_to_build[other].view.as_inner_mut(),
                &mut HashMap::new(),
                &mut id_gen,
            )?;
            // Install the `new_local` name wherever `global_id` was used.
            dataflow.objects_to_build[other]
                .view
                .as_inner_mut()
                .visit_mut_post(&mut |expr| {
                    if let MirRelationExpr::Get { id, .. } = expr {
                        if id == &Id::Global(global_id) {
                            *id = Id::Local(new_local);
                        }
                    }
                });
            // With identifiers rewritten, we can replace `other` with
            // a `MirRelationExpr::Let` binding, whose value is `index` and
            // whose body is `other`.
            let body = dataflow.objects_to_build[other]
                .view
                .as_inner_mut()
                .take_dangerous();
            let value = dataflow.objects_to_build[index]
                .view
                .as_inner_mut()
                .take_dangerous();
            *dataflow.objects_to_build[other].view.as_inner_mut() = MirRelationExpr::Let {
                id: new_local,
                value: Box::new(value),
                body: Box::new(body),
            };
            dataflow.objects_to_build.remove(index);
        }
    }
    Ok(())
}
fn inline_views ( dataflow : & mut DataflowDesc ) -> Result < ( ) , TransformError > { // We cannot inline anything whose `BuildDesc::id` appears in either the // `index_exports` or `sink_exports` of `dataflow`, because we lose our // ability to name it. // A view can / should be in-lined in another view if it is only used by // one subsequent view. If there are two distinct views that have not // themselves been merged, then too bad and it doesn't get inlined. // Starting from the *last* object to build, walk backwards and inline // any view that is neither referenced by a `index_exports` nor // `sink_exports` nor more than two remaining objects to build. for index in ( 0 .. dataflow . objects_to_build . len ( ) ) . rev ( ) { // Capture the name used by others to reference this view. let global_id = dataflow . objects_to_build [ index ] . id ; // Determine if any exports directly reference this view. let mut occurs_in_export = false ; for ( _gid , sink_desc ) in dataflow . sink_exports . iter ( ) { if sink_desc . from == global_id { occurs_in_export = true ; } } for ( _ , index_desc , _ ) in dataflow . index_exports . iter ( ) { if index_desc . on_id == global_id { occurs_in_export = true ; } } // Count the number of subsequent views that reference this view. let mut occurrences_in_later_views = Vec :: new ( ) ; for other in ( index + 1 ) .. dataflow . objects_to_build . len ( ) { if dataflow . objects_to_build [ other ] . view . global_uses ( ) . contains ( & global_id ) { occurrences_in_later_views . push ( other ) ; } } // Inline if the view is referenced in one view and no exports. if ! occurs_in_export && occurrences_in_later_views . len ( ) == 1 { let other = occurrences_in_later_views [ 0 ] ; // We can remove this view and insert it in the later view, // but are not able to relocate the later view `other`. // When splicing in the `index` view, we need to create disjoint // identifiers for the Let's `body` and `value`, as well as a new // identifier for the binding itself. Following `UpdateLet`, we // go with the binding first, then the value, then the body. let update_let = crate :: update_let :: UpdateLet :: default ( ) ; let mut id_gen = crate :: IdGen :: default ( ) ; let new_local = LocalId :: new ( id_gen . allocate_id ( ) ) ; // Use the same `id_gen` to assign new identifiers to `index`. update_let . action ( dataflow . objects_to_build [ index ] . view . as_inner_mut ( ) , & mut HashMap :: new ( ) , & mut id_gen , ) ? ; // Assign new identifiers to the other relation. update_let . action ( dataflow . objects_to_build [ other ] . view . as_inner_mut ( ) , & mut HashMap :: new ( ) , & mut id_gen , ) ? ; // Install the `new_local` name wherever `global_id` was used. dataflow . objects_to_build [ other ] . view . as_inner_mut ( ) . visit_mut_post ( & mut | expr | { if let MirRelationExpr :: Get { id , .. } = expr { if id == & Id :: Global ( global_id ) { * id = Id :: Local ( new_local ) ; } } } ) ; // With identifiers rewritten, we can replace `other` with // a `MirRelationExpr::Let` binding, whose value is `index` and // whose body is `other`. let body = dataflow . objects_to_build [ other ] . view . as_inner_mut ( ) . take_dangerous ( ) ; let value = dataflow . objects_to_build [ index ] . view . as_inner_mut ( ) . take_dangerous ( ) ; * dataflow . objects_to_build [ other ] . view . as_inner_mut ( ) = MirRelationExpr :: Let { id : new_local , value : Box :: new ( value ) , body : Box :: new ( body ) , } ; dataflow . objects_to_build . remove ( index ) ; } } Ok ( ( ) ) }
Inline views used in one other view, and in no exported objects.
Inline views used in one other view and in no exported objects .
dataflow
&mut DataflowDesc
dataflow
&mut DataflowDesc