You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by tj...@apache.org on 2020/11/20 19:50:19 UTC

[avro] branch master updated: AVRO-2984: Optimize Ruby DatumWriter memory consumption (#1014)

This is an automated email from the ASF dual-hosted git repository.

tjwp pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/master by this push:
     new b7d5027  AVRO-2984: Optimize Ruby DatumWriter memory consumption (#1014)
b7d5027 is described below

commit b7d5027291621769653771e4fc91dba98bc08d8d
Author: Joel Turkel <Jt...@salsify.com>
AuthorDate: Fri Nov 20 14:49:59 2020 -0500

    AVRO-2984: Optimize Ruby DatumWriter memory consumption (#1014)
    
    This optimizes Ruby datum writing by avoiding an unnecessary hash
    allocation for constant validation options and avoids an unnecessary
    array allocation for success datum validation results. These
    optimizations are on the hot path for writing messages.
---
 lang/ruby/lib/avro/io.rb               |  4 +++-
 lang/ruby/lib/avro/schema_validator.rb | 17 ++++++++---------
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/lang/ruby/lib/avro/io.rb b/lang/ruby/lib/avro/io.rb
index fbf7939..11d334f 100644
--- a/lang/ruby/lib/avro/io.rb
+++ b/lang/ruby/lib/avro/io.rb
@@ -510,6 +510,8 @@ module Avro
 
     # DatumWriter for generic ruby objects
     class DatumWriter
+      VALIDATION_OPTIONS = { recursive: false, encoded: true }.freeze
+
       attr_accessor :writers_schema
       def initialize(writers_schema=nil)
         @writers_schema = writers_schema
@@ -522,7 +524,7 @@ module Avro
       def write_data(writers_schema, logical_datum, encoder)
         datum = writers_schema.type_adapter.encode(logical_datum)
 
-        unless Schema.validate(writers_schema, datum, { recursive: false, encoded: true })
+        unless Schema.validate(writers_schema, datum, VALIDATION_OPTIONS)
           raise AvroTypeError.new(writers_schema, datum)
         end
 
diff --git a/lang/ruby/lib/avro/schema_validator.rb b/lang/ruby/lib/avro/schema_validator.rb
index aaccb75..f52c239 100644
--- a/lang/ruby/lib/avro/schema_validator.rb
+++ b/lang/ruby/lib/avro/schema_validator.rb
@@ -24,14 +24,8 @@ module Avro
     BOOLEAN_VALUES = [true, false].freeze
 
     class Result
-      attr_reader :errors
-
-      def initialize
-        @errors = []
-      end
-
       def <<(error)
-        @errors << error
+        errors << error
       end
 
       def add_error(path, message)
@@ -39,11 +33,16 @@ module Avro
       end
 
       def failure?
-        @errors.any?
+        defined?(@errors) && errors.any?
       end
 
       def to_s
-        errors.join("\n")
+        failure? ? errors.join("\n") : ''
+      end
+
+      def errors
+        # Use less memory for success results by lazily creating the errors array
+        @errors ||= []
       end
     end