Erlang:优化复杂的qlc

发布于 2024-10-31 22:17:03 字数 1599 浏览 3 评论 0原文

我有 qlc

RefsBlocked = qlc:e(qlc:q([
    Ref1 ||
    {{Ref1, {pattern, {_Status1, _Pattern1, Limit1}}}, Count} <- dict:to_list(
        qlc:fold(
            fun({Key, _Ref2}, Acc) ->
                dict:update_counter(Key, 1, Acc)
            end,
            dict:new(),
            qlc:q([
                {{Ref1, {pattern, {Status1, Pattern1, Limit1}}}, Ref2} ||
                {Ref2, {status, Status2}} <- ets:table(Tmp),
                {Ref3, {tag, Tag3}} <- ets:table(Tmp),
                Ref2 =:= Ref3,
                {Ref1, {pattern, {Status1, Pattern1, Limit1}}} <- ets:table(Tmp),
                Ref =:= Ref1,
                Status1 =:= Status2,
                Pattern1 =:= Tag3
            ])
        )
    ),
    Count >= Limit1
], unique))

,其中 Tmp 是 bag 类型的 ets,Ref 是我需要测试的特定标识符。

Ets 包含从数百到数千的条目,例如

{Ref1, {definition, {Tuple1}}}
{Ref1, {status, scheduled}}
{Ref1, {status, blocked}}
{Ref1, {pattern, {scheduled, Pattern11, Limit11}}}
{Ref1, {pattern, {dispatched, Pattern12, Limit12}}}
{Ref1, {tag, Tag11}}
{Ref2, {definition, {Tuple2}}}
{Ref2, {status, scheduled}}
{Ref2, {status, dispatched}}
{Ref2, {pattern, {scheduled, Pattern21, Limit21}}}
{Ref2, {pattern, {dispatched, Pattern22, Limit22}}}
{Ref2, {tag, Tag21}}
{Ref3, {definition, Tuple3}}
{Ref3, {status, error}}

每个 Ref 有一个定义、一个或两个(四个)状态、零个或多个(在大多数情况下不超过 3 个)模式以及零个或多个模式。更多(大多数情况下不超过 3 个)标签。

我需要测试一个特定标识符是否被其他标识符阻止。当与其标签上的任何模式匹配的标识符数量 = 其模式并且其状态 = 其模式状态大于或等于其模式限制时,它将被阻止。

有没有办法优化qlc?

I have qlc

RefsBlocked = qlc:e(qlc:q([
    Ref1 ||
    {{Ref1, {pattern, {_Status1, _Pattern1, Limit1}}}, Count} <- dict:to_list(
        qlc:fold(
            fun({Key, _Ref2}, Acc) ->
                dict:update_counter(Key, 1, Acc)
            end,
            dict:new(),
            qlc:q([
                {{Ref1, {pattern, {Status1, Pattern1, Limit1}}}, Ref2} ||
                {Ref2, {status, Status2}} <- ets:table(Tmp),
                {Ref3, {tag, Tag3}} <- ets:table(Tmp),
                Ref2 =:= Ref3,
                {Ref1, {pattern, {Status1, Pattern1, Limit1}}} <- ets:table(Tmp),
                Ref =:= Ref1,
                Status1 =:= Status2,
                Pattern1 =:= Tag3
            ])
        )
    ),
    Count >= Limit1
], unique))

where Tmp is an ets of type bag and Ref is a particular identifier I need to test.

Ets contains from hundreds to thousands of entries like

{Ref1, {definition, {Tuple1}}}
{Ref1, {status, scheduled}}
{Ref1, {status, blocked}}
{Ref1, {pattern, {scheduled, Pattern11, Limit11}}}
{Ref1, {pattern, {dispatched, Pattern12, Limit12}}}
{Ref1, {tag, Tag11}}
{Ref2, {definition, {Tuple2}}}
{Ref2, {status, scheduled}}
{Ref2, {status, dispatched}}
{Ref2, {pattern, {scheduled, Pattern21, Limit21}}}
{Ref2, {pattern, {dispatched, Pattern22, Limit22}}}
{Ref2, {tag, Tag21}}
{Ref3, {definition, Tuple3}}
{Ref3, {status, error}}

i. e. for each Ref there is one definition, one or two (of four) statuses, zero or more (in most cases no more than 3) patterns and zero or more (in most cases no more than 3) tags.

I need to test whether one particular identifier is blocked by others. It is blocked when the number of identifiers matching any of its patterns on their Tag = its Pattern and their Status = its pattern Status is more or equal to its pattern Limit.

Is there a way to optimize qlc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白龙吟 2024-11-07 22:17:03

除非您有不同的、更有效的等式关系可供使用,否则您拥有的代码就已经是最好的了。我想您已经分析过这段代码并发现它太慢了?以什么方式?

Unless you have a different, more efficient equality relation to use, the code you have there is as good as it gets. I imagine you've profiled this code and found it too slow? In what way?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文