Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
3296dc25
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
3296dc25
编写于
3月 27, 2019
作者:
M
Matthew Draper
提交者:
GitHub
3月 27, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #31442 from ebeigarts/weak_descendants_tracker
Use weak references in descendants tracker
上级
08a435fa
7432c922
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
64 addition
and
4 deletion
+64
-4
activesupport/CHANGELOG.md
activesupport/CHANGELOG.md
+5
-0
activesupport/lib/active_support/descendants_tracker.rb
activesupport/lib/active_support/descendants_tracker.rb
+50
-4
activesupport/test/descendants_tracker_test_cases.rb
activesupport/test/descendants_tracker_test_cases.rb
+9
-0
未找到文件。
activesupport/CHANGELOG.md
浏览文件 @
3296dc25
*
Use weak references in descendants tracker to allow anonymous subclasses to
be garbage collected.
*Edgars Beigarts*
*
Update
`ActiveSupport::Notifications::Instrumenter#instrument`
to make
passing a block optional. This will let users use
`ActiveSupport::Notifications`
messaging features outside of
...
...
activesupport/lib/active_support/descendants_tracker.rb
浏览文件 @
3296dc25
# frozen_string_literal: true
require
"weakref"
module
ActiveSupport
# This module provides an internal implementation to track descendants
# which is faster than iterating through ObjectSpace.
...
...
@@ -8,7 +10,8 @@ module DescendantsTracker
class
<<
self
def
direct_descendants
(
klass
)
@@direct_descendants
[
klass
]
||
[]
descendants
=
@@direct_descendants
[
klass
]
descendants
?
descendants
.
to_a
:
[]
end
def
descendants
(
klass
)
...
...
@@ -34,15 +37,17 @@ def clear
# This is the only method that is not thread safe, but is only ever called
# during the eager loading phase.
def
store_inherited
(
klass
,
descendant
)
(
@@direct_descendants
[
klass
]
||=
[]
)
<<
descendant
(
@@direct_descendants
[
klass
]
||=
DescendantsArray
.
new
)
<<
descendant
end
private
def
accumulate_descendants
(
klass
,
acc
)
if
direct_descendants
=
@@direct_descendants
[
klass
]
acc
.
concat
(
direct_descendants
)
direct_descendants
.
each
{
|
direct_descendant
|
accumulate_descendants
(
direct_descendant
,
acc
)
}
direct_descendants
.
each
do
|
direct_descendant
|
acc
<<
direct_descendant
accumulate_descendants
(
direct_descendant
,
acc
)
end
end
end
end
...
...
@@ -59,5 +64,46 @@ def direct_descendants
def
descendants
DescendantsTracker
.
descendants
(
self
)
end
# DescendantsArray is an array that contains weak references to classes.
class
DescendantsArray
# :nodoc:
include
Enumerable
def
initialize
@refs
=
[]
end
def
initialize_copy
(
orig
)
@refs
=
@refs
.
dup
end
def
<<
(
klass
)
cleanup!
@refs
<<
WeakRef
.
new
(
klass
)
end
def
each
@refs
.
each
do
|
ref
|
yield
ref
.
__getobj__
rescue
WeakRef
::
RefError
end
end
def
refs_size
@refs
.
size
end
def
cleanup!
@refs
.
delete_if
{
|
ref
|
!
ref
.
weakref_alive?
}
end
def
reject!
@refs
.
reject!
do
|
ref
|
yield
ref
.
__getobj__
rescue
WeakRef
::
RefError
true
end
end
end
end
end
activesupport/test/descendants_tracker_test_cases.rb
浏览文件 @
3296dc25
...
...
@@ -27,6 +27,15 @@ def test_descendants
assert_equal_sets
[],
Child2
.
descendants
end
def
test_descendants_with_garbage_collected_classes
1
.
times
do
child_klass
=
Class
.
new
(
Parent
)
assert_equal_sets
[
Child1
,
Grandchild1
,
Grandchild2
,
Child2
,
child_klass
],
Parent
.
descendants
end
GC
.
start
assert_equal_sets
[
Child1
,
Grandchild1
,
Grandchild2
,
Child2
],
Parent
.
descendants
end
def
test_direct_descendants
assert_equal_sets
[
Child1
,
Child2
],
Parent
.
direct_descendants
assert_equal_sets
[
Grandchild1
,
Grandchild2
],
Child1
.
direct_descendants
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录