← Index
NYTProf Performance Profile   « line view »
For examples/benchmark4.pl
  Run on Thu Aug 28 19:01:43 2014
Reported on Thu Aug 28 19:02:42 2014

Filename/home/rdrake/code/netaddr-ip-fastnew/lib/NetAddr/IP/FastNew.pm
StatementsExecuted 1798405 statements in 8.59s
Subroutines
Calls P F Exclusive
Time
Inclusive
Time
Subroutine
405857112.07s2.41sNetAddr::IP::FastNew::::new_ipv4_maskNetAddr::IP::FastNew::new_ipv4_mask
432500111.88s2.09sNetAddr::IP::FastNew::::new_ipv4_cidrNetAddr::IP::FastNew::new_ipv4_cidr
527528111.79s2.01sNetAddr::IP::FastNew::::new_ipv4NetAddr::IP::FastNew::new_ipv4
1111.26ms22.9msNetAddr::IP::FastNew::::BEGIN@5NetAddr::IP::FastNew::BEGIN@5
11115µs28µsNetAddr::IP::FastNew::::BEGIN@3NetAddr::IP::FastNew::BEGIN@3
11114µs485µsNetAddr::IP::FastNew::::BEGIN@7NetAddr::IP::FastNew::BEGIN@7
1118µs8µsNetAddr::IP::FastNew::::BEGIN@11NetAddr::IP::FastNew::BEGIN@11
1117µs11µsNetAddr::IP::FastNew::::BEGIN@4NetAddr::IP::FastNew::BEGIN@4
1117µs62µsNetAddr::IP::FastNew::::BEGIN@8NetAddr::IP::FastNew::BEGIN@8
0000s0sNetAddr::IP::FastNew::::newNetAddr::IP::FastNew::new
0000s0sNetAddr::IP::FastNew::::new_ipv6NetAddr::IP::FastNew::new_ipv6
Call graph for these subroutines as a Graphviz dot language file.
Line State
ments
Time
on line
Calls Time
in subs
Code
1package NetAddr::IP::FastNew;
2
3226µs240µs
# spent 28µs (15+12) within NetAddr::IP::FastNew::BEGIN@3 which was called: # once (15µs+12µs) by main::BEGIN@5 at line 3
use strict;
# spent 28µs making 1 call to NetAddr::IP::FastNew::BEGIN@3 # spent 12µs making 1 call to strict::import
4222µs215µs
# spent 11µs (7+4) within NetAddr::IP::FastNew::BEGIN@4 which was called: # once (7µs+4µs) by main::BEGIN@5 at line 4
use warnings;
# spent 11µs making 1 call to NetAddr::IP::FastNew::BEGIN@4 # spent 4µs making 1 call to warnings::import
52109µs223.0ms
# spent 22.9ms (1.26+21.6) within NetAddr::IP::FastNew::BEGIN@5 which was called: # once (1.26ms+21.6ms) by main::BEGIN@5 at line 5
use NetAddr::IP;
# spent 22.9ms making 1 call to NetAddr::IP::FastNew::BEGIN@5 # spent 76µs making 1 call to NetAddr::IP::import
6# 1.95 required for inet_pton
7344µs3956µs
# spent 485µs (14+471) within NetAddr::IP::FastNew::BEGIN@7 which was called: # once (14µs+471µs) by main::BEGIN@5 at line 7
use Socket 1.95 qw(inet_pton AF_INET AF_INET6);
# spent 485µs making 1 call to NetAddr::IP::FastNew::BEGIN@7 # spent 462µs making 1 call to Exporter::import # spent 8µs making 1 call to UNIVERSAL::VERSION
8232µs2116µs
# spent 62µs (7+55) within NetAddr::IP::FastNew::BEGIN@8 which was called: # once (7µs+55µs) by main::BEGIN@5 at line 8
use NetAddr::IP::Util;
# spent 62µs making 1 call to NetAddr::IP::FastNew::BEGIN@8 # spent 55µs making 1 call to NetAddr::IP::Util::import
9# the minimum version I test with. 5.10 doesn't support inet_pton.
10# MSWin32 also doesn't support Socket::inet_pton
1121.07ms18µs
# spent 8µs within NetAddr::IP::FastNew::BEGIN@11 which was called: # once (8µs+0s) by main::BEGIN@5 at line 11
use v5.12.5;
# spent 8µs making 1 call to NetAddr::IP::FastNew::BEGIN@11
12
13# The following code is from spamassassin. I may use this to workaround Socket issues, but it only
14# helps if Socket6 is available. Currently this only affects two platforms on
15# cpantesters. OpenBSD (5.5) and GNUkfreebsd (8.1), so they could just upgrade to a
16# version of Socket.pm that supports inet_pton (they're both running 1.94 so
17# they only have to go up to 1.95)
18
19# # try to load inet_pton from Socket or Socket6
20# my $ip6 = eval {
21# require Socket;
22# Socket->VERSION(1.95);
23# Socket->import( 'inet_pton' );
24# 1;
25# } || eval {
26# require Socket6;
27# Socket6->import( 'inet_pton' );
28# 1;
29# };
30
31112µsour $VERSION = eval '0.3';
# spent 2µs executing statements in string eval
32
33=head1 NAME
34
35NetAddr::IP::FastNew - NetAddr::IP new() methods with no validation
36
37=head1 VERSION
38
390.3
40
41=head1 SYNOPSIS
42
43 use NetAddr::IP::FastNew;
44
45 my $ip = new NetAddr::IP::FastNew( '10.10.10.5' );
46
47=head1 DESCRIPTION
48
49This module is designed to quickly create NetAddr::IP objects.
50
51If you have a situation where you need 200_000 NetAddr::IP objects then the
52initialization speed can really become a problem.
53
54=head1 CREDITS
55
56Robert Drake, E<lt>rdrake@cpan.orgE<gt>
57
58=head1 COPYRIGHT AND LICENSE
59
60Copyright (C) 2014 by Robert Drake
61
62This library is free software; you can redistribute it and/or modify
63it under the same terms as Perl itself, either Perl version 5.8.7 or,
64at your option, any later version of Perl 5 you may have available.
65
66=cut
67
681500nsmy $zeros = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
691400nsmy $ones = ~$zeros;
70# this is to zero the ipv6 portion of the address. This is used when we're
71# building IPv4 objects.
721200nsmy $zerov6 = "\0\0\0\0\0\0\0\0\0\0\0\0";
73
74158µsmy $masks = {
75 0 => "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
76 1 => "\200\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
77 10 => "\377\300\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
78 100 => "\377\377\377\377\377\377\377\377\377\377\377\377\360\0\0\0",
79 101 => "\377\377\377\377\377\377\377\377\377\377\377\377\370\0\0\0",
80 102 => "\377\377\377\377\377\377\377\377\377\377\377\377\374\0\0\0",
81 103 => "\377\377\377\377\377\377\377\377\377\377\377\377\376\0\0\0",
82 104 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0",
83 105 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\200\0\0",
84 106 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\300\0\0",
85 107 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\340\0\0",
86 108 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\360\0\0",
87 109 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\370\0\0",
88 11 => "\377\340\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
89 110 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\374\0\0",
90 111 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\376\0\0",
91 112 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\0\0",
92 113 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\200\0",
93 114 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\300\0",
94 115 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\340\0",
95 116 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\360\0",
96 117 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\370\0",
97 118 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\374\0",
98 119 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\376\0",
99 12 => "\377\360\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
100 120 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\0",
101 121 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\200",
102 122 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\300",
103 123 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\340",
104 124 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\360",
105 125 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\370",
106 126 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\374",
107 127 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\376",
108 128 => "\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377\377",
109 13 => "\377\370\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
110 14 => "\377\374\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
111 15 => "\377\376\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
112 16 => "\377\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
113 17 => "\377\377\200\0\0\0\0\0\0\0\0\0\0\0\0\0",
114 18 => "\377\377\300\0\0\0\0\0\0\0\0\0\0\0\0\0",
115 19 => "\377\377\340\0\0\0\0\0\0\0\0\0\0\0\0\0",
116 2 => "\300\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
117 20 => "\377\377\360\0\0\0\0\0\0\0\0\0\0\0\0\0",
118 21 => "\377\377\370\0\0\0\0\0\0\0\0\0\0\0\0\0",
119 22 => "\377\377\374\0\0\0\0\0\0\0\0\0\0\0\0\0",
120 23 => "\377\377\376\0\0\0\0\0\0\0\0\0\0\0\0\0",
121 24 => "\377\377\377\0\0\0\0\0\0\0\0\0\0\0\0\0",
122 25 => "\377\377\377\200\0\0\0\0\0\0\0\0\0\0\0\0",
123 26 => "\377\377\377\300\0\0\0\0\0\0\0\0\0\0\0\0",
124 27 => "\377\377\377\340\0\0\0\0\0\0\0\0\0\0\0\0",
125 28 => "\377\377\377\360\0\0\0\0\0\0\0\0\0\0\0\0",
126 29 => "\377\377\377\370\0\0\0\0\0\0\0\0\0\0\0\0",
127 3 => "\340\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
128 30 => "\377\377\377\374\0\0\0\0\0\0\0\0\0\0\0\0",
129 31 => "\377\377\377\376\0\0\0\0\0\0\0\0\0\0\0\0",
130 32 => "\377\377\377\377\0\0\0\0\0\0\0\0\0\0\0\0",
131 33 => "\377\377\377\377\200\0\0\0\0\0\0\0\0\0\0\0",
132 34 => "\377\377\377\377\300\0\0\0\0\0\0\0\0\0\0\0",
133 35 => "\377\377\377\377\340\0\0\0\0\0\0\0\0\0\0\0",
134 36 => "\377\377\377\377\360\0\0\0\0\0\0\0\0\0\0\0",
135 37 => "\377\377\377\377\370\0\0\0\0\0\0\0\0\0\0\0",
136 38 => "\377\377\377\377\374\0\0\0\0\0\0\0\0\0\0\0",
137 39 => "\377\377\377\377\376\0\0\0\0\0\0\0\0\0\0\0",
138 4 => "\360\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
139 40 => "\377\377\377\377\377\0\0\0\0\0\0\0\0\0\0\0",
140 41 => "\377\377\377\377\377\200\0\0\0\0\0\0\0\0\0\0",
141 42 => "\377\377\377\377\377\300\0\0\0\0\0\0\0\0\0\0",
142 43 => "\377\377\377\377\377\340\0\0\0\0\0\0\0\0\0\0",
143 44 => "\377\377\377\377\377\360\0\0\0\0\0\0\0\0\0\0",
144 45 => "\377\377\377\377\377\370\0\0\0\0\0\0\0\0\0\0",
145 46 => "\377\377\377\377\377\374\0\0\0\0\0\0\0\0\0\0",
146 47 => "\377\377\377\377\377\376\0\0\0\0\0\0\0\0\0\0",
147 48 => "\377\377\377\377\377\377\0\0\0\0\0\0\0\0\0\0",
148 49 => "\377\377\377\377\377\377\200\0\0\0\0\0\0\0\0\0",
149 5 => "\370\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
150 50 => "\377\377\377\377\377\377\300\0\0\0\0\0\0\0\0\0",
151 51 => "\377\377\377\377\377\377\340\0\0\0\0\0\0\0\0\0",
152 52 => "\377\377\377\377\377\377\360\0\0\0\0\0\0\0\0\0",
153 53 => "\377\377\377\377\377\377\370\0\0\0\0\0\0\0\0\0",
154 54 => "\377\377\377\377\377\377\374\0\0\0\0\0\0\0\0\0",
155 55 => "\377\377\377\377\377\377\376\0\0\0\0\0\0\0\0\0",
156 56 => "\377\377\377\377\377\377\377\0\0\0\0\0\0\0\0\0",
157 57 => "\377\377\377\377\377\377\377\200\0\0\0\0\0\0\0\0",
158 58 => "\377\377\377\377\377\377\377\300\0\0\0\0\0\0\0\0",
159 59 => "\377\377\377\377\377\377\377\340\0\0\0\0\0\0\0\0",
160 6 => "\374\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
161 60 => "\377\377\377\377\377\377\377\360\0\0\0\0\0\0\0\0",
162 61 => "\377\377\377\377\377\377\377\370\0\0\0\0\0\0\0\0",
163 62 => "\377\377\377\377\377\377\377\374\0\0\0\0\0\0\0\0",
164 63 => "\377\377\377\377\377\377\377\376\0\0\0\0\0\0\0\0",
165 64 => "\377\377\377\377\377\377\377\377\0\0\0\0\0\0\0\0",
166 65 => "\377\377\377\377\377\377\377\377\200\0\0\0\0\0\0\0",
167 66 => "\377\377\377\377\377\377\377\377\300\0\0\0\0\0\0\0",
168 67 => "\377\377\377\377\377\377\377\377\340\0\0\0\0\0\0\0",
169 68 => "\377\377\377\377\377\377\377\377\360\0\0\0\0\0\0\0",
170 69 => "\377\377\377\377\377\377\377\377\370\0\0\0\0\0\0\0",
171 7 => "\376\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
172 70 => "\377\377\377\377\377\377\377\377\374\0\0\0\0\0\0\0",
173 71 => "\377\377\377\377\377\377\377\377\376\0\0\0\0\0\0\0",
174 72 => "\377\377\377\377\377\377\377\377\377\0\0\0\0\0\0\0",
175 73 => "\377\377\377\377\377\377\377\377\377\200\0\0\0\0\0\0",
176 74 => "\377\377\377\377\377\377\377\377\377\300\0\0\0\0\0\0",
177 75 => "\377\377\377\377\377\377\377\377\377\340\0\0\0\0\0\0",
178 76 => "\377\377\377\377\377\377\377\377\377\360\0\0\0\0\0\0",
179 77 => "\377\377\377\377\377\377\377\377\377\370\0\0\0\0\0\0",
180 78 => "\377\377\377\377\377\377\377\377\377\374\0\0\0\0\0\0",
181 79 => "\377\377\377\377\377\377\377\377\377\376\0\0\0\0\0\0",
182 8 => "\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
183 80 => "\377\377\377\377\377\377\377\377\377\377\0\0\0\0\0\0",
184 81 => "\377\377\377\377\377\377\377\377\377\377\200\0\0\0\0\0",
185 82 => "\377\377\377\377\377\377\377\377\377\377\300\0\0\0\0\0",
186 83 => "\377\377\377\377\377\377\377\377\377\377\340\0\0\0\0\0",
187 84 => "\377\377\377\377\377\377\377\377\377\377\360\0\0\0\0\0",
188 85 => "\377\377\377\377\377\377\377\377\377\377\370\0\0\0\0\0",
189 86 => "\377\377\377\377\377\377\377\377\377\377\374\0\0\0\0\0",
190 87 => "\377\377\377\377\377\377\377\377\377\377\376\0\0\0\0\0",
191 88 => "\377\377\377\377\377\377\377\377\377\377\377\0\0\0\0\0",
192 89 => "\377\377\377\377\377\377\377\377\377\377\377\200\0\0\0\0",
193 9 => "\377\200\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
194 90 => "\377\377\377\377\377\377\377\377\377\377\377\300\0\0\0\0",
195 91 => "\377\377\377\377\377\377\377\377\377\377\377\340\0\0\0\0",
196 92 => "\377\377\377\377\377\377\377\377\377\377\377\360\0\0\0\0",
197 93 => "\377\377\377\377\377\377\377\377\377\377\377\370\0\0\0\0",
198 94 => "\377\377\377\377\377\377\377\377\377\377\377\374\0\0\0\0",
199 95 => "\377\377\377\377\377\377\377\377\377\377\377\376\0\0\0\0",
200 96 => "\377\377\377\377\377\377\377\377\377\377\377\377\0\0\0\0",
201 97 => "\377\377\377\377\377\377\377\377\377\377\377\377\200\0\0\0",
202 98 => "\377\377\377\377\377\377\377\377\377\377\377\377\300\0\0\0",
203 99 => "\377\377\377\377\377\377\377\377\377\377\377\377\340\0\0\0"
204};
205
206
207114µsmy $masks4 = {
208 0 => "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0",
209 1 => "\0\0\0\0\0\0\0\0\0\0\0\0\200\0\0\0",
210 3 => "\0\0\0\0\0\0\0\0\0\0\0\0\340\0\0\0",
211 2 => "\0\0\0\0\0\0\0\0\0\0\0\0\300\0\0\0",
212 4 => "\0\0\0\0\0\0\0\0\0\0\0\0\360\0\0\0",
213 5 => "\0\0\0\0\0\0\0\0\0\0\0\0\370\0\0\0",
214 6 => "\0\0\0\0\0\0\0\0\0\0\0\0\374\0\0\0",
215 7 => "\0\0\0\0\0\0\0\0\0\0\0\0\376\0\0\0",
216 8 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0",
217 9 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\200\0\0",
218 10 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\300\0\0",
219 11 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\340\0\0",
220 12 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\360\0\0",
221 13 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\370\0\0",
222 14 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\374\0\0",
223 15 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\376\0\0",
224 16 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\0\0",
225 17 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\200\0",
226 18 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\300\0",
227 19 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\340\0",
228 20 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\360\0",
229 21 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\370\0",
230 22 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\374\0",
231 23 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\376\0",
232 24 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\0",
233 25 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\200",
234 26 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\300",
235 27 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\340",
236 28 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\360",
237 29 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\370",
238 30 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\374",
239 31 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\376",
240 32 => "\0\0\0\0\0\0\0\0\0\0\0\0\377\377\377\377",
241};
242
- -
245=head1 METHODS
246
247=head2 new
248
249Right now this just calls NetAddr::IP->new().
250
251 my $ip = NetAddr::IP::FastNew->new("127.0.0.1");
252
253=cut
254
255sub new {
256 # attempt to beat NetAddr::IP speeds by guessing the type of address and
257 # initializing it. This will probably not support nearly as many formats
258 # as the original, but will be useful to some users who want something
259 # fast and easy.
260 my $class = shift;
261 return NetAddr::IP->new(@_);
262}
263
264
265=head2 new_ipv4
266
267Create a real NetAddr::IP from a single IPv4 address with almost no
268validation. This has more overhead than the FastNew new() but it's much
269faster if you make use of the IP object.
270
271This only takes one argument, the single IP address. Anything else will fail
272in (probably) bad ways. Validation is completely up to you and is not done
273here.
274
275 my $ip = NetAddr::IP::FastNew->new_ipv4("127.0.0.1");
276
277=cut
278
279
280
# spent 2.01s (1.79+219ms) within NetAddr::IP::FastNew::new_ipv4 which was called 527528 times, avg 4µs/call: # 527528 times (1.79s+219ms) by main::__ANON__[examples/benchmark4.pl:10] at line 10 of examples/benchmark4.pl, avg 4µs/call
sub new_ipv4 {
2815275282.81s527528219ms return bless {
# spent 219ms making 527528 calls to Socket::inet_pton, avg 416ns/call
282 addr => $zerov6 . inet_pton(AF_INET, $_[1]),
283 mask => $ones,
284 isv6 => 0,
285 }, 'NetAddr::IP';
286}
287
288=head2 new_ipv4_mask
289
290Create a real NetAddr::IP from a IPv4 subnet with almost no
291validation.
292
293This requires the IP address and the subnet mask as it's two arguments.
294Anything else will fail in (probably) bad ways. Validation is completely
295up to the caller is not done here.
296
297 my $ip = NetAddr::IP::FastNew->new_ipv4_mask("127.0.0.0", "255.255.255.0");
298
299=cut
300
301
# spent 2.41s (2.07+338ms) within NetAddr::IP::FastNew::new_ipv4_mask which was called 405857 times, avg 6µs/call: # 405857 times (2.07s+338ms) by main::__ANON__[examples/benchmark4.pl:11] at line 11 of examples/benchmark4.pl, avg 6µs/call
sub new_ipv4_mask {
3024058573.01s811714338ms return bless {
# spent 338ms making 811714 calls to Socket::inet_pton, avg 417ns/call
303 addr => $zerov6 . inet_pton(AF_INET, $_[1]),
304 mask => $zerov6 . inet_pton(AF_INET, $_[2]),
305 isv6 => 0,
306 }, 'NetAddr::IP';
307}
308
309=head2 new_ipv4_cidr
310
311Create a real NetAddr::IP object from a IPv4 cidr with almost no
312validation.
313
314This requires the IP address and the cidr in a single argument.
315Anything else will fail in (probably) bad ways. Validation is completely
316up to the caller is not done here.
317
318 my $ip = NetAddr::IP::FastNew->new_ipv4_cidr("127.0.0.0/24");
319
320=cut
321
322# you can use the ipv6 mask table by saying $masks->{128-32+$cidr}. Since
323# it's an IPv4 address NetAddr::IP ignores the first 2 hextets.
324# Unfortunately, it seems to be about 50,000 calls/sec slower than having an
325# ipv4 mask table (I guess the overhead of doing the simple 96+$cidr math..)
326
327# for whatever reason, split is faster for this too, but this is the slowest
328# function. Try to use the other two when possible.
329
330
# spent 2.09s (1.88+218ms) within NetAddr::IP::FastNew::new_ipv4_cidr which was called 432500 times, avg 5µs/call: # 432500 times (1.88s+218ms) by main::__ANON__[examples/benchmark4.pl:12] at line 12 of examples/benchmark4.pl, avg 5µs/call
sub new_ipv4_cidr {
331432500325ms my ($ip, $cidr) = split('/', $_[1]);
332 #my $pos = index($_[1],'/');
333 #my $ip = substr($_[1], 0, $pos);
334
3354325002.44s432500218ms return bless { 'addr' => $zerov6 . inet_pton(AF_INET, $ip),
# spent 218ms making 432500 calls to Socket::inet_pton, avg 504ns/call
336 'mask' => $masks4->{$cidr},
337 'isv6' => 0
338 }, 'NetAddr::IP';
339}
340
341=head2 new_ipv6
342
343Create a real NetAddr::IP object from an IPv6 subnet with no validation. This
344is almost as fast as the lazy object. The only caveat being it requires a
345cidr mask.
346
347 my $ip = NetAddr::IP::FastNew->new_ipv6("fe80::/64");
348
349=cut
350
351sub new_ipv6 {
352 my $pos = index($_[1],'/');
353 my $ip = substr($_[1], 0, $pos);
354 return bless { 'addr' => inet_pton(AF_INET6, $ip), 'mask' => $masks->{substr($_[1], $pos+1)}, 'isv6' => 1 }, 'NetAddr::IP';
355}
356
357
358123µs1;