Skip to content

Commit 50cd3c1

Browse files
committed
Merge pull request #4467 from gifnksm/inlining-core
Inlining methods/functions in core.
2 parents 38315fb + 62f2749 commit 50cd3c1

22 files changed

+338
-0
lines changed

src/libcore/char.rs

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ pub use is_XID_continue = unicode::derived_property::XID_Continue;
6464
* Indicates whether a character is in lower case, defined
6565
* in terms of the Unicode General Category 'Ll'
6666
*/
67+
#[inline(always)]
6768
pub pure fn is_lowercase(c: char) -> bool {
6869
return unicode::general_category::Ll(c);
6970
}
@@ -72,6 +73,7 @@ pub pure fn is_lowercase(c: char) -> bool {
7273
* Indicates whether a character is in upper case, defined
7374
* in terms of the Unicode General Category 'Lu'.
7475
*/
76+
#[inline(always)]
7577
pub pure fn is_uppercase(c: char) -> bool {
7678
return unicode::general_category::Lu(c);
7779
}
@@ -81,6 +83,7 @@ pub pure fn is_uppercase(c: char) -> bool {
8183
* terms of the Unicode General Categories 'Zs', 'Zl', 'Zp'
8284
* additional 'Cc'-category control codes in the range [0x09, 0x0d]
8385
*/
86+
#[inline(always)]
8487
pub pure fn is_whitespace(c: char) -> bool {
8588
return ('\x09' <= c && c <= '\x0d')
8689
|| unicode::general_category::Zs(c)
@@ -93,6 +96,7 @@ pub pure fn is_whitespace(c: char) -> bool {
9396
* defined in terms of the Unicode General Categories 'Nd', 'Nl', 'No'
9497
* and the Derived Core Property 'Alphabetic'.
9598
*/
99+
#[inline(always)]
96100
pub pure fn is_alphanumeric(c: char) -> bool {
97101
return unicode::derived_property::Alphabetic(c) ||
98102
unicode::general_category::Nd(c) ||
@@ -101,11 +105,13 @@ pub pure fn is_alphanumeric(c: char) -> bool {
101105
}
102106

103107
/// Indicates whether the character is an ASCII character
108+
#[inline(always)]
104109
pub pure fn is_ascii(c: char) -> bool {
105110
c - ('\x7F' & c) == '\x00'
106111
}
107112

108113
/// Indicates whether the character is numeric (Nd, Nl, or No)
114+
#[inline(always)]
109115
pub pure fn is_digit(c: char) -> bool {
110116
return unicode::general_category::Nd(c) ||
111117
unicode::general_category::Nl(c) ||
@@ -122,6 +128,7 @@ pub pure fn is_digit(c: char) -> bool {
122128
* 'b' or 'B', 11, etc. Returns none if the char does not
123129
* refer to a digit in the given radix.
124130
*/
131+
#[inline]
125132
pub pure fn to_digit(c: char, radix: uint) -> Option<uint> {
126133
let val = match c {
127134
'0' .. '9' => c as uint - ('0' as uint),
@@ -190,6 +197,7 @@ pub pure fn escape_default(c: char) -> ~str {
190197
*
191198
* -1 if a < b, 0 if a == b, +1 if a > b
192199
*/
200+
#[inline(always)]
193201
pub pure fn cmp(a: char, b: char) -> int {
194202
return if b > a { -1 }
195203
else if b < a { 1 }

src/libcore/clone.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ pub trait Clone {
1616
}
1717

1818
impl (): Clone {
19+
#[inline(always)]
1920
fn clone(&self) -> () { () }
2021
}

src/libcore/cmp.rs

+6
Original file line numberDiff line numberDiff line change
@@ -53,26 +53,32 @@ pub trait Ord {
5353
pure fn gt(&self, other: &self) -> bool;
5454
}
5555

56+
#[inline(always)]
5657
pub pure fn lt<T: Ord>(v1: &T, v2: &T) -> bool {
5758
(*v1).lt(v2)
5859
}
5960

61+
#[inline(always)]
6062
pub pure fn le<T: Ord Eq>(v1: &T, v2: &T) -> bool {
6163
(*v1).lt(v2) || (*v1).eq(v2)
6264
}
6365

66+
#[inline(always)]
6467
pub pure fn eq<T: Eq>(v1: &T, v2: &T) -> bool {
6568
(*v1).eq(v2)
6669
}
6770

71+
#[inline(always)]
6872
pub pure fn ne<T: Eq>(v1: &T, v2: &T) -> bool {
6973
(*v1).ne(v2)
7074
}
7175

76+
#[inline(always)]
7277
pub pure fn ge<T: Ord>(v1: &T, v2: &T) -> bool {
7378
(*v1).ge(v2)
7479
}
7580

81+
#[inline(always)]
7682
pub pure fn gt<T: Ord>(v1: &T, v2: &T) -> bool {
7783
(*v1).gt(v2)
7884
}

src/libcore/either.rs

+7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub enum Either<T, U> {
2828
Right(U)
2929
}
3030

31+
#[inline(always)]
3132
pub fn either<T, U, V>(f_left: fn(&T) -> V,
3233
f_right: fn(&U) -> V, value: &Either<T, U>) -> V {
3334
/*!
@@ -90,6 +91,7 @@ pub fn partition<T, U>(eithers: ~[Either<T, U>])
9091
return (move lefts, move rights);
9192
}
9293

94+
#[inline(always)]
9395
pub pure fn flip<T, U>(eith: Either<T, U>) -> Either<U, T> {
9496
//! Flips between left and right of a given either
9597
@@ -99,6 +101,7 @@ pub pure fn flip<T, U>(eith: Either<T, U>) -> Either<U, T> {
99101
}
100102
}
101103

104+
#[inline(always)]
102105
pub pure fn to_result<T, U>(eith: Either<T, U>)
103106
-> Result<U, T> {
104107
/*!
@@ -114,18 +117,21 @@ pub pure fn to_result<T, U>(eith: Either<T, U>)
114117
}
115118
}
116119

120+
#[inline(always)]
117121
pub pure fn is_left<T, U>(eith: &Either<T, U>) -> bool {
118122
//! Checks whether the given value is a left
119123
120124
match *eith { Left(_) => true, _ => false }
121125
}
122126

127+
#[inline(always)]
123128
pub pure fn is_right<T, U>(eith: &Either<T, U>) -> bool {
124129
//! Checks whether the given value is a right
125130
126131
match *eith { Right(_) => true, _ => false }
127132
}
128133

134+
#[inline(always)]
129135
pub pure fn unwrap_left<T,U>(eith: Either<T,U>) -> T {
130136
//! Retrieves the value in the left branch. Fails if the either is Right.
131137
@@ -134,6 +140,7 @@ pub pure fn unwrap_left<T,U>(eith: Either<T,U>) -> T {
134140
}
135141
}
136142

143+
#[inline(always)]
137144
pub pure fn unwrap_right<T,U>(eith: Either<T,U>) -> U {
138145
//! Retrieves the value in the right branch. Fails if the either is Left.
139146

src/libcore/f32.rs

+37
Original file line numberDiff line numberDiff line change
@@ -105,38 +105,52 @@ pub const infinity: f32 = 1.0_f32/0.0_f32;
105105

106106
pub const neg_infinity: f32 = -1.0_f32/0.0_f32;
107107

108+
#[inline(always)]
108109
pub pure fn is_NaN(f: f32) -> bool { f != f }
109110

111+
#[inline(always)]
110112
pub pure fn add(x: f32, y: f32) -> f32 { return x + y; }
111113

114+
#[inline(always)]
112115
pub pure fn sub(x: f32, y: f32) -> f32 { return x - y; }
113116

117+
#[inline(always)]
114118
pub pure fn mul(x: f32, y: f32) -> f32 { return x * y; }
115119

120+
#[inline(always)]
116121
pub pure fn div(x: f32, y: f32) -> f32 { return x / y; }
117122

123+
#[inline(always)]
118124
pub pure fn rem(x: f32, y: f32) -> f32 { return x % y; }
119125

126+
#[inline(always)]
120127
pub pure fn lt(x: f32, y: f32) -> bool { return x < y; }
121128

129+
#[inline(always)]
122130
pub pure fn le(x: f32, y: f32) -> bool { return x <= y; }
123131

132+
#[inline(always)]
124133
pub pure fn eq(x: f32, y: f32) -> bool { return x == y; }
125134

135+
#[inline(always)]
126136
pub pure fn ne(x: f32, y: f32) -> bool { return x != y; }
127137

138+
#[inline(always)]
128139
pub pure fn ge(x: f32, y: f32) -> bool { return x >= y; }
129140

141+
#[inline(always)]
130142
pub pure fn gt(x: f32, y: f32) -> bool { return x > y; }
131143

132144
// FIXME (#1999): replace the predicates below with llvm intrinsics or
133145
// calls to the libmath macros in the rust runtime for performance.
134146

135147
/// Returns true if `x` is a positive number, including +0.0f320 and +Infinity
148+
#[inline(always)]
136149
pub pure fn is_positive(x: f32) -> bool
137150
{ return x > 0.0f32 || (1.0f32/x) == infinity; }
138151

139152
/// Returns true if `x` is a negative number, including -0.0f320 and -Infinity
153+
#[inline(always)]
140154
pub pure fn is_negative(x: f32) -> bool
141155
{ return x < 0.0f32 || (1.0f32/x) == neg_infinity; }
142156

@@ -145,6 +159,7 @@ pub pure fn is_negative(x: f32) -> bool
145159
*
146160
* This is the same as `f32::is_negative`.
147161
*/
162+
#[inline(always)]
148163
pub pure fn is_nonpositive(x: f32) -> bool {
149164
return x < 0.0f32 || (1.0f32/x) == neg_infinity;
150165
}
@@ -154,21 +169,25 @@ pub pure fn is_nonpositive(x: f32) -> bool {
154169
*
155170
* This is the same as `f32::is_positive`.)
156171
*/
172+
#[inline(always)]
157173
pub pure fn is_nonnegative(x: f32) -> bool {
158174
return x > 0.0f32 || (1.0f32/x) == infinity;
159175
}
160176

161177
/// Returns true if `x` is a zero number (positive or negative zero)
178+
#[inline(always)]
162179
pub pure fn is_zero(x: f32) -> bool {
163180
return x == 0.0f32 || x == -0.0f32;
164181
}
165182

166183
/// Returns true if `x`is an infinite number
184+
#[inline(always)]
167185
pub pure fn is_infinite(x: f32) -> bool {
168186
return x == infinity || x == neg_infinity;
169187
}
170188

171189
/// Returns true if `x`is a finite number
190+
#[inline(always)]
172191
pub pure fn is_finite(x: f32) -> bool {
173192
return !(is_NaN(x) || is_infinite(x));
174193
}
@@ -219,45 +238,63 @@ pub mod consts {
219238
pub const ln_10: f32 = 2.30258509299404568401799145468436421_f32;
220239
}
221240

241+
#[inline(always)]
222242
pub pure fn signbit(x: f32) -> int {
223243
if is_negative(x) { return 1; } else { return 0; }
224244
}
225245

246+
#[inline(always)]
226247
pub pure fn logarithm(n: f32, b: f32) -> f32 {
227248
return log2(n) / log2(b);
228249
}
229250

230251
#[cfg(notest)]
231252
impl f32 : cmp::Eq {
253+
#[inline(always)]
232254
pure fn eq(&self, other: &f32) -> bool { (*self) == (*other) }
255+
#[inline(always)]
233256
pure fn ne(&self, other: &f32) -> bool { (*self) != (*other) }
234257
}
235258

236259
#[cfg(notest)]
237260
impl f32 : cmp::Ord {
261+
#[inline(always)]
238262
pure fn lt(&self, other: &f32) -> bool { (*self) < (*other) }
263+
#[inline(always)]
239264
pure fn le(&self, other: &f32) -> bool { (*self) <= (*other) }
265+
#[inline(always)]
240266
pure fn ge(&self, other: &f32) -> bool { (*self) >= (*other) }
267+
#[inline(always)]
241268
pure fn gt(&self, other: &f32) -> bool { (*self) > (*other) }
242269
}
243270

244271
impl f32: num::Num {
272+
#[inline(always)]
245273
pure fn add(&self, other: &f32) -> f32 { return *self + *other; }
274+
#[inline(always)]
246275
pure fn sub(&self, other: &f32) -> f32 { return *self - *other; }
276+
#[inline(always)]
247277
pure fn mul(&self, other: &f32) -> f32 { return *self * *other; }
278+
#[inline(always)]
248279
pure fn div(&self, other: &f32) -> f32 { return *self / *other; }
280+
#[inline(always)]
249281
pure fn modulo(&self, other: &f32) -> f32 { return *self % *other; }
282+
#[inline(always)]
250283
pure fn neg(&self) -> f32 { return -*self; }
251284

285+
#[inline(always)]
252286
pure fn to_int(&self) -> int { return *self as int; }
287+
#[inline(always)]
253288
static pure fn from_int(n: int) -> f32 { return n as f32; }
254289
}
255290

256291
impl f32: num::Zero {
292+
#[inline(always)]
257293
static pure fn zero() -> f32 { 0.0 }
258294
}
259295

260296
impl f32: num::One {
297+
#[inline(always)]
261298
static pure fn one() -> f32 { 1.0 }
262299
}
263300

0 commit comments

Comments
 (0)