Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
9f84e60a
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,发现更多精彩内容 >>
提交
9f84e60a
编写于
6月 30, 2012
作者:
T
thedarkone
提交者:
Aaron Patterson
10月 18, 2012
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Make DescendantsTracker thread safe and optimize the #descendants method.
上级
4f106bbb
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
51 addition
and
30 deletion
+51
-30
activesupport/lib/active_support/descendants_tracker.rb
activesupport/lib/active_support/descendants_tracker.rb
+34
-19
activesupport/test/descendants_tracker_test_cases.rb
activesupport/test/descendants_tracker_test_cases.rb
+12
-6
activesupport/test/descendants_tracker_with_autoloading_test.rb
...support/test/descendants_tracker_with_autoloading_test.rb
+5
-5
未找到文件。
activesupport/lib/active_support/descendants_tracker.rb
浏览文件 @
9f84e60a
...
...
@@ -2,20 +2,20 @@ module ActiveSupport
# This module provides an internal implementation to track descendants
# which is faster than iterating through ObjectSpace.
module
DescendantsTracker
@@direct_descendants
=
Hash
.
new
{
|
h
,
k
|
h
[
k
]
=
[]
}
@@direct_descendants
=
{
}
def
self
.
direct_descendants
(
klass
)
@@direct_descendants
[
klass
]
class
<<
self
def
direct_descendants
(
klass
)
@@direct_descendants
[
klass
]
||
[]
end
def
self
.
descendants
(
klass
)
@@direct_descendants
[
klass
].
inject
([])
do
|
descendants
,
_klass
|
descendants
<<
_klass
descendants
.
concat
_klass
.
descendants
end
def
descendants
(
klass
)
arr
=
[]
accumulate_descendants
(
klass
,
arr
)
arr
end
def
self
.
clear
def
clear
if
defined?
ActiveSupport
::
Dependencies
@@direct_descendants
.
each
do
|
klass
,
descendants
|
if
ActiveSupport
::
Dependencies
.
autoloaded?
(
klass
)
...
...
@@ -29,8 +29,23 @@ def self.clear
end
end
# 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
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
)
}
end
end
end
def
inherited
(
base
)
self
.
direct_descendants
<<
base
DescendantsTracker
.
store_inherited
(
self
,
base
)
super
end
...
...
activesupport/test/descendants_tracker_test_cases.rb
浏览文件 @
9f84e60a
require
'set'
module
DescendantsTrackerTestCases
class
Parent
extend
ActiveSupport
::
DescendantsTracker
...
...
@@ -18,15 +20,15 @@ class Grandchild2 < Child1
ALL
=
[
Parent
,
Child1
,
Child2
,
Grandchild1
,
Grandchild2
]
def
test_descendants
assert_equal
[
Child1
,
Grandchild1
,
Grandchild2
,
Child2
],
Parent
.
descendants
assert_equal
[
Grandchild1
,
Grandchild2
],
Child1
.
descendants
assert_equal
[],
Child2
.
descendants
assert_equal
_sets
[
Child1
,
Grandchild1
,
Grandchild2
,
Child2
],
Parent
.
descendants
assert_equal
_sets
[
Grandchild1
,
Grandchild2
],
Child1
.
descendants
assert_equal
_sets
[],
Child2
.
descendants
end
def
test_direct_descendants
assert_equal
[
Child1
,
Child2
],
Parent
.
direct_descendants
assert_equal
[
Grandchild1
,
Grandchild2
],
Child1
.
direct_descendants
assert_equal
[],
Child2
.
direct_descendants
assert_equal
_sets
[
Child1
,
Child2
],
Parent
.
direct_descendants
assert_equal
_sets
[
Grandchild1
,
Grandchild2
],
Child1
.
direct_descendants
assert_equal
_sets
[],
Child2
.
direct_descendants
end
def
test_clear
...
...
@@ -40,6 +42,10 @@ def test_clear
protected
def
assert_equal_sets
(
expected
,
actual
)
Set
.
new
(
expected
)
==
Set
.
new
(
actual
)
end
def
mark_as_autoloaded
(
*
klasses
)
# If ActiveSupport::Dependencies is not loaded, forget about autoloading.
# This allows using AS::DescendantsTracker without AS::Dependencies.
...
...
activesupport/test/descendants_tracker_with_autoloading_test.rb
浏览文件 @
9f84e60a
...
...
@@ -18,17 +18,17 @@ def test_clear_with_autoloaded_parent_children_and_granchildren
def
test_clear_with_autoloaded_children_and_granchildren
mark_as_autoloaded
Child1
,
Grandchild1
,
Grandchild2
do
ActiveSupport
::
DescendantsTracker
.
clear
assert_equal
[
Child2
],
Parent
.
descendants
assert_equal
[],
Child2
.
descendants
assert_equal
_sets
[
Child2
],
Parent
.
descendants
assert_equal
_sets
[],
Child2
.
descendants
end
end
def
test_clear_with_autoloaded_granchildren
mark_as_autoloaded
Grandchild1
,
Grandchild2
do
ActiveSupport
::
DescendantsTracker
.
clear
assert_equal
[
Child1
,
Child2
],
Parent
.
descendants
assert_equal
[],
Child1
.
descendants
assert_equal
[],
Child2
.
descendants
assert_equal
_sets
[
Child1
,
Child2
],
Parent
.
descendants
assert_equal
_sets
[],
Child1
.
descendants
assert_equal
_sets
[],
Child2
.
descendants
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录