Skip to content

Commit 561377b

Browse files
committed
Fix slow completion on method chain of huge arrays(size >= 10000000)
1 parent a3c17cd commit 561377b

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

lib/repl_type_completor/types.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ 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+
union_type_from_objects(
221+
objects_list.flat_map {|objects| objects.size < OBJECT_TO_TYPE_SAMPLE_SIZE ? objects : objects.sample(OBJECT_TO_TYPE_SAMPLE_SIZE) }
222+
)
223+
end
224+
219225
class SingletonType
220226
attr_reader :module_or_class
221227
def initialize(module_or_class)
@@ -268,11 +274,11 @@ def expand_params
268274
return params unless @instances
269275

270276
if @klass == Array
271-
type = Types.union_type_from_objects(@instances.flatten(1))
277+
type = Types.union_type_from_objects_list(@instances)
272278
{ Elem: UnionType[*params[:Elem], *type] }
273279
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))
280+
key = Types.union_type_from_objects_list(@instances.map(&:keys))
281+
value = Types.union_type_from_objects_list(@instances.map(&:values))
276282
{
277283
K: UnionType[*params[:K], key],
278284
V: UnionType[*params[:V], value]

0 commit comments

Comments
 (0)