-
Notifications
You must be signed in to change notification settings - Fork 32
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
Remove slice! from fixed_protocol #1207
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -102,7 +102,7 @@ def identify_and_finish_packet | |
end | ||
|
||
if unique_id_mode | ||
target_packets.each do |packet_name, packet| | ||
target_packets.each do |_packet_name, packet| | ||
if packet.identify?(@data[@discard_leading_bytes..-1]) | ||
identified_packet = packet | ||
break | ||
|
@@ -136,7 +136,13 @@ def identify_and_finish_packet | |
@packet_name = identified_packet.packet_name | ||
|
||
# Get the data from this packet | ||
packet_data = @data.slice!(0, identified_packet.defined_length + @discard_leading_bytes) | ||
# Previous implementation looked like the following: | ||
# packet_data = @data.slice!(0, identified_packet.defined_length + @discard_leading_bytes) | ||
# But slice! is 6x slower at small packets (1024) | ||
# and 1000x slower at large packets (1Mb) | ||
# Run test/benchmarks/string_mod_benchmark.rb for details | ||
packet_data = @data[0...identified_packet.defined_length + @discard_leading_bytes] | ||
@data = @data[(identified_packet.defined_length + @discard_leading_bytes)..-1] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is equivalent functionality. I tried to add a new test case to the spec but not sure if I fully covered the case because if you change |
||
break | ||
end | ||
end | ||
|
@@ -152,7 +158,7 @@ def identify_and_finish_packet | |
@data.replace('') | ||
end | ||
|
||
return packet_data | ||
return packet_data, @extra | ||
end | ||
|
||
def reduce_to_single_packet | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# encoding: ascii-8bit | ||
|
||
# Copyright 2023 OpenC3, Inc. | ||
# All Rights Reserved. | ||
# | ||
# This program is free software; you can modify and/or redistribute it | ||
# under the terms of the GNU Affero General Public License | ||
# as published by the Free Software Foundation; version 3 with | ||
# attribution addendums as found in the LICENSE.txt | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# This file may also be used under the terms of a commercial license | ||
# if purchased from OpenC3, Inc. | ||
|
||
require 'securerandom' | ||
require 'benchmark' | ||
|
||
Benchmark.bm do |x| | ||
data = SecureRandom.random_bytes(1024) | ||
x.report("slice!") do | ||
1000.times.each { data.slice!(0, 1) } | ||
end | ||
data = SecureRandom.random_bytes(1024) | ||
x.report("assign") do | ||
1000.times.each { data = data[1..-1] } | ||
end | ||
data = SecureRandom.random_bytes(1024) | ||
x.report("replace") do | ||
1000.times.each { data.replace(data[1..-1]) } | ||
end | ||
end | ||
|
||
Benchmark.bm do |x| | ||
data = SecureRandom.random_bytes(1024*1024) | ||
x.report("slice!") do | ||
1000.times.each { data.slice!(0, 1) } | ||
end | ||
data = SecureRandom.random_bytes(1024*1024) | ||
x.report("assign") do | ||
1000.times.each { data = data[1..-1] } | ||
end | ||
data = SecureRandom.random_bytes(1024*1024) | ||
x.report("replace") do | ||
1000.times.each { data.replace(data[1..-1]) } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this intentionally ... and shouldn't there be parens around the + expression?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Order of operations applies plus before range but it's clearer with parens