{"action":"opened","issue":{"url":"https://api.github.com/repos/rust-lang/rust/issues/33885","repository_url":"https://api.github.com/repos/rust-lang/rust","labels_url":"https://api.github.com/repos/rust-lang/rust/issues/33885/labels{/name}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/33885/comments","events_url":"https://api.github.com/repos/rust-lang/rust/issues/33885/events","html_url":"https://github.com/rust-lang/rust/issues/33885","id":157040108,"number":33885,"title":"Ipv4Addr cmp() is slow","user":{"login":"polachok","id":94035,"avatar_url":"https://avatars.githubusercontent.com/u/94035?v=3","gravatar_id":"","url":"https://api.github.com/users/polachok","html_url":"https://github.com/polachok","followers_url":"https://api.github.com/users/polachok/followers","following_url":"https://api.github.com/users/polachok/following{/other_user}","gists_url":"https://api.github.com/users/polachok/gists{/gist_id}","starred_url":"https://api.github.com/users/polachok/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/polachok/subscriptions","organizations_url":"https://api.github.com/users/polachok/orgs","repos_url":"https://api.github.com/users/polachok/repos","events_url":"https://api.github.com/users/polachok/events{/privacy}","received_events_url":"https://api.github.com/users/polachok/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-05-26T17:46:17Z","updated_at":"2016-05-26T17:46:17Z","closed_at":null,"body":"I'm using `Ipv4Addr`s as keys in a BtreeMap. I have to look up the map ~15 million times per second (10G ethernet line rate).\r\n\r\nIpv4Addr cmp() compiles to this on my system (linux x86-64, rustc 1.10.0-nightly (476fe6eef 2016-05-21))\r\n\r\n```\r\n0000000000051f10 <_ZN52_$LT$net..ip..Ipv4Addr$u20$as$u20$core..cmp..Ord$GT$3cmp17h39591ec7a18c4b02E>:\r\n   51f10:       50                      push   %rax\r\n   51f11:       c7 44 24 04 1d 1d 1d    movl   $0x1d1d1d1d,0x4(%rsp)\r\n   51f18:       1d \r\n   51f19:       c7 04 24 1d 1d 1d 1d    movl   $0x1d1d1d1d,(%rsp)\r\n   51f20:       8b 07                   mov    (%rdi),%eax\r\n   51f22:       89 c1                   mov    %eax,%ecx\r\n   51f24:       88 44 24 04             mov    %al,0x4(%rsp)\r\n   51f28:       88 64 24 05             mov    %ah,0x5(%rsp)\r\n   51f2c:       c1 e8 10                shr    $0x10,%eax\r\n   51f2f:       c1 e9 18                shr    $0x18,%ecx\r\n   51f32:       88 44 24 06             mov    %al,0x6(%rsp)\r\n   51f36:       88 4c 24 07             mov    %cl,0x7(%rsp)\r\n   51f3a:       8b 06                   mov    (%rsi),%eax\r\n   51f3c:       89 c1                   mov    %eax,%ecx\r\n   51f3e:       88 04 24                mov    %al,(%rsp)\r\n   51f41:       88 64 24 01             mov    %ah,0x1(%rsp)\r\n   51f45:       c1 e8 10                shr    $0x10,%eax\r\n   51f48:       c1 e9 18                shr    $0x18,%ecx\r\n   51f4b:       88 44 24 02             mov    %al,0x2(%rsp)\r\n   51f4f:       88 4c 24 03             mov    %cl,0x3(%rsp)\r\n   51f53:       48 8d 7c 24 04          lea    0x4(%rsp),%rdi\r\n   51f58:       48 8d 34 24             lea    (%rsp),%rsi\r\n   51f5c:       ba 04 00 00 00          mov    $0x4,%edx\r\n   51f61:       e8 0a 17 fc ff          callq  13670 <memcmp@plt>\r\n   51f66:       89 c1                   mov    %eax,%ecx\r\n   51f68:       31 c0                   xor    %eax,%eax\r\n   51f6a:       85 c9                   test   %ecx,%ecx\r\n   51f6c:       b1 ff                   mov    $0xff,%cl\r\n   51f6e:       78 02                   js     51f72 <_ZN52_$LT$net..ip..Ipv4Addr$u20$as$u20$core..cmp..Ord$GT$3cmp17h39591ec7a18c4b02E+0x62>\r\n   51f70:       b1 01                   mov    $0x1,%cl\r\n   51f72:       74 02                   je     51f76 <_ZN52_$LT$net..ip..Ipv4Addr$u20$as$u20$core..cmp..Ord$GT$3cmp17h39591ec7a18c4b02E+0x66>\r\n   51f74:       88 c8                   mov    %cl,%al\r\n   51f76:       59                      pop    %rcx\r\n```\r\n\r\nWhich seems kinda inefficient for a thing which is basically u32.\r\nI guess part of the reason is the [implementation](http://doc.rust-lang.org/src/std/up/src/libstd/net/ip.rs.html#251) which converts it to an array(!) first.\r\nI copied the definition and implemented Ord like this:\r\n```\r\nimpl Ord for Ipv4Addr2 {\r\n\tfn cmp(&self, other: &Self) -> cmp::Ordering {\r\n\t\treturn Ord::cmp(&ntoh(self.inner.s_addr), &ntoh(other.inner.s_addr));\r\n\t}\r\n}\r\n```\r\n\r\nIt's about 10 times faster on my [benchmark](https://github.com/polachok/ipv4-cmp-bench).\r\n\r\n```\r\nrustc 1.10.0-nightly (476fe6eef 2016-05-21)\r\nbinary: rustc\r\ncommit-hash: 476fe6eefe17db91ff7a60aab34aa67a0a750a18\r\ncommit-date: 2016-05-21\r\nhost: x86_64-unknown-linux-gnu\r\nrelease: 1.10.0-nightly\r\n```"}}
{"actor":{"display_login":"polachok"}}