google-protobuf 3.25.0 → 4.29.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. checksums.yaml +4 -4
  2. data/ext/google/protobuf_c/convert.c +46 -18
  3. data/ext/google/protobuf_c/defs.c +499 -26
  4. data/ext/google/protobuf_c/extconf.rb +1 -1
  5. data/ext/google/protobuf_c/glue.c +53 -2
  6. data/ext/google/protobuf_c/map.c +82 -17
  7. data/ext/google/protobuf_c/map.h +9 -2
  8. data/ext/google/protobuf_c/message.c +144 -104
  9. data/ext/google/protobuf_c/message.h +8 -5
  10. data/ext/google/protobuf_c/protobuf.c +30 -17
  11. data/ext/google/protobuf_c/protobuf.h +3 -7
  12. data/ext/google/protobuf_c/repeated_field.c +64 -10
  13. data/ext/google/protobuf_c/repeated_field.h +8 -1
  14. data/ext/google/protobuf_c/ruby-upb.c +13774 -11526
  15. data/ext/google/protobuf_c/ruby-upb.h +11198 -9048
  16. data/ext/google/protobuf_c/shared_convert.c +10 -5
  17. data/ext/google/protobuf_c/shared_convert.h +2 -2
  18. data/ext/google/protobuf_c/shared_message.c +3 -31
  19. data/ext/google/protobuf_c/shared_message.h +0 -4
  20. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.c +467 -0
  21. data/ext/google/protobuf_c/third_party/utf8_range/utf8_range.h +9 -8
  22. data/lib/google/protobuf/any_pb.rb +1 -22
  23. data/lib/google/protobuf/api_pb.rb +1 -24
  24. data/lib/google/protobuf/descriptor_pb.rb +3 -23
  25. data/lib/google/protobuf/duration_pb.rb +1 -22
  26. data/lib/google/protobuf/empty_pb.rb +1 -22
  27. data/lib/google/protobuf/ffi/descriptor.rb +13 -2
  28. data/lib/google/protobuf/ffi/descriptor_pool.rb +16 -9
  29. data/lib/google/protobuf/ffi/enum_descriptor.rb +13 -1
  30. data/lib/google/protobuf/ffi/ffi.rb +8 -6
  31. data/lib/google/protobuf/ffi/field_descriptor.rb +37 -16
  32. data/lib/google/protobuf/ffi/file_descriptor.rb +13 -12
  33. data/lib/google/protobuf/ffi/internal/arena.rb +0 -6
  34. data/lib/google/protobuf/ffi/internal/convert.rb +21 -30
  35. data/lib/google/protobuf/ffi/map.rb +50 -13
  36. data/lib/google/protobuf/ffi/message.rb +202 -58
  37. data/lib/google/protobuf/ffi/method_descriptor.rb +114 -0
  38. data/lib/google/protobuf/ffi/object_cache.rb +3 -3
  39. data/lib/google/protobuf/ffi/oneof_descriptor.rb +20 -11
  40. data/lib/google/protobuf/ffi/repeated_field.rb +50 -142
  41. data/lib/google/protobuf/ffi/service_descriptor.rb +107 -0
  42. data/lib/google/protobuf/field_mask_pb.rb +1 -22
  43. data/lib/google/protobuf/internal/object_cache.rb +99 -0
  44. data/lib/google/protobuf/plugin_pb.rb +2 -24
  45. data/lib/google/protobuf/repeated_field.rb +4 -5
  46. data/lib/google/protobuf/source_context_pb.rb +1 -22
  47. data/lib/google/protobuf/struct_pb.rb +1 -22
  48. data/lib/google/protobuf/timestamp_pb.rb +1 -22
  49. data/lib/google/protobuf/type_pb.rb +1 -24
  50. data/lib/google/protobuf/wrappers_pb.rb +1 -22
  51. data/lib/google/protobuf.rb +1 -1
  52. data/lib/google/protobuf_ffi.rb +3 -2
  53. data/lib/google/protobuf_native.rb +0 -1
  54. data/lib/google/tasks/ffi.rake +1 -3
  55. metadata +25 -12
  56. data/ext/google/protobuf_c/third_party/utf8_range/naive.c +0 -92
  57. data/ext/google/protobuf_c/third_party/utf8_range/range2-neon.c +0 -157
  58. data/ext/google/protobuf_c/third_party/utf8_range/range2-sse.c +0 -170
  59. data/lib/google/protobuf/descriptor_dsl.rb +0 -465
  60. data/lib/google/protobuf/object_cache.rb +0 -97
@@ -22,10 +22,7 @@ module Google
22
22
  # @param value [OneofDescriptor] FieldDescriptor to convert to an FFI native type
23
23
  # @param _ [Object] Unused
24
24
  def to_native(value, _ = nil)
25
- oneof_def_ptr = value.instance_variable_get(:@oneof_def)
26
- warn "Underlying oneof_def was nil!" if oneof_def_ptr.nil?
27
- raise "Underlying oneof_def was null!" if !oneof_def_ptr.nil? and oneof_def_ptr.null?
28
- oneof_def_ptr
25
+ value.instance_variable_get(:@oneof_def) || ::FFI::Pointer::NULL
29
26
  end
30
27
 
31
28
  ##
@@ -56,6 +53,17 @@ module Google
56
53
  nil
57
54
  end
58
55
 
56
+ def options
57
+ @options ||= begin
58
+ size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
59
+ temporary_arena = Google::Protobuf::FFI.create_arena
60
+ buffer = Google::Protobuf::FFI.oneof_options(self, size_ptr, temporary_arena)
61
+ opts = Google::Protobuf::OneofOptions.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze)
62
+ opts.clear_features()
63
+ opts.freeze
64
+ end
65
+ end
66
+
59
67
  private
60
68
 
61
69
  def initialize(oneof_def, descriptor_pool)
@@ -72,17 +80,18 @@ module Google
72
80
 
73
81
  class FFI
74
82
  # MessageDef
75
- attach_function :get_oneof_by_name, :upb_MessageDef_FindOneofByNameWithSize, [Descriptor, :string, :size_t], OneofDescriptor
76
- attach_function :get_oneof_by_index, :upb_MessageDef_Oneof, [Descriptor, :int], OneofDescriptor
83
+ attach_function :get_oneof_by_name, :upb_MessageDef_FindOneofByNameWithSize, [Descriptor, :string, :size_t], OneofDescriptor
84
+ attach_function :get_oneof_by_index, :upb_MessageDef_Oneof, [Descriptor, :int], OneofDescriptor
77
85
 
78
86
  # OneofDescriptor
79
- attach_function :get_oneof_name, :upb_OneofDef_Name, [OneofDescriptor], :string
80
- attach_function :get_oneof_field_count, :upb_OneofDef_FieldCount, [OneofDescriptor], :int
81
- attach_function :get_oneof_field_by_index, :upb_OneofDef_Field, [OneofDescriptor, :int], FieldDescriptor
82
- attach_function :get_oneof_containing_type,:upb_OneofDef_ContainingType,[:pointer], Descriptor
87
+ attach_function :get_oneof_name, :upb_OneofDef_Name, [OneofDescriptor], :string
88
+ attach_function :get_oneof_field_count, :upb_OneofDef_FieldCount, [OneofDescriptor], :int
89
+ attach_function :get_oneof_field_by_index, :upb_OneofDef_Field, [OneofDescriptor, :int], FieldDescriptor
90
+ attach_function :get_oneof_containing_type,:upb_OneofDef_ContainingType, [:pointer], Descriptor
91
+ attach_function :oneof_options, :OneOfDescriptor_serialized_options, [OneofDescriptor, :pointer, Internal::Arena], :pointer
83
92
 
84
93
  # FieldDescriptor
85
- attach_function :real_containing_oneof, :upb_FieldDef_RealContainingOneof,[FieldDescriptor], OneofDescriptor
94
+ attach_function :real_containing_oneof, :upb_FieldDef_RealContainingOneof, [FieldDescriptor], OneofDescriptor
86
95
  end
87
96
  end
88
97
  end
@@ -5,8 +5,6 @@
5
5
  # license that can be found in the LICENSE file or at
6
6
  # https://developers.google.com/open-source/licenses/bsd
7
7
 
8
- require 'forwardable'
9
-
10
8
  #
11
9
  # This class makes RepeatedField act (almost-) like a Ruby Array.
12
10
  # It has convenience methods that extend the core C or Java based
@@ -15,7 +13,7 @@ require 'forwardable'
15
13
  # This is a best-effort to mirror Array behavior. Two comments:
16
14
  # 1) patches always welcome :)
17
15
  # 2) if performance is an issue, feel free to rewrite the method
18
- # in jruby and C. The source code has plenty of examples
16
+ # in C. The source code has plenty of examples
19
17
  #
20
18
  # KNOWN ISSUES
21
19
  # - #[]= doesn't allow less used approaches such as `arr[1, 2] = 'fizz'`
@@ -26,28 +24,17 @@ module Google
26
24
  module Protobuf
27
25
  class FFI
28
26
  # Array
29
- attach_function :append_array, :upb_Array_Append, [:Array, MessageValue.by_value, Internal::Arena], :bool
30
- attach_function :get_msgval_at,:upb_Array_Get, [:Array, :size_t], MessageValue.by_value
31
- attach_function :create_array, :upb_Array_New, [Internal::Arena, CType], :Array
32
- attach_function :array_resize, :upb_Array_Resize, [:Array, :size_t, Internal::Arena], :bool
33
- attach_function :array_set, :upb_Array_Set, [:Array, :size_t, MessageValue.by_value], :void
34
- attach_function :array_size, :upb_Array_Size, [:Array], :size_t
27
+ attach_function :append_array, :upb_Array_Append, [:Array, MessageValue.by_value, Internal::Arena], :bool
28
+ attach_function :get_msgval_at, :upb_Array_Get, [:Array, :size_t], MessageValue.by_value
29
+ attach_function :create_array, :upb_Array_New, [Internal::Arena, CType], :Array
30
+ attach_function :array_resize, :upb_Array_Resize, [:Array, :size_t, Internal::Arena], :bool
31
+ attach_function :array_set, :upb_Array_Set, [:Array, :size_t, MessageValue.by_value], :void
32
+ attach_function :array_size, :upb_Array_Size, [:Array], :size_t
33
+ attach_function :array_freeze, :upb_Array_Freeze, [:Array, MiniTable.by_ref], :void
34
+ attach_function :array_frozen?, :upb_Array_IsFrozen, [:Array], :bool
35
35
  end
36
36
 
37
37
  class RepeatedField
38
- extend Forwardable
39
- # NOTE: using delegators rather than method_missing to make the
40
- # relationship explicit instead of implicit
41
- def_delegators :to_ary,
42
- :&, :*, :-, :'<=>',
43
- :assoc, :bsearch, :bsearch_index, :combination, :compact, :count,
44
- :cycle, :dig, :drop, :drop_while, :eql?, :fetch, :find_index, :flatten,
45
- :include?, :index, :inspect, :join,
46
- :pack, :permutation, :product, :pretty_print, :pretty_print_cycle,
47
- :rassoc, :repeated_combination, :repeated_permutation, :reverse,
48
- :rindex, :rotate, :sample, :shuffle, :shelljoin,
49
- :to_s, :transpose, :uniq, :|
50
-
51
38
  include Enumerable
52
39
 
53
40
  ##
@@ -189,6 +176,38 @@ module Google
189
176
  end
190
177
  alias size :length
191
178
 
179
+ ##
180
+ # Is this object frozen?
181
+ # Returns true if either this Ruby wrapper or the underlying
182
+ # representation are frozen. Freezes the wrapper if the underlying
183
+ # representation is already frozen but this wrapper isn't.
184
+ def frozen?
185
+ unless Google::Protobuf::FFI.array_frozen? array
186
+ raise RuntimeError.new "Ruby frozen RepeatedField with mutable representation" if super
187
+ return false
188
+ end
189
+ method(:freeze).super_method.call unless super
190
+ true
191
+ end
192
+
193
+ ##
194
+ # Freezes the RepeatedField object. We have to intercept this so we can
195
+ # freeze the underlying representation, not just the Ruby wrapper. Returns
196
+ # self.
197
+ def freeze
198
+ if method(:frozen?).super_method.call
199
+ unless Google::Protobuf::FFI.array_frozen? array
200
+ raise RuntimeError.new "Underlying representation of repeated field still mutable despite frozen wrapper"
201
+ end
202
+ return self
203
+ end
204
+ unless Google::Protobuf::FFI.array_frozen? array
205
+ mini_table = (type == :message) ? Google::Protobuf::FFI.get_mini_table(@descriptor) : nil
206
+ Google::Protobuf::FFI.array_freeze(array, mini_table)
207
+ end
208
+ super
209
+ end
210
+
192
211
  def dup
193
212
  instance = self.class.allocate
194
213
  instance.send(:initialize, type, descriptor: descriptor, arena: arena)
@@ -262,123 +281,6 @@ module Google
262
281
  push(*other.to_a)
263
282
  end
264
283
 
265
- def first(n=nil)
266
- if n.nil?
267
- return self[0]
268
- elsif n < 0
269
- raise ArgumentError, "negative array size"
270
- else
271
- return self[0...n]
272
- end
273
- end
274
-
275
-
276
- def last(n=nil)
277
- if n.nil?
278
- return self[-1]
279
- elsif n < 0
280
- raise ArgumentError, "negative array size"
281
- else
282
- start = [self.size-n, 0].max
283
- return self[start...self.size]
284
- end
285
- end
286
-
287
-
288
- def pop(n=nil)
289
- if n
290
- results = []
291
- n.times{ results << pop_one }
292
- return results
293
- else
294
- return pop_one
295
- end
296
- end
297
-
298
-
299
- def empty?
300
- self.size == 0
301
- end
302
-
303
- # array aliases into enumerable
304
- alias_method :each_index, :each_with_index
305
- alias_method :slice, :[]
306
- alias_method :values_at, :select
307
- alias_method :map, :collect
308
-
309
-
310
- class << self
311
- def define_array_wrapper_method(method_name)
312
- define_method(method_name) do |*args, &block|
313
- arr = self.to_a
314
- result = arr.send(method_name, *args)
315
- self.replace(arr)
316
- return result if result
317
- return block ? block.call : result
318
- end
319
- end
320
- private :define_array_wrapper_method
321
-
322
-
323
- def define_array_wrapper_with_result_method(method_name)
324
- define_method(method_name) do |*args, &block|
325
- # result can be an Enumerator, Array, or nil
326
- # Enumerator can sometimes be returned if a block is an optional argument and it is not passed in
327
- # nil usually specifies that no change was made
328
- result = self.to_a.send(method_name, *args, &block)
329
- if result
330
- new_arr = result.to_a
331
- self.replace(new_arr)
332
- if result.is_a?(Enumerator)
333
- # generate a fresh enum; rewinding the exiting one, in Ruby 2.2, will
334
- # reset the enum with the same length, but all the #next calls will
335
- # return nil
336
- result = new_arr.to_enum
337
- # generate a wrapper enum so any changes which occur by a chained
338
- # enum can be captured
339
- ie = ProxyingEnumerator.new(self, result)
340
- result = ie.to_enum
341
- end
342
- end
343
- result
344
- end
345
- end
346
- private :define_array_wrapper_with_result_method
347
- end
348
-
349
-
350
- %w(delete delete_at shift slice! unshift).each do |method_name|
351
- define_array_wrapper_method(method_name)
352
- end
353
-
354
-
355
- %w(collect! compact! delete_if fill flatten! insert reverse!
356
- rotate! select! shuffle! sort! sort_by! uniq!).each do |method_name|
357
- define_array_wrapper_with_result_method(method_name)
358
- end
359
- alias_method :keep_if, :select!
360
- alias_method :map!, :collect!
361
- alias_method :reject!, :delete_if
362
-
363
-
364
- # propagates changes made by user of enumerator back to the original repeated field.
365
- # This only applies in cases where the calling function which created the enumerator,
366
- # such as #sort!, modifies itself rather than a new array, such as #sort
367
- class ProxyingEnumerator < Struct.new(:repeated_field, :external_enumerator)
368
- def each(*args, &block)
369
- results = []
370
- external_enumerator.each_with_index do |val, i|
371
- result = yield(val)
372
- results << result
373
- #nil means no change occurred from yield; usually occurs when #to_a is called
374
- if result
375
- repeated_field[i] = result if result != val
376
- end
377
- end
378
- results
379
- end
380
- end
381
-
382
284
  private
383
285
  include Google::Protobuf::Internal::Convert
384
286
 
@@ -468,10 +370,14 @@ module Google
468
370
  OBJECT_CACHE.try_add(@array.address, self)
469
371
  end
470
372
 
471
- # @param field [FieldDescriptor] Descriptor of the field where the RepeatedField will be assigned
472
- # @param values [Enumerable] Initial values; may be nil or empty
373
+ ##
374
+ # Constructor that uses the type information from the given
375
+ # FieldDescriptor to configure the new RepeatedField instance.
376
+ # @param field [FieldDescriptor] Type information for the new RepeatedField
473
377
  # @param arena [Arena] Owning message's arena
474
- def self.construct_for_field(field, arena, values: nil, array: nil)
378
+ # @param values [Enumerable] Initial values
379
+ # @param array [::FFI::Pointer] Existing upb_Array
380
+ def self.construct_for_field(field, arena: nil, values: nil, array: nil)
475
381
  instance = allocate
476
382
  options = {initial_values: values, name: field.name, arena: arena, array: array}
477
383
  if [:enum, :message].include? field.type
@@ -501,3 +407,5 @@ module Google
501
407
  end
502
408
  end
503
409
  end
410
+
411
+ require 'google/protobuf/repeated_field'
@@ -0,0 +1,107 @@
1
+ # Protocol Buffers - Google's data interchange format
2
+ # Copyright 2024 Google Inc. All rights reserved.
3
+ #
4
+ # Use of this source code is governed by a BSD-style
5
+ # license that can be found in the LICENSE file or at
6
+ # https://developers.google.com/open-source/licenses/bsd
7
+
8
+ module Google
9
+ module Protobuf
10
+ class ServiceDescriptor
11
+ attr :service_def, :descriptor_pool
12
+ include Enumerable
13
+
14
+ include Google::Protobuf::Internal::Convert
15
+
16
+ # FFI Interface methods and setup
17
+ extend ::FFI::DataConverter
18
+ native_type ::FFI::Type::POINTER
19
+
20
+ class << self
21
+ prepend Google::Protobuf::Internal::TypeSafety
22
+ include Google::Protobuf::Internal::PointerHelper
23
+
24
+ # @param value [ServiceDescriptor] ServiceDescriptor to convert to an FFI native type
25
+ # @param _ [Object] Unused
26
+ def to_native(value, _)
27
+ service_def_ptr = value.nil? ? nil : value.instance_variable_get(:@service_def)
28
+ return ::FFI::Pointer::NULL if service_def_ptr.nil?
29
+ raise "Underlying service_def was null!" if service_def_ptr.null?
30
+ service_def_ptr
31
+ end
32
+
33
+ ##
34
+ # @param service_def [::FFI::Pointer] ServiceDef pointer to be wrapped
35
+ # @param _ [Object] Unused
36
+ def from_native(service_def, _ = nil)
37
+ return nil if service_def.nil? or service_def.null?
38
+ file_def = Google::Protobuf::FFI.file_def_by_raw_service_def(service_def)
39
+ descriptor_from_file_def(file_def, service_def)
40
+ end
41
+ end
42
+
43
+ def self.new(*arguments, &block)
44
+ raise "Descriptor objects may not be created from Ruby."
45
+ end
46
+
47
+ def to_s
48
+ inspect
49
+ end
50
+
51
+ def inspect
52
+ "#{self.class.name}: #{name}"
53
+ end
54
+
55
+ def name
56
+ @name ||= Google::Protobuf::FFI.get_service_full_name(self)
57
+ end
58
+
59
+ def file_descriptor
60
+ @descriptor_pool.send(:get_file_descriptor, Google::Protobuf::FFI.file_def_by_raw_service_def(@service_def))
61
+ end
62
+
63
+ def each &block
64
+ n = Google::Protobuf::FFI.method_count(self)
65
+ 0.upto(n-1) do |i|
66
+ yield(Google::Protobuf::FFI.get_method_by_index(self, i))
67
+ end
68
+ nil
69
+ end
70
+
71
+ def options
72
+ @options ||= begin
73
+ size_ptr = ::FFI::MemoryPointer.new(:size_t, 1)
74
+ temporary_arena = Google::Protobuf::FFI.create_arena
75
+ buffer = Google::Protobuf::FFI.service_options(self, size_ptr, temporary_arena)
76
+ Google::Protobuf::ServiceOptions.decode(buffer.read_string_length(size_ptr.read(:size_t)).force_encoding("ASCII-8BIT").freeze).freeze
77
+ end
78
+ end
79
+
80
+ private
81
+
82
+ def initialize(service_def, descriptor_pool)
83
+ @service_def = service_def
84
+ @descriptor_pool = descriptor_pool
85
+ end
86
+
87
+ def self.private_constructor(service_def, descriptor_pool)
88
+ instance = allocate
89
+ instance.send(:initialize, service_def, descriptor_pool)
90
+ instance
91
+ end
92
+
93
+ def c_type
94
+ @c_type ||= Google::Protobuf::FFI.get_c_type(self)
95
+ end
96
+ end
97
+
98
+ class FFI
99
+ # ServiceDef
100
+ attach_function :file_def_by_raw_service_def, :upb_ServiceDef_File, [:pointer], :FileDef
101
+ attach_function :get_service_full_name, :upb_ServiceDef_FullName, [ServiceDescriptor], :string
102
+ attach_function :method_count, :upb_ServiceDef_MethodCount, [ServiceDescriptor], :int
103
+ attach_function :get_method_by_index, :upb_ServiceDef_Method, [ServiceDescriptor, :int], MethodDescriptor
104
+ attach_function :service_options, :ServiceDescriptor_serialized_options, [ServiceDescriptor, :pointer, Internal::Arena], :pointer
105
+ end
106
+ end
107
+ end
@@ -8,28 +8,7 @@ require 'google/protobuf'
8
8
  descriptor_data = "\n google/protobuf/field_mask.proto\x12\x0fgoogle.protobuf\"\x1a\n\tFieldMask\x12\r\n\x05paths\x18\x01 \x03(\tB\x85\x01\n\x13\x63om.google.protobufB\x0e\x46ieldMaskProtoP\x01Z2google.golang.org/protobuf/types/known/fieldmaskpb\xf8\x01\x01\xa2\x02\x03GPB\xaa\x02\x1eGoogle.Protobuf.WellKnownTypesb\x06proto3"
9
9
 
10
10
  pool = Google::Protobuf::DescriptorPool.generated_pool
11
-
12
- begin
13
- pool.add_serialized_file(descriptor_data)
14
- rescue TypeError
15
- # Compatibility code: will be removed in the next major version.
16
- require 'google/protobuf/descriptor_pb'
17
- parsed = Google::Protobuf::FileDescriptorProto.decode(descriptor_data)
18
- parsed.clear_dependency
19
- serialized = parsed.class.encode(parsed)
20
- file = pool.add_serialized_file(serialized)
21
- warn "Warning: Protobuf detected an import path issue while loading generated file #{__FILE__}"
22
- imports = [
23
- ]
24
- imports.each do |type_name, expected_filename|
25
- import_file = pool.lookup(type_name).file_descriptor
26
- if import_file.name != expected_filename
27
- warn "- #{file.name} imports #{expected_filename}, but that import was loaded as #{import_file.name}"
28
- end
29
- end
30
- warn "Each proto file must use a consistent fully-qualified name."
31
- warn "This will become an error in the next major version."
32
- end
11
+ pool.add_serialized_file(descriptor_data)
33
12
 
34
13
  module Google
35
14
  module Protobuf
@@ -0,0 +1,99 @@
1
+ # Protocol Buffers - Google's data interchange format
2
+ # Copyright 2023 Google Inc. All rights reserved.
3
+ #
4
+ # Use of this source code is governed by a BSD-style
5
+ # license that can be found in the LICENSE file or at
6
+ # https://developers.google.com/open-source/licenses/bsd
7
+
8
+ module Google
9
+ module Protobuf
10
+ module Internal
11
+ # A pointer -> Ruby Object cache that keeps references to Ruby wrapper
12
+ # objects. This allows us to look up any Ruby wrapper object by the address
13
+ # of the object it is wrapping. That way we can avoid ever creating two
14
+ # different wrapper objects for the same C object, which saves memory and
15
+ # preserves object identity.
16
+ #
17
+ # We use WeakMap for the cache. If sizeof(long) > sizeof(VALUE), we also
18
+ # need a secondary Hash to store WeakMap keys, because our pointer keys may
19
+ # need to be stored as Bignum instead of Fixnum. Since WeakMap is weak for
20
+ # both keys and values, a Bignum key will cause the WeakMap entry to be
21
+ # collected immediately unless there is another reference to the Bignum.
22
+ # This happens on 64-bit Windows, on which pointers are 64 bits but longs
23
+ # are 32 bits. In this case, we enable the secondary Hash to hold the keys
24
+ # and prevent them from being collected.
25
+ class ObjectCache
26
+ def initialize
27
+ @map = ObjectSpace::WeakMap.new
28
+ @mutex = Mutex.new
29
+ end
30
+
31
+ def get(key)
32
+ @map[key]
33
+ end
34
+
35
+ def try_add(key, value)
36
+ @map[key] || @mutex.synchronize do
37
+ @map[key] ||= value
38
+ end
39
+ end
40
+ end
41
+
42
+ class LegacyObjectCache
43
+ def initialize
44
+ @secondary_map = {}
45
+ @map = ObjectSpace::WeakMap.new
46
+ @mutex = Mutex.new
47
+ end
48
+
49
+ def get(key)
50
+ value = if secondary_key = @secondary_map[key]
51
+ @map[secondary_key]
52
+ else
53
+ @mutex.synchronize do
54
+ @map[(@secondary_map[key] ||= Object.new)]
55
+ end
56
+ end
57
+
58
+ # GC if we could remove at least 2000 entries or 20% of the table size
59
+ # (whichever is greater). Since the cost of the GC pass is O(N), we
60
+ # want to make sure that we condition this on overall table size, to
61
+ # avoid O(N^2) CPU costs.
62
+ cutoff = (@secondary_map.size * 0.2).ceil
63
+ cutoff = 2_000 if cutoff < 2_000
64
+ if (@secondary_map.size - @map.size) > cutoff
65
+ purge
66
+ end
67
+
68
+ value
69
+ end
70
+
71
+ def try_add(key, value)
72
+ if secondary_key = @secondary_map[key]
73
+ if old_value = @map[secondary_key]
74
+ return old_value
75
+ end
76
+ end
77
+
78
+ @mutex.synchronize do
79
+ secondary_key ||= (@secondary_map[key] ||= Object.new)
80
+ @map[secondary_key] ||= value
81
+ end
82
+ end
83
+
84
+ private
85
+
86
+ def purge
87
+ @mutex.synchronize do
88
+ @secondary_map.each do |key, secondary_key|
89
+ unless @map.key?(secondary_key)
90
+ @secondary_map.delete(key)
91
+ end
92
+ end
93
+ end
94
+ nil
95
+ end
96
+ end
97
+ end
98
+ end
99
+ end
@@ -7,32 +7,10 @@ require 'google/protobuf'
7
7
  require 'google/protobuf/descriptor_pb'
8
8
 
9
9
 
10
- descriptor_data = "\n%google/protobuf/compiler/plugin.proto\x12\x18google.protobuf.compiler\x1a google/protobuf/descriptor.proto\"F\n\x07Version\x12\r\n\x05major\x18\x01 \x01(\x05\x12\r\n\x05minor\x18\x02 \x01(\x05\x12\r\n\x05patch\x18\x03 \x01(\x05\x12\x0e\n\x06suffix\x18\x04 \x01(\t\"\x81\x02\n\x14\x43odeGeneratorRequest\x12\x18\n\x10\x66ile_to_generate\x18\x01 \x03(\t\x12\x11\n\tparameter\x18\x02 \x01(\t\x12\x38\n\nproto_file\x18\x0f \x03(\x0b\x32$.google.protobuf.FileDescriptorProto\x12\x45\n\x17source_file_descriptors\x18\x11 \x03(\x0b\x32$.google.protobuf.FileDescriptorProto\x12;\n\x10\x63ompiler_version\x18\x03 \x01(\x0b\x32!.google.protobuf.compiler.Version\"\xe0\x02\n\x15\x43odeGeneratorResponse\x12\r\n\x05\x65rror\x18\x01 \x01(\t\x12\x1a\n\x12supported_features\x18\x02 \x01(\x04\x12\x42\n\x04\x66ile\x18\x0f \x03(\x0b\x32\x34.google.protobuf.compiler.CodeGeneratorResponse.File\x1a\x7f\n\x04\x46ile\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x17\n\x0finsertion_point\x18\x02 \x01(\t\x12\x0f\n\x07\x63ontent\x18\x0f \x01(\t\x12?\n\x13generated_code_info\x18\x10 \x01(\x0b\x32\".google.protobuf.GeneratedCodeInfo\"W\n\x07\x46\x65\x61ture\x12\x10\n\x0c\x46\x45\x41TURE_NONE\x10\x00\x12\x1b\n\x17\x46\x45\x41TURE_PROTO3_OPTIONAL\x10\x01\x12\x1d\n\x19\x46\x45\x41TURE_SUPPORTS_EDITIONS\x10\x02\x42r\n\x1c\x63om.google.protobuf.compilerB\x0cPluginProtosZ)google.golang.org/protobuf/types/pluginpb\xaa\x02\x18Google.Protobuf.Compiler"
10
+ descriptor_data = "\n%google/protobuf/compiler/plugin.proto\x12\x18google.protobuf.compiler\x1a google/protobuf/descriptor.proto\"F\n\x07Version\x12\r\n\x05major\x18\x01 \x01(\x05\x12\r\n\x05minor\x18\x02 \x01(\x05\x12\r\n\x05patch\x18\x03 \x01(\x05\x12\x0e\n\x06suffix\x18\x04 \x01(\t\"\x81\x02\n\x14\x43odeGeneratorRequest\x12\x18\n\x10\x66ile_to_generate\x18\x01 \x03(\t\x12\x11\n\tparameter\x18\x02 \x01(\t\x12\x38\n\nproto_file\x18\x0f \x03(\x0b\x32$.google.protobuf.FileDescriptorProto\x12\x45\n\x17source_file_descriptors\x18\x11 \x03(\x0b\x32$.google.protobuf.FileDescriptorProto\x12;\n\x10\x63ompiler_version\x18\x03 \x01(\x0b\x32!.google.protobuf.compiler.Version\"\x92\x03\n\x15\x43odeGeneratorResponse\x12\r\n\x05\x65rror\x18\x01 \x01(\t\x12\x1a\n\x12supported_features\x18\x02 \x01(\x04\x12\x17\n\x0fminimum_edition\x18\x03 \x01(\x05\x12\x17\n\x0fmaximum_edition\x18\x04 \x01(\x05\x12\x42\n\x04\x66ile\x18\x0f \x03(\x0b\x32\x34.google.protobuf.compiler.CodeGeneratorResponse.File\x1a\x7f\n\x04\x46ile\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\x17\n\x0finsertion_point\x18\x02 \x01(\t\x12\x0f\n\x07\x63ontent\x18\x0f \x01(\t\x12?\n\x13generated_code_info\x18\x10 \x01(\x0b\x32\".google.protobuf.GeneratedCodeInfo\"W\n\x07\x46\x65\x61ture\x12\x10\n\x0c\x46\x45\x41TURE_NONE\x10\x00\x12\x1b\n\x17\x46\x45\x41TURE_PROTO3_OPTIONAL\x10\x01\x12\x1d\n\x19\x46\x45\x41TURE_SUPPORTS_EDITIONS\x10\x02\x42r\n\x1c\x63om.google.protobuf.compilerB\x0cPluginProtosZ)google.golang.org/protobuf/types/pluginpb\xaa\x02\x18Google.Protobuf.Compiler"
11
11
 
12
12
  pool = Google::Protobuf::DescriptorPool.generated_pool
13
-
14
- begin
15
- pool.add_serialized_file(descriptor_data)
16
- rescue TypeError
17
- # Compatibility code: will be removed in the next major version.
18
- require 'google/protobuf/descriptor_pb'
19
- parsed = Google::Protobuf::FileDescriptorProto.decode(descriptor_data)
20
- parsed.clear_dependency
21
- serialized = parsed.class.encode(parsed)
22
- file = pool.add_serialized_file(serialized)
23
- warn "Warning: Protobuf detected an import path issue while loading generated file #{__FILE__}"
24
- imports = [
25
- ["google.protobuf.FileDescriptorProto", "google/protobuf/descriptor.proto"],
26
- ]
27
- imports.each do |type_name, expected_filename|
28
- import_file = pool.lookup(type_name).file_descriptor
29
- if import_file.name != expected_filename
30
- warn "- #{file.name} imports #{expected_filename}, but that import was loaded as #{import_file.name}"
31
- end
32
- end
33
- warn "Each proto file must use a consistent fully-qualified name."
34
- warn "This will become an error in the next major version."
35
- end
13
+ pool.add_serialized_file(descriptor_data)
36
14
 
37
15
  module Google
38
16
  module Protobuf
@@ -47,12 +47,12 @@ module Google
47
47
  def_delegators :to_ary,
48
48
  :&, :*, :-, :'<=>',
49
49
  :assoc, :bsearch, :bsearch_index, :combination, :compact, :count,
50
- :cycle, :dig, :drop, :drop_while, :eql?, :fetch, :find_index, :flatten,
51
- :include?, :index, :inspect, :join,
50
+ :cycle, :difference, :dig, :drop, :drop_while, :eql?, :fetch, :find_index, :flatten,
51
+ :include?, :index, :inspect, :intersection, :join,
52
52
  :pack, :permutation, :product, :pretty_print, :pretty_print_cycle,
53
53
  :rassoc, :repeated_combination, :repeated_permutation, :reverse,
54
54
  :rindex, :rotate, :sample, :shuffle, :shelljoin,
55
- :to_s, :transpose, :uniq, :|
55
+ :to_s, :transpose, :union, :uniq, :|
56
56
 
57
57
 
58
58
  def first(n=nil)
@@ -94,7 +94,6 @@ module Google
94
94
  end
95
95
 
96
96
  # array aliases into enumerable
97
- alias_method :each_index, :each_with_index
98
97
  alias_method :slice, :[]
99
98
  alias_method :values_at, :select
100
99
  alias_method :map, :collect
@@ -145,7 +144,7 @@ module Google
145
144
  end
146
145
 
147
146
 
148
- %w(collect! compact! delete_if fill flatten! insert reverse!
147
+ %w(collect! compact! delete_if each_index fill flatten! insert reverse!
149
148
  rotate! select! shuffle! sort! sort_by! uniq!).each do |method_name|
150
149
  define_array_wrapper_with_result_method(method_name)
151
150
  end
@@ -8,28 +8,7 @@ require 'google/protobuf'
8
8
  descriptor_data = "\n$google/protobuf/source_context.proto\x12\x0fgoogle.protobuf\"\"\n\rSourceContext\x12\x11\n\tfile_name\x18\x01 \x01(\tB\x8a\x01\n\x13\x63om.google.protobufB\x12SourceContextProtoP\x01Z6google.golang.org/protobuf/types/known/sourcecontextpb\xa2\x02\x03GPB\xaa\x02\x1eGoogle.Protobuf.WellKnownTypesb\x06proto3"
9
9
 
10
10
  pool = Google::Protobuf::DescriptorPool.generated_pool
11
-
12
- begin
13
- pool.add_serialized_file(descriptor_data)
14
- rescue TypeError
15
- # Compatibility code: will be removed in the next major version.
16
- require 'google/protobuf/descriptor_pb'
17
- parsed = Google::Protobuf::FileDescriptorProto.decode(descriptor_data)
18
- parsed.clear_dependency
19
- serialized = parsed.class.encode(parsed)
20
- file = pool.add_serialized_file(serialized)
21
- warn "Warning: Protobuf detected an import path issue while loading generated file #{__FILE__}"
22
- imports = [
23
- ]
24
- imports.each do |type_name, expected_filename|
25
- import_file = pool.lookup(type_name).file_descriptor
26
- if import_file.name != expected_filename
27
- warn "- #{file.name} imports #{expected_filename}, but that import was loaded as #{import_file.name}"
28
- end
29
- end
30
- warn "Each proto file must use a consistent fully-qualified name."
31
- warn "This will become an error in the next major version."
32
- end
11
+ pool.add_serialized_file(descriptor_data)
33
12
 
34
13
  module Google
35
14
  module Protobuf
@@ -8,28 +8,7 @@ require 'google/protobuf'
8
8
  descriptor_data = "\n\x1cgoogle/protobuf/struct.proto\x12\x0fgoogle.protobuf\"\x84\x01\n\x06Struct\x12\x33\n\x06\x66ields\x18\x01 \x03(\x0b\x32#.google.protobuf.Struct.FieldsEntry\x1a\x45\n\x0b\x46ieldsEntry\x12\x0b\n\x03key\x18\x01 \x01(\t\x12%\n\x05value\x18\x02 \x01(\x0b\x32\x16.google.protobuf.Value:\x02\x38\x01\"\xea\x01\n\x05Value\x12\x30\n\nnull_value\x18\x01 \x01(\x0e\x32\x1a.google.protobuf.NullValueH\x00\x12\x16\n\x0cnumber_value\x18\x02 \x01(\x01H\x00\x12\x16\n\x0cstring_value\x18\x03 \x01(\tH\x00\x12\x14\n\nbool_value\x18\x04 \x01(\x08H\x00\x12/\n\x0cstruct_value\x18\x05 \x01(\x0b\x32\x17.google.protobuf.StructH\x00\x12\x30\n\nlist_value\x18\x06 \x01(\x0b\x32\x1a.google.protobuf.ListValueH\x00\x42\x06\n\x04kind\"3\n\tListValue\x12&\n\x06values\x18\x01 \x03(\x0b\x32\x16.google.protobuf.Value*\x1b\n\tNullValue\x12\x0e\n\nNULL_VALUE\x10\x00\x42\x7f\n\x13\x63om.google.protobufB\x0bStructProtoP\x01Z/google.golang.org/protobuf/types/known/structpb\xf8\x01\x01\xa2\x02\x03GPB\xaa\x02\x1eGoogle.Protobuf.WellKnownTypesb\x06proto3"
9
9
 
10
10
  pool = Google::Protobuf::DescriptorPool.generated_pool
11
-
12
- begin
13
- pool.add_serialized_file(descriptor_data)
14
- rescue TypeError
15
- # Compatibility code: will be removed in the next major version.
16
- require 'google/protobuf/descriptor_pb'
17
- parsed = Google::Protobuf::FileDescriptorProto.decode(descriptor_data)
18
- parsed.clear_dependency
19
- serialized = parsed.class.encode(parsed)
20
- file = pool.add_serialized_file(serialized)
21
- warn "Warning: Protobuf detected an import path issue while loading generated file #{__FILE__}"
22
- imports = [
23
- ]
24
- imports.each do |type_name, expected_filename|
25
- import_file = pool.lookup(type_name).file_descriptor
26
- if import_file.name != expected_filename
27
- warn "- #{file.name} imports #{expected_filename}, but that import was loaded as #{import_file.name}"
28
- end
29
- end
30
- warn "Each proto file must use a consistent fully-qualified name."
31
- warn "This will become an error in the next major version."
32
- end
11
+ pool.add_serialized_file(descriptor_data)
33
12
 
34
13
  module Google
35
14
  module Protobuf