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

String Literal Struct Accessor to Hash Optimization #2821

Closed
tinkerer-red opened this issue Nov 14, 2023 · 6 comments
Closed

String Literal Struct Accessor to Hash Optimization #2821

tinkerer-red opened this issue Nov 14, 2023 · 6 comments
Assignees
Labels
feature request New feature (or a request for one)
Milestone

Comments

@tinkerer-red
Copy link

Is your feature request related to a problem?

Would be nice if string literals would be converted into hash look ups when using struct_get, struct_exists, or the [$ ""] operator at compile time.

evidence of this not happening:
YYC:
image
VM:
image

Describe the solution you'd like

when a string literal is given to a struct accessor it should be converted into a hash look up with a real literal at compile time.

Describe alternatives you've considered

adding a build script to my projects to optimize.

Using forceinline functions with statics which will cache the hash the first time then always returns that. But this does not actually work.

Additional context

No response

@tinkerer-red tinkerer-red added the feature request New feature (or a request for one) label Nov 14, 2023
@rwkay
Copy link

rwkay commented Nov 16, 2023

Fixed in 2024.2 - optimisations have been added

  • variable_struct_get is now optimised if the second parameter is a constant string e.g if variable_struct_get( a, "hello" ) it now gets optimised to a.hello
    variable_struct_set is now optimised if the second parameter is a constant string e.g. variable_struct_set( a, "hello", 20) is optimised to be a.hello = 20

NOTE: a[$ "hello" ] = 20 is already turned into variable_struct_set( a, "hello", 20) as part of the accessor framework similarly any reference to the value a[$ "hello" ] is turned into variable_struct_get( a, "hello" ). So this accomplishes the desired result.

My test code for this is

var a = { Hello : 20 };

var b = a[$ "Hello" ];
a[$ "foo" ] = "foo";
show_debug_message( $"b = {b},a = {a}" );

@rwkay rwkay closed this as completed Nov 16, 2023
@gnysek
Copy link
Contributor

gnysek commented Nov 16, 2023

@rwkay if it's optimised this way, then it would break existing functionality as variable_struct_get can return undefined if property doesn't exist, while a.hello would crash game in that case.
Should it be that variable_struct_get( a, "hello" ) is optimised to a.hello ?? undefined ?

Or, alternatively, it's time for ?. operator...

@tinkerer-red
Copy link
Author

..or struct_set_from_hash(a, <"hello" hash litteral>) which does return undefined

@rwkay rwkay reopened this Nov 17, 2023
@rwkay
Copy link

rwkay commented Nov 17, 2023

OK this is now fixed for the case where hello does not exist on a it now returns undefined

variable_struct_get is now optimised if the second parameter is a constant string e.g if variable_struct_get( a, "hello" ) it now gets optimised to struct_get_from_hash( a, #"hello" ) where #"hello" is the hash for the variable named hello

@rwkay rwkay closed this as completed Nov 17, 2023
@tinkerer-red
Copy link
Author

Does that mean we can now use hash literals? like for instance if I want to pass #"hello" into a function such as trigger_event(#"onCollide")

@rwkay
Copy link

rwkay commented Nov 17, 2023

No it is just a notation I was using to describe what is happening it is not GML syntax

@iampremo iampremo added this to the 2024.2 milestone Nov 20, 2023
@iampremo iampremo transferred this issue from another repository Jan 9, 2024
@scott-dunbar scott-dunbar self-assigned this Jan 9, 2024
@YYBartT YYBartT self-assigned this Jan 15, 2024
YYBartT added a commit to YoYoGames/GameMaker-Manual that referenced this issue Jan 23, 2024
YoYoGames/GameMaker-Bugs#2804
YoYoGames/GameMaker-Bugs#2821
* Added a note under "Struct Functions" on the Variable Functions page
@YYBartT YYBartT moved this from Done to Ready for QA in Team Workload Jan 23, 2024
@scott-dunbar scott-dunbar moved this from Ready for QA to Verified in Team Workload Jan 30, 2024
gurpreetsinghmatharoo added a commit to YoYoGames/GameMaker-Manual that referenced this issue Feb 13, 2024
* develop:
  [Feature] Review for PR #88
  [General] Fixed toc when-closed arrow icon, needed rotation
  [General] Added file for ToC when-closed arrow
  [General] added css appends for index and glossary colours
  [Feature] Sequence Audio FX Parameters review
  [General]variable_clone() doesn't copy script refs in arrays correctly YoYoGames/GameMaker-Bugs#4548 * Added an additional note to mention you cannot clone built-in structs
  [Feature]Audio FX Parameter Tracks YoYoGames/GameMaker-Bugs#3162 * Added a new page "Sound in Sequences" under "The Sequence Editor":   * Overview image, ways in which to add sounds, details on emitters, audio effects and the Inspector   * Reordering audio effects in the Track Panel   * Short section on audio emitters' positions in sequence instances (i.e. the sequence instance's transform is added to the emitter's position)   * Added GIFs & videos to make things clearer * "The Track Panel" page:   * Listed audio effects under "Adding Parameter Tracks" * "The Sequence Instance Struct" page:   * Added soundIndex property under activeTracks, which refers to the sound *instance* currently being played * "The Track Struct" page:   * Listed all audio effect track parameter names under "Name" * Replaced all occurrences of maximum number of effect slots value of 8 with a RH variable * Some typo fixes here and there, replaced hyperlinks with cross-references, names with variables, etc.
  [General]Add note to for page that explains multiple variable assignment using var
  [Feature]Add vertex_submit_ext which takes a known amount of Vertexes YoYoGames/GameMaker-Bugs#2754 * Provided some details on frozen vertex buffers (reside in VRAM) * Consistency between vertex_submit & vertex_submit_ext function pages * Moved "surface must exist" warning note to snippet and inserted that on the function pages * Added a full description for the second example on the vertex_submit_ext page, fixed the modulo calculation so the max number will include the final vertex All done
  [Feature]Vertex Buffer Update Functions YoYoGames/GameMaker-Bugs#3163 * Added function page using script & added to ToC, updated links * Added most page contents, examples still to review
  [Feature]Debugger: [Mac IDE] Unable to use F11 to 'Step into function call' as it is overridden by the system hotkey default YoYoGames/GameMaker-Bugs#2337 * Manually added to the Shortcuts page, it will be added automatically with the next export from the shortcuts
  [General]In-Game: get_string_async() causes games to come out of fullscreen, minimised and paused YoYoGames/GameMaker-Bugs#2255 * Added a note to the Dialog page, explaining that you can use borderless fullscreen if you want to use these functions * Moved notes on the async function pages to a snippet and inserted that * Some other visual changes on the pages
  [Feature]Included Files Refresh Button YoYoGames/GameMaker-Bugs#2729 * Updated screenshots on page, added an icon image for the Refresh button * Added a line about the button
  [Feature]Textures that are resized to fit a texture page size end up on a texture page of a smaller size YoYoGames/GameMaker-Bugs#4386 * Updated screenshot & removed list items for actual width & height on The Debug Overlay page
  [Feature]String Literal Hash Optimization YoYoGames/GameMaker-Bugs#2804 YoYoGames/GameMaker-Bugs#2821 * Added a note under "Struct Functions" on the Variable Functions page
  [General]Manual+In-Game: Feather/Manual info for audio_destroy_stream() is incorrect, and the function does not return its intended value correctly YoYoGames/GameMaker-Bugs#4415 * Changed the parameter: renamed to "sound", type changed to Sound Asset, return value to Real (int32 isn't a type we've documented so far in the manual) * Tiny other changes to the page
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature (or a request for one)
Projects
Archived in project
Development

No branches or pull requests

6 participants