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

Avoid memoizing dependencies #1926

Merged
merged 1 commit into from
Apr 22, 2024
Merged

Conversation

vinistock
Copy link
Member

Motivation

I noticed that we were keeping the project's dependencies in memory forever, but we only need that during boot. Let's save some memory and allow the array to be garbage collected.

Implementation

I turned the array into a local variable that gets used during apply_options.

Automated Tests

Adapted our tests.

@vinistock vinistock added server This pull request should be included in the server gem's release notes other Changes that aren't bugfixes, enhancements or breaking changes labels Apr 12, 2024
@vinistock vinistock self-assigned this Apr 12, 2024
@vinistock vinistock requested a review from a team as a code owner April 12, 2024 20:14
@vinistock vinistock requested review from andyw8 and st0012 April 12, 2024 20:14
Copy link
Member

@st0012 st0012 left a comment

Choose a reason for hiding this comment

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

I think it's fine to not storing the dependencies, but do we also need to change the timing test library and typechecker are detected? To me it's quite odd that we now need to rely on apply_options to detect those attributes even though they actually don't care about the options.
IMO we can just also call gather_dependencies in initialize too and keep the same behaviour.

@vinistock
Copy link
Member Author

Why do you find it odd? To me it's a bit more organized to have default values in the initialize and then all automatic detection happens once we apply user options.

@st0012
Copy link
Member

st0012 commented Apr 12, 2024

I'd expect apply_options to only change attributes that rely on options, which is the current behaviour.
In a way, it seems to blur the boundary between GlobalState#initialize and GlobalState#apply_options as I can only assume GlobalState is fully initialized after we called apply_opitons on it. I think this is clearly demonstrated by the test changes:

state = GlobalState.new
state.apply_options({})
assert_equal("rails", state.test_library)

@vinistock
Copy link
Member Author

This ends up being a consequence of Sorbet. It's not possible to have non-nilable instance variables without putting them in the initializer, but we are not able to fully initialize the GlobalState until we receive the LSP's initialize request.

Ideally, we would do something like this

def run_initialize(message)
  @global_state = GlobalState.new(message)
end

But there's no way to get proper typing that way. Even if we duplicate the dependencies array and move the test framework and type checker detection inside initialize, the problem you describe isn't solved. The global state is still only fully initialized once you invoke apply_options.

It just wouldn't impact those specific tests, but conceptually, the global state is never fully initialized until the user options are applied.

@vinistock vinistock force-pushed the vs/allow_separate_linter_configuration branch from 1326854 to 019c876 Compare April 22, 2024 19:48
Base automatically changed from vs/allow_separate_linter_configuration to main April 22, 2024 20:38
@vinistock vinistock force-pushed the vs/avoid_memoizing_dependencies branch from e050ed1 to f2e23de Compare April 22, 2024 20:43
@vinistock vinistock enabled auto-merge (squash) April 22, 2024 20:43
@vinistock vinistock merged commit 28b6d11 into main Apr 22, 2024
42 checks passed
@vinistock vinistock deleted the vs/avoid_memoizing_dependencies branch April 22, 2024 20:53
st0012 added a commit that referenced this pull request May 31, 2024
As stated in this comment: #1926 (comment)
A global state is not fully initialized without `apply_options` being called,
which the current `with_server` test helper does not do.

This means that when tests are being run, detections around things like
test library are not performed, which can yield unexpected results to
developers, especially addon authors.

This commit changes the `with_server` test helper to call `apply_options`.
st0012 added a commit that referenced this pull request May 31, 2024
As stated in this comment: #1926 (comment)
A global state is not fully initialized without `apply_options` being called,
which the current `with_server` test helper does not do.

This means that when tests are being run, detections around things like
test library are not performed, which can yield unexpected results to
developers, especially addon authors.

This commit changes the `with_server` test helper to call `apply_options`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
other Changes that aren't bugfixes, enhancements or breaking changes server This pull request should be included in the server gem's release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants