-
Notifications
You must be signed in to change notification settings - Fork 5
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
feat: Oj Serializers 2.0 #9
Conversation
# options - list of external options to pass to the sub class (available in `item.options`) | ||
# | ||
# Returns a Hash, with the attributes specified in the serializer. | ||
def one_as_hash(item, options = nil) |
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.
@ElMassimo Can you make options
default to {}
? Our serializer specs were raising undefined method [] for nil
errors since @object.options
was nil.
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.
Sounds unusual, as options
would never return nil
, because it falls back to {}
.
If you provide a small reproduction I can take a look.
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.
Ok, did not realize there was an options
method directly in the serializer. Our serializer is calling @object.options
directly, which is where the nil
error comes from.
@ElMassimo Will this include changes to allow |
Correct, the controller will use whatever format is specified in the serializer. For example, if you use |
Also, can we add the benchmark results in the Readme? |
7351866
to
e94694c
Compare
Looks good! |
Also, might be a good idea to add Panko to the benchmarks? |
Can you add the |
6eb3d0b
to
49ba50b
Compare
Added Alba and Panko to the benchmarks. Following results from running on MacBook Pro (16-inch, 2021, M1 Pro): serializing a model
serializing a collection
serializing a large collection
I think the only reason Panko performs worse in this benchmark is that it's specialized for ActiveRecord, but the benchmark uses a normal object, and the fact that the CPU is an M1 Pro instead of an Intel chip. Otherwise I'd expect Panko to be 2x faster in simple cases (although it requires a C extension, it's less compatible with AMS, has less features, and is not as flexible when combining with other libraries as it requires using |
49ba50b
to
b366276
Compare
2faeccd
to
7b1bfd1
Compare
4416ed8
to
102d30e
Compare
750cda7
to
59bb312
Compare
4e9e254
to
8da9ef9
Compare
8da9ef9
to
8a481f7
Compare
When are you expecting to release to master? |
Nice work, congrats! |
Description 📖
This pull request introduces a rework of the internals, after experimenting with ways to reduce the indirection to obtain the values for each attribute.
It has more features than v1, while increasing performance even further 🚀
Features ✨
render_as_hash
to efficiently build a Hash from the serializerrender
shortcut, unifyingone
andmany
transform_keys :camelize
: a built-in setting to convert keys, in a way that does not affect runtime performancesort_keys_by :name
: allows to sort the response alphabetically, without affecting runtime performanceserialize
as an easier approach to define serializer attributesBreaking Changes
Since returning a
Hash
is more convenient than returning aOj::StringWriter
, and performance is comparable, I'm considering makingdefault_format :hash
the default.The previous APIs will still be available as
one_as_json
andmany_as_json
, as well as adefault_format :json
setting to make the library work exactly the same as in version 1.Performance 🚀
Additional optimizations have made JSON rendering 20% to 40% faster than the previous version, so it's now 3x to 7x faster than
active_model_serializers
orblueprinter
.These are some preliminary results from running the benchmarks running on a MacBook Pro (16-inch, 2021, M1 Pro):
serializing options
serializing a model
serializing a collection
serializing a large collection
memory when serializing an object
memory when serializing a collection