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

Prevent creation of unnecessary fieldset(mirror and revised) #2450

Open
wants to merge 1 commit into
base: 0-10-stable
Choose a base branch
from

Conversation

liijunwei
Copy link

@liijunwei liijunwei commented Aug 12, 2023

Purpose

see: #2370

Changes

  • change#1: only initialize ActiveModel::Serializer::Fieldset when fieldset is not nil
  • change#2: only ask for fields_for when fieldset is not nil

Caveats

NA

Related GitHub issues

Additional helpful information

@@ -348,7 +349,8 @@ def data_for(serializer, include_slice)
data.tap do |resource_object|
next if resource_object.nil?
# NOTE(BF): the attributes are cached above, separately from the relationships, below.
requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
Copy link
Author

@liijunwei liijunwei Aug 12, 2023

Choose a reason for hiding this comment

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

change#2: only ask for fields_for when fieldset is not nil

this is not a change, it just saves the fields_for cost

also align with above

@liijunwei liijunwei force-pushed the prevent_creation_of_unnecessary_fieldset branch 2 times, most recently from aa20faa to 1af09b5 Compare August 12, 2023 11:37
@liijunwei
Copy link
Author

hello @bf4 , sorry about pinging directly, could you take a look when you have a moment ? 🙏

@bf4
Copy link
Member

bf4 commented Aug 14, 2023

I'm on vacation and haven't read too carefully

If tests pass and it works for people, that's fine with me

There are some changes which are refactors which may introduce possible bugs. When that's the case, it's often worth extracting just the refactors and any work which supports the change you want, but doesn't change behavior, so that the actual code change which changes the behavior is smaller and safer and easier to review. That is, make a new pr without the behavior changes, merge it, then rebase/update the feature pr

@liijunwei
Copy link
Author

liijunwei commented Aug 15, 2023

ok, let me separate the change from refactoring
thank you for the advice.

have a nice vacation :D

@liijunwei liijunwei force-pushed the prevent_creation_of_unnecessary_fieldset branch 2 times, most recently from 26a1dd8 to c34dc87 Compare August 15, 2023 01:20
@@ -5,7 +5,11 @@ module Adapter
class Attributes < Base
def initialize(*)
super
instance_options[:fieldset] ||= ActiveModel::Serializer::Fieldset.new(fields_to_fieldset(instance_options.delete(:fields)))

instance_options[:fieldset] ||= begin
Copy link
Author

@liijunwei liijunwei Aug 15, 2023

Choose a reason for hiding this comment

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

change#1: only initialize ActiveModel::Serializer::Fieldset when fieldset is not nil

instance_options[:fieldset] could be:

  1. the fieldset passed in through option
  2. ActiveModel::Serializer::Fieldset instance
  3. nil

Copy link
Member

Choose a reason for hiding this comment

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

That's fine. Or add a class method on fieldset which does what's in this begin block

Copy link
Author

Choose a reason for hiding this comment

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

echoing what you previously mentioned, I also want keep the changes minimal 🙏
if it's necessary, is it better to do it in another refactoring PR?

@@ -53,7 +53,7 @@ def self.fragment_cache(cached_hash, non_cached_hash, root = true)
def initialize(serializer, options = {})
super
@include_directive = JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
@fieldset = options[:fieldset] || ActiveModel::Serializer::Fieldset.new(options.delete(:fields))
@fieldset = options[:fieldset] || ((option_fields = options.delete(:fields)) && ActiveModel::Serializer::Fieldset.new(option_fields))
Copy link
Author

Choose a reason for hiding this comment

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

@fieldset could be:

  1. the fieldset passed in through option
  2. ActiveModel::Serializer::Fieldset instance
  3. nil

@liijunwei
Copy link
Author

Updated: only include desired changes for performance optimization

@liijunwei liijunwei force-pushed the prevent_creation_of_unnecessary_fieldset branch 2 times, most recently from 40befea to 73420cd Compare August 15, 2023 05:07
@liijunwei liijunwei force-pushed the prevent_creation_of_unnecessary_fieldset branch from 73420cd to a47c639 Compare August 15, 2023 05:08
@liijunwei
Copy link
Author

hello @bf4 , sorry to bother , just would like to see whether the changes looks good to you ? Orz

@bf4
Copy link
Member

bf4 commented Sep 1, 2023

Seems fine

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