Skip to content

Spanner Performance Improvements#2741

Merged
blowmage merged 4 commits intogoogleapis:masterfrom
blowmage:spanner-data-to_h-perf
Dec 29, 2018
Merged

Spanner Performance Improvements#2741
blowmage merged 4 commits intogoogleapis:masterfrom
blowmage:spanner-data-to_h-perf

Conversation

@blowmage
Copy link
Copy Markdown
Contributor

@blowmage blowmage commented Dec 28, 2018

We have received feedback that Spanner's serialization to a Ruby Hash is not as performant as expected. While there are micro-optimizations that can be made, the largest improvement in performance can be made by optionally skipping the check for duplicate names. Spanner data can contain duplicate names, or no names, which makes representing it in a Ruby Hash difficult. Consider the following example:

hash = { foo: :bar, foo: :baz } #=> {:foo=>:baz}

The former value of :bar was dropped in favor of the latter value of :baz. To ensure that values are not dropped, Spanner data will raise DuplicateNameError when serializing to a Ruby Hash. This PR adds the optional skip_dup_check argument that will avoid the check for users that want the most performance and are willing to manage the risk of losing values.

require "google/cloud/spanner"

spanner = Google::Cloud::Spanner.new

db = spanner.client "my-instance", "my-database"

dup_type = db.fields [[:x, :INT64], [:x, :STRING], [:x, :BOOL]]
dup_data = dup_type.struct [42, nil, false]

dup_data.to_h # raises Google::Cloud::Spanner::DuplicateNameError

dup_data.to_h skip_dup_check: true #=> {:x=>false}

Here are the performance numbers for master, the performance tweaks, and the performance tweaks with skipping the duplicate name check:

                        user     system      total        real
master (GC on)      0.260000   0.000000   0.260000 (  0.269273)
perf only (GC on)   0.240000   0.000000   0.240000 (  0.245119)
perf skip (GC on)   0.140000   0.000000   0.140000 (  0.148022)
master (GC off)     0.220000   0.020000   0.240000 (  0.232874)
perf only (GC off)  0.200000   0.010000   0.210000 (  0.216652)
perf skip (GC off)  0.130000   0.000000   0.130000 (  0.141780)

This refactor seems to improve performance a bit.
Here are the benchmarks with GC disabled:

                 user     system      total        real
Using pairs  0.204243   0.008495   0.212738 (  0.213284)
Using to_a   0.196423   0.007768   0.204191 (  0.204834)
This enhancement allows users to skip the dupplicate name check
when serializing Data to a Ruby Hash or Array.
This speeds up the serialization, but data may be lost.

Here are the benchmarks with GC disabled:

                 user     system      total        real
With dup check     0.196423   0.007768   0.204191 (  0.204834)
Without dup check  0.136929   0.007002   0.143931 (  0.145008)
This refactor improves detection of duplicate names.
Here are the benchmarks with GC disabled:

                    user     system      total        real
Using uniq      0.060000   0.000000   0.060000 (  0.062388)
Using group_by  0.040000   0.000000   0.040000 (  0.044493)
@blowmage blowmage added the api: spanner Issues related to the Spanner API. label Dec 28, 2018
@blowmage blowmage self-assigned this Dec 28, 2018
@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Dec 28, 2018
[key, value]
end
end
Hash[hashified_pairs]

This comment was marked as spam.

This comment was marked as spam.

#
# @param [Boolean] skip_dup_check Skip the check for whether nested data
# contains duplicate names. When skipped, the nested hash may lose
# values. Default is `false`. Optional.

This comment was marked as spam.

This comment was marked as spam.

# hash. Do not use this method if the data has more than one member with
# the same name.
# hash. This method will raise or lose values if the data has more than
# one member with the same name.

This comment was marked as spam.

This comment was marked as spam.

#
def duplicate_names?
keys.count != keys.uniq.count
keys.group_by { |e| e }.select { |_k, v| v.size > 1 }.any?

This comment was marked as spam.

@blowmage blowmage requested a review from a team December 28, 2018 21:58
@blowmage blowmage merged commit 34619eb into googleapis:master Dec 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: spanner Issues related to the Spanner API. cla: yes This human has signed the Contributor License Agreement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants