提交 b1458218 编写于 作者: R Ryuta Kamizono

Lazy allocate `@_start_transaction_state`

上级 aa5e6b59
...@@ -403,7 +403,7 @@ def initialize_dup(other) # :nodoc: ...@@ -403,7 +403,7 @@ def initialize_dup(other) # :nodoc:
@new_record = true @new_record = true
@destroyed = false @destroyed = false
@_start_transaction_state = {} @_start_transaction_state = nil
@transaction_state = nil @transaction_state = nil
super super
...@@ -575,7 +575,7 @@ def init_internals ...@@ -575,7 +575,7 @@ def init_internals
@marked_for_destruction = false @marked_for_destruction = false
@destroyed_by_association = nil @destroyed_by_association = nil
@new_record = true @new_record = true
@_start_transaction_state = {} @_start_transaction_state = nil
@transaction_state = nil @transaction_state = nil
end end
......
...@@ -386,14 +386,15 @@ def with_transaction_returning_status ...@@ -386,14 +386,15 @@ def with_transaction_returning_status
# Save the new record state and id of a record so it can be restored later if a transaction fails. # Save the new record state and id of a record so it can be restored later if a transaction fails.
def remember_transaction_record_state def remember_transaction_record_state
@_start_transaction_state.reverse_merge!( @_start_transaction_state ||= {
id: id, id: id,
new_record: @new_record, new_record: @new_record,
destroyed: @destroyed, destroyed: @destroyed,
attributes: @attributes, attributes: @attributes,
frozen?: frozen?, frozen?: frozen?,
) level: 0
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) + 1 }
@_start_transaction_state[:level] += 1
remember_new_record_before_last_commit remember_new_record_before_last_commit
end end
...@@ -407,19 +408,20 @@ def remember_new_record_before_last_commit ...@@ -407,19 +408,20 @@ def remember_new_record_before_last_commit
# Clear the new record state and id of a record. # Clear the new record state and id of a record.
def clear_transaction_record_state def clear_transaction_record_state
@_start_transaction_state[:level] = (@_start_transaction_state[:level] || 0) - 1 return unless @_start_transaction_state
@_start_transaction_state[:level] -= 1
force_clear_transaction_record_state if @_start_transaction_state[:level] < 1 force_clear_transaction_record_state if @_start_transaction_state[:level] < 1
end end
# Force to clear the transaction record state. # Force to clear the transaction record state.
def force_clear_transaction_record_state def force_clear_transaction_record_state
@_start_transaction_state.clear @_start_transaction_state = nil
@transaction_state = nil @transaction_state = nil
end end
# Restore the new record state and id of a record that was previously saved by a call to save_record_state. # Restore the new record state and id of a record that was previously saved by a call to save_record_state.
def restore_transaction_record_state(force_restore_state = false) def restore_transaction_record_state(force_restore_state = false)
unless @_start_transaction_state.empty? if @_start_transaction_state
transaction_level = (@_start_transaction_state[:level] || 0) - 1 transaction_level = (@_start_transaction_state[:level] || 0) - 1
if transaction_level < 1 || force_restore_state if transaction_level < 1 || force_restore_state
restore_state = @_start_transaction_state restore_state = @_start_transaction_state
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册