Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whitelist for slice args that do not need to be mutable #1199

Merged
merged 14 commits into from
Feb 7, 2019

Conversation

chinedufn
Copy link
Contributor

Closes #1005

crates/webidl/src/util.rs Outdated Show resolved Hide resolved
crates/webidl/src/idl_type.rs Outdated Show resolved Hide resolved
crates/webidl/src/util.rs Outdated Show resolved Hide resolved
@chinedufn
Copy link
Contributor Author

@alexcrichton this PR is now ready for review!

I whitelisted all of the WebGlRenderingContext methods and left instructions in comments about how future people (myself.. others..) can add more as we run into them.

Copy link
Contributor

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, thanks!

/// Here we implement a whitelist for those cases. This whitelist is currently
/// maintained by hand.
fn maybe_adjust<'a>(mut idl_type: IdlType<'a>, id: &'a OperationId) -> IdlType<'a> {
let immutable_f32_fns: Vec<&'static str> = vec![
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This plus the HashSet collection below I think can be somewhat costly to do once-per-function, so could this work be executed once and stored into an outer context which is then passed in here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up introducing lazy_static so that I didn't have to pass the hashset through.

The downside being an extra dep.

Please let me know if that downside is too great here.

}

// example `op`... -> "vertexAttrib1fv"
if let OperationId::Operation(Some(op)) = id {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can reduce some indentation as:

let id = match id {
    OperationId::Operation(Some(op)) => op,
    _ => return idl_type,
};

if !immutable_whitelist.contains(id) {
    return idl_type;
}

// ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First block makes sense!

For the second block I'm thinking that we don't want to return early because in the future we might test for other whitelists besides our F32 whitelist.

Unless we just had one whitelist HashMap that also specified the types to whitelist the function for...

But anyways.. for now I'll adapt this advice!

// slice argument immutable.
if immutable_f32_slice_whitelist.get(op).is_some() {
if let IdlType::Union(ref mut union) = idl_type {
if let IdlType::Float32Array { ref mut immutable } = union[0] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this instead be something like:

fn flag_slices_immutable(ty: &mut IdlType) {
    match ty {
        IdlType::Float32Array { immutable } => *immutable = true,
        IdlType::Union(list) => {
            for item in list { flag_slices_immutable(item); }
        }
        // ... other recursive cases like Nullable handled here

        // catch-all for everything else like Object
        _ => {}
    }
}

@chinedufn
Copy link
Contributor Author

@alexcrichton thanks a lot for all of the tips/advice/guidance here - I've applied your feedback and things are now ready for review.

for item in list { flag_slices_immutable(item); }
}

// TODO: ... other recursive cases like Nullable handled here
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's only a few of these other than Union, mind going ahead and expanding them in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhh gotcha - will do next time I hop in this branch!


lazy_static! {
/// These functions will have their f32 slice arguments be immutable.
static ref IMMUTABLE_F32_WHITELIST: HashSet<&'static str> = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of lazy_static!, could this be constructed and perhaps cached in the FirstPassRecord and then passed into maybe_adjust?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointer, will do next time I hop in this branch!

@chinedufn
Copy link
Contributor Author

@alexcrichton all feedback addressed!

@alexcrichton
Copy link
Contributor

Thanks! To avoid the RefCell, could the whitelist be initialized just after a FirstPassRecord is created?

@chinedufn
Copy link
Contributor Author

I noticed that we were using Default to initialize them so that felt a little less future proof if it got used in other places.

But for all I know the chances of that are next to zero (don't fully know the flow yet).

So cool I'll follow suit on how we set the builtin_idents https://github.com/chinedufn/wasm-bindgen/blob/ed28ce9db26f78fd06b4bd9c8c11896101b99696/crates/webidl/src/lib.rs#L83

@chinedufn
Copy link
Contributor Author

@alexcrichton done - ready for review

@alexcrichton
Copy link
Contributor

Ah yeah we shouldn't be creating too many instances of this struct but if it comes up I'm sure we can deduplicate as necessary!

Thanks!

@alexcrichton alexcrichton merged commit 9d27bc2 into rustwasm:master Feb 7, 2019
@chinedufn chinedufn deleted the ref-slice branch February 7, 2019 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants