Conversation
The application_plan_name method in transactor/status is not called from any other part of the codebase.
eguzki
left a comment
There was a problem hiding this comment.
LGTM
However, building xml's using a library like Nokogiri::XML::Builder seems more appropriate. It is always less error prone than using string concatenations.
| end | ||
|
|
||
| def add_authorize_section(xml) | ||
| xml << '<authorized>' |
There was a problem hiding this comment.
This is not frozen and will allocate plenty of strings.
There was a problem hiding this comment.
I have updated the code to freeze the strings.
Thank you.
1a90d06 to
78b3bd4
Compare
|
Regarding the use of Nokogiri::XML::Builder, I agree with you @eguzki The idea is to move this code out of transactor/Status.rb in another PR, in order to decouple the data representation from the model, along with trying to generalize the data representation to be compatible with other formats (like json). |
78b3bd4 to
669f5eb
Compare
669f5eb to
18f9a08
Compare
| def add_authorize_section(xml) | ||
| xml << '<authorized>'.freeze | ||
| if authorized? | ||
| xml << 'true'.freeze << '</authorized>'.freeze |
There was a problem hiding this comment.
you can make this perform better with a single xml << '<authorized>true</authorized>'.freeze
| xml << 'true'.freeze << '</authorized>'.freeze | ||
| else | ||
| xml << 'false'.freeze << '</authorized>'.freeze | ||
| xml << '<reason>'.freeze |
There was a problem hiding this comment.
here you can do xml << '<authorized>false</authorized><reason>'.freeze
There was a problem hiding this comment.
the same principle applies to other places in the added methods
There was a problem hiding this comment.
👍 . Done. In some other places I left it the way it is for readability
18f9a08 to
f1b0a91
Compare
f1b0a91 to
2bedf8b
Compare
| redirect_uri = application.redirect_url | ||
| xml << "<application><id>".freeze << "#{application.id}" << "</id>".freeze | ||
| xml << "<key>".freeze << "#{application.keys.first}" << "</key>".freeze | ||
| xml << "<#{@redirect_uri_field}>#{redirect_uri}</#{@redirect_uri_field}>" |
There was a problem hiding this comment.
you can join here literals in a single string: "</id><key>", "</key><" as it will avoid a couple of string concatenation ops.
| end | ||
|
|
||
| def add_user_section(xml) | ||
| if !@user.nil? |
There was a problem hiding this comment.
have you considered moving the check to the caller?
2bedf8b to
2e77a83
Compare
unleashed
left a comment
There was a problem hiding this comment.
Check my comment, it's the only remaining thing.
| xml << "<application><id>".freeze << "#{application.id}" << "</id><key>".freeze | ||
| xml << "#{application.keys.first}" << "</key><".freeze | ||
| xml << "#{@redirect_uri_field}>#{redirect_uri}</#{@redirect_uri_field}" | ||
| xml << '></application>'.freeze |
There was a problem hiding this comment.
here you could move the "</key><" literal to prefix and the '></application>' literal to suffix the line with the redirects to avoid two << operations.
There was a problem hiding this comment.
If I do that then this literals will not be frozen even if I put a .freeze, which we were trying to do for the literal parts as mikz commented.
There was a problem hiding this comment.
@miguelsorianod but there is no good reason to have extra frozen string when you are allocating it moments before anyway
RubyVM::InstructionSequence.compile(<<'RUBY').disasm
xml << "#{@redirect_uri_field}>#{redirect_uri}</#{@redirect_uri_field}";
xml << '></application>'.freeze
RUBY== disasm: #<ISeq:<compiled>@<compiled>>================================
0000 trace 1 ( 1)
0002 putself
0003 opt_send_without_block <callinfo!mid:xml, argc:0, FCALL|VCALL|ARGS_SIMPLE>, <callcache>
0006 getinstancevariable :@redirect_uri_field, <is:0>
0009 tostring
0010 putobject ">"
0012 putself
0013 opt_send_without_block <callinfo!mid:redirect_uri, argc:0, FCALL|VCALL|ARGS_SIMPLE>, <callcache>
0016 tostring
0017 putobject "</"
0019 getinstancevariable :@redirect_uri_field, <is:0>
0022 tostring
0023 concatstrings 5
0025 opt_ltlt <callinfo!mid:<<, argc:1, ARGS_SIMPLE>, <callcache>
0028 pop
0029 trace 1 ( 2)
0031 putself
0032 opt_send_without_block <callinfo!mid:xml, argc:0, FCALL|VCALL|ARGS_SIMPLE>, <callcache>
0035 opt_str_freeze "></application>"
0037 opt_ltlt <callinfo!mid:<<, argc:1, ARGS_SIMPLE>, <callcache>
0040 leave
versus
RubyVM::InstructionSequence.compile(<<'RUBY').disasm
xml << "#{@redirect_uri_field}>#{redirect_uri}</#{@redirect_uri_field}></application>"
RUBY== disasm: #<ISeq:<compiled>@<compiled>>================================
0000 trace 1 ( 1)
0002 putself
0003 opt_send_without_block <callinfo!mid:xml, argc:0, FCALL|VCALL|ARGS_SIMPLE>, <callcache>
0006 getinstancevariable :@redirect_uri_field, <is:0>
0009 tostring
0010 putobject ">"
0012 putself
0013 opt_send_without_block <callinfo!mid:redirect_uri, argc:0, FCALL|VCALL|ARGS_SIMPLE>, <callcache>
0016 tostring
0017 putobject "</"
0019 getinstancevariable :@redirect_uri_field, <is:0>
0022 tostring
0023 putobject "></application>"
0025 concatstrings 6
0027 opt_ltlt <callinfo!mid:<<, argc:1, ARGS_SIMPLE>, <callcache>
0030 leave
There was a problem hiding this comment.
in other words: there is one allocation already going on there with the interpolation, and freezing and adding the frozen strings adds two extra operations, whereas those strings could already be part of the allocated string in the first place.
There was a problem hiding this comment.
I understand that an allocation is less costly that an allocation + a concatenation in that specific case. My doubt was about what happened if that same literal would appear in other places of the code.
The solution then would be to freeze the '>' part in the other part of the code. It would not make sense to freeze it in this specific section of the code because anyway you will be doing an allocation for the redirect part. I'll change it now, thank you!
There was a problem hiding this comment.
The thing is what are you willing to optimize for. If you have the same literals sprinkled elsewhere you can make all of them frozen, only some, or none. With strings as interpreted in MRI, using .freeze makes the effect of interning them, which is to say they become similar to static, immutable constants. In that case, when the interpreter finds references to the same string (and also with .freeze*), it will refer to the interned one to save on unneeded allocations and extra copies.
But of course, having a few copies of the same string (or more accurately, substring) to save in execution speed is usually a good tradeoff. So, in some cases you can intern them, but it only makes sense if you actually save the allocation, and if that's not the case, it's worse if it also adds an extra cost in execution time.
*: Ruby 2.3+ have flags for making the String#freeze behaviour the default with literals.
There was a problem hiding this comment.
I understand that an allocation is less costly that an allocation + a concatenation in that specific case. My doubt was about what happened if that same literal would appear in other places of the code.
The solution then would be to freeze the '>' part in the other part of the code. It would not make sense to freeze it in this specific section of the code because anyway you will be doing an allocation for the redirect part. I have changed it and pushed the changes already, thank you!
There was a problem hiding this comment.
This is 93% the same comment as above! I want my 10s back!!
2e77a83 to
6718e90
Compare
| def add_application_section(xml) | ||
| redirect_uri = application.redirect_url | ||
| xml << '<application>' \ | ||
| xml << "<application>" \ |
There was a problem hiding this comment.
I'd say this was correctly using single quotes. There is no interpolation within them so it signals to the reader: don't worry, this is the actual string, no interpolation there.
…tions The to_xml method was checking for exclude_application and exclude_user options but there are not any references to this options in the code. We remove them.
6718e90 to
97ae3ab
Compare
|
bors r=@eguzki,@davidor,@mikz,@unleashed |
26: Refactor/transactor status r=eguzki,davidor,mikz,unleashed a=miguelsorianod I have refactored some parts of the transactor/status class. Also I have removed some unreferenced methods/code from the class. I removed the options on the to_xml method named 'exclude_application' and 'exclude_user' because are not referenced/set anywhere on the code and seem to be legacy options (it seems were added in commit 3424475). Maybe @unleashed / @mikz that have been longer with the codebase can confirm this. Thank you. Co-authored-by: Miguel Soriano <msoriano@redhat.com>
Build failed |
|
bors r=@eguzki,@davidor,@mikz,@unleashed |
26: Refactor/transactor status r=eguzki,davidor,mikz,unleashed a=miguelsorianod I have refactored some parts of the transactor/status class. Also I have removed some unreferenced methods/code from the class. I removed the options on the to_xml method named 'exclude_application' and 'exclude_user' because are not referenced/set anywhere on the code and seem to be legacy options (it seems were added in commit 3424475). Maybe @unleashed / @mikz that have been longer with the codebase can confirm this. Thank you. Co-authored-by: Miguel Soriano <msoriano@redhat.com>
Build failed |
|
last try, otherwise it is clear that some powerful, higher order entity does not want this merged, and if so better not to annoy them (a la tercera va la vencida) bors r=@eguzki,@davidor,@mikz,@unleashed |
26: Refactor/transactor status r=eguzki,davidor,mikz,unleashed a=miguelsorianod I have refactored some parts of the transactor/status class. Also I have removed some unreferenced methods/code from the class. I removed the options on the to_xml method named 'exclude_application' and 'exclude_user' because are not referenced/set anywhere on the code and seem to be legacy options (it seems were added in commit 3424475). Maybe @unleashed / @mikz that have been longer with the codebase can confirm this. Thank you. Co-authored-by: Miguel Soriano <msoriano@redhat.com>
Build succeeded |
I have refactored some parts of the transactor/status class.
Also I have removed some unreferenced methods/code from the class. I removed the options on the to_xml method named 'exclude_application' and 'exclude_user' because are not referenced/set anywhere on the code and seem to be legacy options (it seems were added in commit 3424475). Maybe @unleashed / @mikz that have been longer with the codebase can confirm this.
Thank you.