Conversation
📊 Bundle Size Comparison
👀 Notable resultsStatic test results:No major changes. Dynamic test results:No major changes. 📋 All resultsClick to reveal the results table (334 entries).
If you wish to run a comparison for other, slower bundlers, run the 'Tree-shake test' from the GitHub Actions menu. |
|
pkg.pr.new packages benchmark commit |
There was a problem hiding this comment.
Pull request overview
This pull request modernizes the function autonaming mechanism in the TypeGPU unplugin by refactoring common naming logic into a reusable helper function. Previously, functions with the "use gpu" directive used a different naming mechanism than other resources, leading to inconsistencies.
Changes:
- Introduced
extractLabelledExpressionhelper function to consolidate label extraction logic across different node types - Refactored
getFunctionNameto use the new helper, unifying it with other resource naming - Simplified
performExpressionNamingby using the extracted helper function
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/unplugin-typegpu/src/common.ts | Core refactoring: extracted extractLabelledExpression helper function and updated getFunctionName and performExpressionNaming to use it |
| packages/unplugin-typegpu/test/use-gpu-directive.test.ts | Updated test expectations to reflect that functions now receive proper names (e.g., "mod" instead of undefined) |
| packages/typegpu/tests/examples/individual/uniformity.test.ts | Updated WGSL output expectations to reflect improved function naming (e.g., "seed2_1" and "sample_1" instead of "item" and "item_1") |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| function extractLabelledExpression<T extends acorn.AnyNode | babel.Node>( | ||
| node: T, | ||
| ): [string, ExpressionFor<T>] | undefined { | ||
| if ( | ||
| node.type === 'VariableDeclarator' && | ||
| node.id.type === 'Identifier' && | ||
| node.init | ||
| ) { | ||
| return [node.id.name, node.init as ExpressionFor<T>]; | ||
| } else if (node.type === 'AssignmentExpression') { | ||
| const maybeName = tryFindIdentifier(node.left); | ||
| if (maybeName) { | ||
| return [maybeName, node.right as ExpressionFor<T>]; | ||
| } | ||
| } else if ( | ||
| (node.type === 'Property' || node.type === 'ObjectProperty') && | ||
| node.key.type === 'Identifier' | ||
| ) { | ||
| return [node.key.name, node.value as ExpressionFor<T>]; | ||
| } else if ( | ||
| (node.type === 'ClassProperty' || node.type === 'PropertyDefinition') && | ||
| node.value && | ||
| node.key.type === 'Identifier' | ||
| ) { | ||
| return [node.key.name, node.value as ExpressionFor<T>]; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
I would love some comments with simple examples for the branches - you can quite quickly parse the condition but it would be nice to see at a glance
Currently, functions are named by a different mechanism than everything else -- the name is one of the props assigned to typegpu metadata.
I tried to unify these mechanisms, but I came across two issues.
Firstly, at this moment there is no way to get access to implementation-specific functions in
common.ts. I needed to usecontainsResourceConstructorCall, which has different implementations in rollup and babel, and I couldn't do it without nasty ifs or rewriting thecommon.tsto be an abstract class.Secondly, the general autonaming mechanism relies on a resource being assigned to a variable, so I couldn't do it in the case of function declarations (if we apply the autonaming first, nothing happens because there is no assignment, and if we transpile first, also nothing happens because now on the rhs of the variable declaration is a complex mess with metadata).
So instead I just modernised the function naming mechanism.