diff options
author | Carl Thuringer <carl@hatchloyalty.com> | 2019-02-13 19:34:04 -0600 |
---|---|---|
committer | Carl Thuringer <carl@hatchloyalty.com> | 2019-02-20 10:11:35 -0600 |
commit | 6735a0719d43b08a18bb5ea544ad2af0935138a5 (patch) | |
tree | ebb8e291a7ff17f024f85bba5c7ede26e83c617b /actionview/RUNNING_UNIT_TESTS.rdoc | |
parent | ff6b713f5e729859995f204093ad3f8e08f39ea8 (diff) | |
download | rails-6735a0719d43b08a18bb5ea544ad2af0935138a5.tar.gz rails-6735a0719d43b08a18bb5ea544ad2af0935138a5.tar.bz2 rails-6735a0719d43b08a18bb5ea544ad2af0935138a5.zip |
Reduce unused allocations when casting UUIDs for Postgres
Using the subscript method `#[]` on a string has several overloads and
rather complex implementation. One of the overloads is the capability to
accept a regular expression and then run a match, then return the
receiver (if it matched) or one of the groups from the MatchData.
The function of the `UUID#cast` method is to cast a UUID to a type and
format acceptable by postgres. Naturally UUIDs are supposed to be
string and of a certain format, but it had been determined that it was
not ideal for the framework to send just any old string to Postgres and
allow the engine to complain when "foobar" or "" was sent, being
obviously of the wrong format for a valid UUID. Therefore this code was
written to facilitate the checking, and if it were not of the correct
format, a `nil` would be returned as is conventional in Rails.
Now, the subscript method will allocate one or more strings on a match
and return one of them, based on the index parameter. However, there
is no need for a new string, as a UUID of the correct format is already
such, and so long as the format was verified then the string supplied is
adequate for consumption by the database.
The subscript method also creates a MatchData object which will never be
used, and so must eventually be garbage collected.
Garbage collection indeed. This innocuous method tends to be called
quite a lot, for example if the primary key of a table is a uuid, then
this method will be called. If the foreign key of a relation is a UUID,
once again this method is called. If that foreign key is belonging to
a has_many relationship with dozens of objects, then again dozens of
UUIDs shall be cast to a dup of themselves, and spawn dozens of
MatchData objects, and so on.
So, for users that:
* Use UUIDs as primary keys
* Use Postgres
* Operate on collections of objects
This accomplishes a significant savings in total allocations, and may
save many garbage collections.
Diffstat (limited to 'actionview/RUNNING_UNIT_TESTS.rdoc')
0 files changed, 0 insertions, 0 deletions