{"action":"created","issue":{"url":"https://api.github.com/repos/henix/ssoup/issues/1","labels_url":"https://api.github.com/repos/henix/ssoup/issues/1/labels{/name}","comments_url":"https://api.github.com/repos/henix/ssoup/issues/1/comments","events_url":"https://api.github.com/repos/henix/ssoup/issues/1/events","html_url":"https://github.com/henix/ssoup/issues/1","id":59446056,"number":1,"title":"dfsTraverse 性能提升","user":{"login":"henix","id":288251,"avatar_url":"https://avatars.githubusercontent.com/u/288251?v=3","gravatar_id":"","url":"https://api.github.com/users/henix","html_url":"https://github.com/henix","followers_url":"https://api.github.com/users/henix/followers","following_url":"https://api.github.com/users/henix/following{/other_user}","gists_url":"https://api.github.com/users/henix/gists{/gist_id}","starred_url":"https://api.github.com/users/henix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/henix/subscriptions","organizations_url":"https://api.github.com/users/henix/orgs","repos_url":"https://api.github.com/users/henix/repos","events_url":"https://api.github.com/users/henix/events{/privacy}","received_events_url":"https://api.github.com/users/henix/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-03-02T07:06:36Z","updated_at":"2015-03-10T07:27:54Z","closed_at":null,"body":"线上用 VisualVM sampler 发现 Iterator 的某个匿名中的 Iterator 的 hasNext 占用 CPU 较高。故考虑优化一下 dfsTraverse 中的 Iterator 使用。\r\n\r\n采用 ScalaMeter"},"comment":{"url":"https://api.github.com/repos/henix/ssoup/issues/comments/78007003","html_url":"https://github.com/henix/ssoup/issues/1#issuecomment-78007003","issue_url":"https://api.github.com/repos/henix/ssoup/issues/1","id":78007003,"user":{"login":"henix","id":288251,"avatar_url":"https://avatars.githubusercontent.com/u/288251?v=3","gravatar_id":"","url":"https://api.github.com/users/henix","html_url":"https://github.com/henix","followers_url":"https://api.github.com/users/henix/followers","following_url":"https://api.github.com/users/henix/following{/other_user}","gists_url":"https://api.github.com/users/henix/gists{/gist_id}","starred_url":"https://api.github.com/users/henix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/henix/subscriptions","organizations_url":"https://api.github.com/users/henix/orgs","repos_url":"https://api.github.com/users/henix/repos","events_url":"https://api.github.com/users/henix/events{/privacy}","received_events_url":"https://api.github.com/users/henix/received_events","type":"User","site_admin":false},"created_at":"2015-03-10T07:27:54Z","updated_at":"2015-03-10T07:27:54Z","body":"使用 jmh 重新进行了性能测试，比较下面两种 dfsTraverse 谁更高效：\r\n\r\n```scala\r\ndef dfsTraverse(e: Element): Iterator[Element] = Iterator.single(e) ++ getChilds(e).flatMap(dfsTraverse)\r\n\r\ndef dfsTraverse(e: Element): Iterator[Element] = if (e.childNodeSize() == 0) Iterator.single(e) else Iterator.single(e) ++ getChilds(e).flatMap(dfsTraverse)\r\n```\r\n\r\n第一感觉似乎第二种更好，而且对 gc 也更友好。但测量结果表明并不是这样。\r\n\r\n第一种实现的 3 次结果：\r\n\r\n```\r\n[info] Benchmark                                           Mode  Cnt     Score   Error   Units\r\n[info] SelectorsBenchmark.dfsTraverse                     thrpt   10  1033.550 ± 4.036   ops/s\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled  thrpt   10     7.000 ±   NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total     thrpt   10    19.000 ±   NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled   thrpt   10     7.000 ±   NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total      thrpt   10    73.000 ±   NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse                      avgt   10     0.963 ± 0.016   ms/op\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled   avgt   10    11.000 ±   NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total      avgt   10    24.000 ±   NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled    avgt   10    25.000 ±   NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total       avgt   10    68.000 ±   NaN      ms\r\n\r\n[info] Benchmark                                           Mode  Cnt     Score    Error   Units\r\n[info] SelectorsBenchmark.dfsTraverse                     thrpt   10  1060.601 ± 40.026   ops/s\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled  thrpt   10    11.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total     thrpt   10    22.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled   thrpt   10    29.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total      thrpt   10   109.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse                      avgt   10     0.952 ±  0.009   ms/op\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled   avgt   10     8.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total      avgt   10    20.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled    avgt   10    25.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total       avgt   10    76.000 ±    NaN      ms\r\n\r\n[info] Benchmark                                           Mode  Cnt     Score    Error   Units\r\n[info] SelectorsBenchmark.dfsTraverse                     thrpt   10  1063.436 ± 75.428   ops/s\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled  thrpt   10    18.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total     thrpt   10    35.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled   thrpt   10    48.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total      thrpt   10   111.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse                      avgt   10     0.928 ±  0.022   ms/op\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled   avgt   10    12.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total      avgt   10    26.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled    avgt   10    20.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total       avgt   10    69.000 ±    NaN      ms\r\n```\r\n\r\n第二种实现的 4 次结果：\r\n\r\n```\r\n[info] Benchmark                                           Mode  Cnt    Score    Error   Units\r\n[info] SelectorsBenchmark.dfsTraverse                     thrpt   10  991.781 ± 66.554   ops/s\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled  thrpt   10   22.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total     thrpt   10   33.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled   thrpt   10   51.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total      thrpt   10  144.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse                      avgt   10    0.980 ±  0.036   ms/op\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled   avgt   10    8.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total      avgt   10   20.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled    avgt   10    8.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total       avgt   10   46.000 ±    NaN      ms\r\n\r\n[info] Benchmark                                           Mode  Cnt     Score    Error   Units\r\n[info] SelectorsBenchmark.dfsTraverse                     thrpt   10  1082.235 ± 15.005   ops/s\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled  thrpt   10    14.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total     thrpt   10    29.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled   thrpt   10    28.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total      thrpt   10    87.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse                      avgt   10     0.958 ±  0.011   ms/op\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled   avgt   10     8.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total      avgt   10    20.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled    avgt   10    15.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total       avgt   10    67.000 ±    NaN      ms\r\n\r\n[info] Benchmark                                           Mode  Cnt     Score   Error   Units\r\n[info] SelectorsBenchmark.dfsTraverse                     thrpt   10  1075.301 ± 9.973   ops/s\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled  thrpt   10    10.000 ±   NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total     thrpt   10    22.000 ±   NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled   thrpt   10    23.000 ±   NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total      thrpt   10    72.000 ±   NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse                      avgt   10     0.966 ± 0.013   ms/op\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled   avgt   10     9.000 ±   NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total      avgt   10    21.000 ±   NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled    avgt   10     8.000 ±   NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total       avgt   10    51.000 ±   NaN      ms\r\n\r\n[info] Benchmark                                           Mode  Cnt     Score    Error   Units\r\n[info] SelectorsBenchmark.dfsTraverse                     thrpt   10  1025.276 ± 14.869   ops/s\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled  thrpt   10     8.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total     thrpt   10    20.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled   thrpt   10    36.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total      thrpt   10    91.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse                      avgt   10     0.964 ±  0.010   ms/op\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.profiled   avgt   10     8.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.count.total      avgt   10    20.000 ±    NaN  counts\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.profiled    avgt   10    28.000 ±    NaN      ms\r\n[info] SelectorsBenchmark.dfsTraverse:@gc.time.total       avgt   10    85.000 ±    NaN      ms\r\n```\r\n\r\n运行时间方面第一种似乎更优，而 gc 方面则看不出差别。"}}