1. 21 2月, 2019 1 次提交
    • C
      Reduce unused allocations when casting UUIDs for Postgres · 6735a071
      Carl Thuringer 提交于
      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.
      6735a071
  2. 20 2月, 2019 2 次提交
  3. 19 2月, 2019 12 次提交
  4. 18 2月, 2019 8 次提交
  5. 17 2月, 2019 9 次提交
  6. 16 2月, 2019 8 次提交