Skip to content

Commit 5616268

Browse files
authored
Merge pull request #75 from ruby/avoid_slow_flatten
Fix slow completion on method chain of huge arrays and hashes (size >= 10000000)
2 parents a3c17cd + eb0cec2 commit 5616268

1 file changed

Lines changed: 10 additions & 3 deletions

File tree

lib/repl_type_completor/types.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,13 @@ def self.union_type_from_objects(objects)
216216
UnionType[*class_instances.map { InstanceType.new _1, nil, _2 }, *modules.uniq.map { SingletonType.new _1 }]
217217
end
218218

219+
def self.union_type_from_objects_list(objects_list)
220+
objects = objects_list.flat_map do |objects|
221+
objects.size < OBJECT_TO_TYPE_SAMPLE_SIZE ? objects : objects.sample(OBJECT_TO_TYPE_SAMPLE_SIZE)
222+
end
223+
union_type_from_objects(objects)
224+
end
225+
219226
class SingletonType
220227
attr_reader :module_or_class
221228
def initialize(module_or_class)
@@ -268,11 +275,11 @@ def expand_params
268275
return params unless @instances
269276

270277
if @klass == Array
271-
type = Types.union_type_from_objects(@instances.flatten(1))
278+
type = Types.union_type_from_objects_list(@instances)
272279
{ Elem: UnionType[*params[:Elem], *type] }
273280
elsif @klass == Hash
274-
key = Types.union_type_from_objects(@instances.map(&:keys).flatten(1))
275-
value = Types.union_type_from_objects(@instances.map(&:values).flatten(1))
281+
key = Types.union_type_from_objects_list(@instances.map(&:keys))
282+
value = Types.union_type_from_objects_list(@instances.map(&:values))
276283
{
277284
K: UnionType[*params[:K], key],
278285
V: UnionType[*params[:V], value]

0 commit comments

Comments
 (0)