Skip to content

Commit 09b98f5

Browse files
committed
Lint/HashCompareByIdentity
Hash#compare_by_identity is intentionally not used since we only need to keep track of object ids we've seen before, based on object_id. If we were to use compare_by_identity, then we'd need to store entire objects in memory.
1 parent 5153417 commit 09b98f5

File tree

3 files changed

+10
-9
lines changed

3 files changed

+10
-9
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,11 +590,6 @@ Lint/EnsureReturn:
590590
Exclude:
591591
- 'lib/puppet/configurer.rb'
592592

593-
Lint/HashCompareByIdentity:
594-
Exclude:
595-
- 'lib/puppet/pops/serialization/serializer.rb'
596-
- 'lib/puppet/pops/types/recursion_guard.rb'
597-
598593
# This cop supports safe auto-correction (--auto-correct).
599594
# Configuration parameters: EnforcedStyle.
600595
# SupportedStyles: standard_error, runtime_error

lib/puppet/pops/serialization/serializer.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class Serializer
1616
# @option options [Boolean] :type_by_reference `true` if Object types are serialized by name only.
1717
# @api public
1818
def initialize(writer, options = EMPTY_HASH)
19+
# Hash#compare_by_identity is intentionally not used since we only need to map
20+
# object ids to their size and we don't want to store entire objects in memory
1921
@written = {}
2022
@writer = writer
2123
@options = options
@@ -37,7 +39,7 @@ def write(value)
3739
when :default
3840
@writer.write(Extension::Default::INSTANCE)
3941
else
40-
index = @written[value.object_id]
42+
index = @written[value.object_id] # rubocop:disable Lint/HashCompareByIdentity
4143
if index.nil?
4244
write_tabulated_first_time(value)
4345
else
@@ -76,7 +78,7 @@ def start_object(attr_count)
7678
end
7779

7880
def push_written(value)
79-
@written[value.object_id] = @written.size
81+
@written[value.object_id] = @written.size # rubocop:disable Lint/HashCompareByIdentity
8082
end
8183

8284
# Write the start of a sensitive object

lib/puppet/pops/types/recursion_guard.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ module Types
88
#
99
# All comparisons are made using the `object_id` of the instance rather than the instance itself.
1010
#
11+
# Hash#compare_by_identity is intentionally not used since we only need to keep
12+
# track of object ids we've seen before, based on object_id, so we don't store
13+
# entire objects in memory.
14+
#
1115
# @api private
1216
class RecursionGuard
1317
attr_reader :state
@@ -25,14 +29,14 @@ def initialize
2529
# @param instance [Object] the instance to check
2630
# @return [Integer] the resulting state
2731
def recursive_this?(instance)
28-
instance_variable_defined?(:@recursive_this_map) && @recursive_this_map.has_key?(instance.object_id)
32+
instance_variable_defined?(:@recursive_this_map) && @recursive_this_map.has_key?(instance.object_id) # rubocop:disable Lint/HashCompareByIdentity
2933
end
3034

3135
# Checks if recursion was detected for the given argument in the 'that' context
3236
# @param instance [Object] the instance to check
3337
# @return [Integer] the resulting state
3438
def recursive_that?(instance)
35-
instance_variable_defined?(:@recursive_that_map) && @recursive_that_map.has_key?(instance.object_id)
39+
instance_variable_defined?(:@recursive_that_map) && @recursive_that_map.has_key?(instance.object_id) # rubocop:disable Lint/HashCompareByIdentity
3640
end
3741

3842
# Add the given argument as 'this' invoke the given block with the resulting state

0 commit comments

Comments
 (0)