Skip to content

Commit

Permalink
Add frozen string literal to production code
Browse files Browse the repository at this point in the history
Allows to slightly reduce memory usage.

In the benchmark sample, there is a reduction on Ruby 3.2 from:

    Total allocated: 685520 bytes (4936 objects)

to:

    Total allocated: 660144 bytes (4502 objects)
  • Loading branch information
tagliala committed Sep 16, 2023
1 parent c25a157 commit a889621
Show file tree
Hide file tree
Showing 28 changed files with 60 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/active_record/connection_adapters/chronomodel_adapter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'chrono_model'

module ActiveRecord
Expand Down
2 changes: 2 additions & 0 deletions lib/active_record/tasks/chronomodel_database_tasks.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ActiveRecord
module Tasks
class ChronomodelDatabaseTasks < PostgreSQLDatabaseTasks
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'active_record'

require 'chrono_model/conversions'
Expand Down
4 changes: 3 additions & 1 deletion lib/chrono_model/adapter.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'active_record/connection_adapters/postgresql_adapter'

require 'chrono_model/adapter/migrations'
Expand Down Expand Up @@ -84,7 +86,7 @@ def column_definitions; end
define_method(:column_definitions) do |table_name|
return super(table_name) unless is_chrono?(table_name)

on_schema(TEMPORAL_SCHEMA + ',' + self.schema_search_path, recurse: :ignore) { super(table_name) }
on_schema("#{TEMPORAL_SCHEMA},#{self.schema_search_path}", recurse: :ignore) { super(table_name) }
end

# Evaluates the given block in the temporal schema.
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/adapter/ddl.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'active_support/core_ext/string/strip'
require 'multi_json'

Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/adapter/indexes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
class Adapter < ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
module Indexes
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/adapter/migrations.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
class Adapter < ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
module Migrations
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/adapter/migrations_modules/legacy.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
class Adapter
module MigrationsModules
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/adapter/migrations_modules/stable.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
class Adapter
module MigrationsModules
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/adapter/tsrange.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
class Adapter < ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
module TSRange
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/adapter/upgrade.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
class Adapter < ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
module Upgrade
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/conversions.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module Conversions
extend self
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/json.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module Json
extend self
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/patches.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'chrono_model/patches/as_of_time_holder'
require 'chrono_model/patches/as_of_time_relation'

Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/patches/as_of_time_holder.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module Patches
# Added to classes that need to carry the As-Of date around
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/patches/as_of_time_relation.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module Patches
# This class is a dummy relation whose scope is only to pass around the
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/patches/association.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module Patches
# Patches ActiveRecord::Associations::Association to add support for
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/patches/batches.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module Patches
module Batches
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/patches/join_node.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module Patches
# This class supports the AR 5.0 code that expects to receive an
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/patches/preloader.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module Patches
# Patches ActiveRecord::Associations::Preloader to add support for
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/patches/relation.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module Patches
module Relation
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/time_gate.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
# Provides the TimeMachine API to non-temporal models that associate
# temporal ones.
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/time_machine.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require 'chrono_model/time_machine/time_query'
require 'chrono_model/time_machine/timeline'
require 'chrono_model/time_machine/history_model'
Expand Down
4 changes: 3 additions & 1 deletion lib/chrono_model/time_machine/history_model.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module TimeMachine
module HistoryModel
Expand Down Expand Up @@ -126,7 +128,7 @@ def descends_from_active_record?
# current class or a descendant (or self).
#
def find_sti_class(type_name)
super(type_name + "::History")
super("#{type_name}::History")
end
end

Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/time_machine/time_query.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module TimeMachine
#
Expand Down
6 changes: 4 additions & 2 deletions lib/chrono_model/time_machine/timeline.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module TimeMachine
module Timeline
Expand Down Expand Up @@ -35,12 +37,12 @@ def timeline(record = nil, options = {})
end

relation = relation
.order('ts ' << (options[:reverse] ? 'DESC' : 'ASC'))
.order("ts #{options[:reverse] ? 'DESC' : 'ASC'}")

relation = relation.from(%["public".#{quoted_table_name}]) unless self.chrono?
relation = relation.where(id: rid) if rid

sql = "SELECT ts FROM ( #{relation.to_sql} ) AS foo WHERE ts IS NOT NULL"
sql = +"SELECT ts FROM ( #{relation.to_sql} ) AS foo WHERE ts IS NOT NULL"

if options.key?(:before)
sql << " AND ts < '#{Conversions.time_to_utc_string(options[:before])}'"
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/utilities.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
module Utilities
# Amends the given history item setting a different period.
Expand Down
2 changes: 2 additions & 0 deletions lib/chrono_model/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module ChronoModel
VERSION = "1.2.2"
end

0 comments on commit a889621

Please sign in to comment.