New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FEATURE: support custom codecs for transport #250
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe49a3d
FEATURE: support custom codecs for transport
SamSaffron 49c7200
Corrected implementation
SamSaffron b4b4e8d
Improve implementation
SamSaffron 06d1778
Update bench/codecs/packed_string.rb
SamSaffron 24c4a69
Lint
benlangfeld 501d52e
More accurate list of unreleased features
benlangfeld File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'bundler/inline' | ||
|
||
gemfile do | ||
source 'https://rubygems.org' | ||
gem 'message_bus', path: '../' | ||
gem 'benchmark-ips' | ||
gem 'oj' | ||
end | ||
|
||
require 'benchmark/ips' | ||
require 'message_bus' | ||
|
||
class StringHack | ||
class FastIdList | ||
def self.from_array(array) | ||
new(",#{array.join(",")},") | ||
end | ||
|
||
def self.from_string(string) | ||
new(string) | ||
end | ||
|
||
def initialize(packed) | ||
@packed = packed | ||
end | ||
|
||
def include?(id) | ||
@packed.include?(",#{id},") | ||
end | ||
|
||
def to_s | ||
@packed | ||
end | ||
end | ||
|
||
class OjString | ||
def encode(data:, user_ids:, group_ids:, client_ids:) | ||
|
||
if user_ids | ||
user_ids = FastIdList.from_array(user_ids).to_s | ||
end | ||
|
||
::Oj.dump({ | ||
data: data, | ||
user_ids: user_ids, | ||
group_ids: group_ids, | ||
client_ids: client_ids | ||
}, | ||
mode: :compat) | ||
end | ||
|
||
def decode(payload) | ||
result = ::Oj.load(payload, mode: :compat) | ||
|
||
if str = result["user_ids"] | ||
result["user_ids"] = FastIdList.from_string(str) | ||
end | ||
|
||
result | ||
end | ||
end | ||
end | ||
|
||
json_codec = MessageBus::Codec::Json.new | ||
oj_codec = MessageBus::Codec::Oj.new | ||
oj_fast_codec = MessageBus::Codec::OjFast.new | ||
oj_fast_string_hack = StringHack::OjString.new | ||
|
||
json = json_codec.encode( | ||
data: "hello world", | ||
user_ids: (1..10000).to_a, | ||
group_ids: nil, | ||
client_ids: nil | ||
) | ||
|
||
json_fast = oj_fast_codec.encode( | ||
data: "hello world", | ||
user_ids: (1..10000).to_a, | ||
group_ids: nil, | ||
client_ids: nil | ||
) | ||
|
||
json_fast2 = oj_fast_string_hack.encode( | ||
data: "hello world", | ||
user_ids: (1..10000).to_a, | ||
group_ids: nil, | ||
client_ids: nil | ||
) | ||
|
||
Benchmark.ips do |x| | ||
x.report("json") do |n| | ||
while n > 0 | ||
decoded = json_codec.decode(json) | ||
decoded["user_ids"].include?(5000) | ||
n -= 1 | ||
end | ||
end | ||
|
||
x.report("oj") do |n| | ||
while n > 0 | ||
decoded = oj_codec.decode(json) | ||
decoded["user_ids"].include?(5000) | ||
n -= 1 | ||
end | ||
end | ||
|
||
x.report("oj_fast") do |n| | ||
while n > 0 | ||
decoded = oj_fast_codec.decode(json_fast) | ||
decoded["user_ids"].include?(5000) | ||
n -= 1 | ||
end | ||
end | ||
|
||
x.report("oj_fast string hack") do |n| | ||
while n > 0 | ||
decoded = oj_fast_string_hack.decode(json_fast2) | ||
decoded["user_ids"].include?(5000) | ||
n -= 1 | ||
end | ||
end | ||
x.compare! | ||
end | ||
|
||
#Comparison: | ||
# oj_fast: 129350.0 i/s | ||
# oj_fast string hack: 26255.2 i/s - 4.93x (± 0.00) slower | ||
# oj: 3073.5 i/s - 42.09x (± 0.00) slower | ||
# json: 2221.9 i/s - 58.22x (± 0.00) slower |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
module MessageBus | ||
module Codec | ||
class Base | ||
def encode(data:, user_ids:, group_ids:, client_ids:) | ||
raise ConcreteClassMustImplementError | ||
end | ||
|
||
def decode(payload) | ||
raise ConcreteClassMustImplementError | ||
end | ||
end | ||
|
||
autoload :Json, File.expand_path("json", __dir__) | ||
autoload :Oj, File.expand_path("oj", __dir__) | ||
autoload :OjFast, File.expand_path("oj_fast", __dir__) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# frozen_string_literal: true | ||
|
||
module MessageBus | ||
module Codec | ||
class Json < Base | ||
def encode(data:, user_ids:, group_ids:, client_ids:) | ||
JSON.dump( | ||
data: data, | ||
user_ids: user_ids, | ||
group_ids: group_ids, | ||
client_ids: client_ids | ||
) | ||
end | ||
|
||
def decode(payload) | ||
JSON.parse(payload) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'oj' unless defined? ::Oj | ||
|
||
module MessageBus | ||
module Codec | ||
class Oj < Base | ||
def encode(data:, user_ids:, group_ids:, client_ids:) | ||
::Oj.dump({ | ||
data: data, | ||
user_ids: user_ids, | ||
group_ids: group_ids, | ||
client_ids: client_ids | ||
}, | ||
mode: :compat) | ||
end | ||
|
||
def decode(payload) | ||
::Oj.load(payload, mode: :compat) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'oj' unless defined? ::Oj | ||
require 'base64' | ||
|
||
module MessageBus | ||
module Codec | ||
class FastIdList | ||
def self.from_array(array) | ||
new(array.sort.pack("V*")) | ||
end | ||
|
||
def self.from_string(string) | ||
new(string) | ||
end | ||
|
||
def initialize(packed) | ||
@packed = packed | ||
end | ||
|
||
def include?(id) | ||
found = (0...length).bsearch do |index| | ||
@packed.byteslice(index * 4, 4).unpack1("V") >= id | ||
end | ||
|
||
if found | ||
found && @packed.byteslice(found * 4, 4).unpack1("V") == id | ||
end | ||
end | ||
|
||
def length | ||
@length ||= @packed.bytesize / 4 | ||
end | ||
|
||
def to_a | ||
@packed.unpack("V*") | ||
end | ||
|
||
def to_s | ||
@packed | ||
end | ||
end | ||
|
||
class OjFast < Base | ||
def encode(data:, user_ids:, group_ids:, client_ids:) | ||
|
||
if user_ids | ||
user_ids = FastIdList.from_array(user_ids).to_s | ||
end | ||
|
||
#if group_ids | ||
# group_ids = FastIdList.from_array(group_ids).to_s | ||
#end | ||
data = ::Oj.dump(data, mode: :compat) | ||
|
||
Marshal.dump( | ||
"data" => data, | ||
"user_ids" => user_ids, | ||
"group_ids" => group_ids, | ||
"client_ids" => client_ids | ||
) | ||
end | ||
|
||
def decode(payload) | ||
result = Marshal.load(payload) | ||
result["data"] = ::Oj.load(result["data"], mode: :compat) | ||
|
||
if str = result["user_ids"] | ||
result["user_ids"] = FastIdList.from_string(str) | ||
end | ||
|
||
# groups need to implement (-) | ||
# if str = result["group_ids"] | ||
# result["group_ids"] = FastIdList.from_string(str) | ||
# end | ||
# | ||
|
||
result | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: you mentioned the problem with 4 bytes, but why is it not possible to use
"Q"
to pack 8 bytes?I made a test and it seems to work:
Sorry if that doesn't make sense :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure made Q vs V optional in the demo codec in the bench directory.