Skip to content

Reduce bloat from naga::Error::as_parse_error #9941

Description

@bolshoytoster

I currently measure naga::Error::as_parse_error to emit 7818 lines of LLVM IR, taking up 27.9KiB in the output binary (using llvm-lines and cargo-bloat). I have managed to get it down to ~5119 lines of LLVM IR and ~22.2KiB of output by merging a lot of the match arms that are similar, i.e. changing

Error::UnexpectedComponents(bad_span) => ParseError {
	message: "unexpected components".to_string(),
	labels: vec![(bad_span, "unexpected components".into())],
	notes: vec![],
},
Error::UnexpectedOperationInConstContext(span) => ParseError {
	message: "this operation is not supported in a const context".to_string(),
	labels: vec![(span, "operation not supported here".into())],
	notes: vec![],
},
Error::NotStorageTexture(bad_span) => ParseError {
	message: "textureStore can only be applied to storage textures".to_string(),
	labels: vec![(bad_span, "not a storage texture".into())],
	notes: vec![],
},
// ..

to

Error::UnexpectedComponents(span)
	| Error::UnexpectedOperationInConstContext(span)
	| Error::NotStorageTexture(span)
	// ..
	=> {
	let (message, label) = match self {
		Error::UnexpectedComponents(_) => (
			"unexpected components",
			"unexpected components"
		),
		Error::UnexpectedOperationInConstContext(_) => (
			"this operation is not supported in a const context",
			"operation not supported here"
		),
		Error::NotStorageTexture(_) => (
			"textureStore can only be applied to storage textures",
			"not a storage texture"
		),
		// ..
		_ => unreachable!()
	};

	ParseError {
		message: message.to_string(),
		labels: vec![(span, label.into())],
		notes: vec![],
	}
}

And avoiding calling .to_string() in the Error::Unexpected arm:

Token::Separator(c) => format!("`{c}`"),
Token::Paren(c) => format!("`{c}`"),
Token::Attribute => "@".to_string(),
Token::Number(_) => "number".to_string(),
Token::Word(s) => s.to_string(),

->

Token::Separator(c) | Token::Paren(c) => Cow::Owned(format!("`{c}`")),
Token::Attribute => Cow::Borrowed("@"),
Token::Number(_) => Cow::Borrowed("number"),
Token::Word(s) => Cow::Borrowed(s),

There are probably more opportunities to reduce the function's size, but a 35% reduction in IR isn't nothing.

Would this be accepted as a PR?

Edit: I've now gotten it down to 4880 lines IR, 21.4KiB output 4501 lines IR, 18.5KiB output

It looks like most of the generated IR is drop glue (since the function uses a lot of functions that allocate, which can panic). It seems to generate ~154 separate almost-identical unwind blocks. I'm not sure how to convince the compiler to merge these.

If anyone knows more about this, I'd like some help to see if the function can be made even smaller.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    Status
    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions