Skip to content

Conversation

@AntonOresten
Copy link
Contributor

I ran into some construction issues when a type that was being freed couldn't be reconstructed. You could argue that type should have proper constructors, but since we want to only access and free the leaves anyway, we may as well use fmapstructure instead of fmap. Does this risk type pollution, or does it not matter?

@gemini-code-assist
Copy link

Summary of Changes

Hello @AntonOresten, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request updates the unsafe_free! function within the Internal module to leverage fmapstructure from the Functors package. The primary motivation is to enhance the robustness of the freeing mechanism, particularly for complex data structures, by ensuring that the operation correctly targets and frees only the terminal elements (leaves) without encountering reconstruction problems for intermediate types.

Highlights

  • Refactor unsafe_free!: The unsafe_free! function in Internal module has been refactored to use fmapstructure instead of fmap.
  • Address construction issues: This change aims to resolve issues where types being freed could not be reconstructed, by ensuring only the 'leaves' of a structure are accessed and freed.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue with unsafe_free! where fmap could fail if a type cannot be reconstructed. Your change to fmapstructure correctly solves this problem. To answer your question in the description: since the return value is discarded, using fmapstructure does not risk type pollution here and is perfectly safe.

However, we can make this more efficient and arguably clearer. fmapstructure still builds a new structure which is then discarded. A better approach for applying a function for its side-effects is to iterate over the leaves using fleaves from Functors.jl. I've added specific suggestions to implement this using foreach for a concise and allocation-free solution.

@AntonOresten AntonOresten changed the title Use fmapstructure in Internal.unsafe_free! Avoid reconstruction in Internal.unsafe_free! Nov 13, 2025
Copy link
Member

@avik-pal avik-pal left a comment

Choose a reason for hiding this comment

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

This looks good to me. We can merge once tests pass

Can you bump the patch version?

@AntonOresten
Copy link
Contributor Author

AntonOresten commented Nov 13, 2025

Done! Thanks.

and for the record, Gemini was helpful:
image

@AntonOresten
Copy link
Contributor Author

AntonOresten commented Nov 13, 2025

Are the regressions relevant, or just noise / within the margin of error?
Should I try fmapstructure again?

@avik-pal
Copy link
Member

No those are well within the tolerance for gh runners

@avik-pal avik-pal merged commit 584ca77 into LuxDL:main Nov 13, 2025
67 of 72 checks passed
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