Created
December 18, 2016 21:18
-
-
Save smcl/3d4785c86720b6643b60367a36b2203f to your computer and use it in GitHub Desktop.
weird snippet i saw in ironpython - they pre-allocate an cache of 1100 ints-as-objects in an array (in range -100..+10000) and avoid a number of int->object cast by returning the cached version. weird
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Gets a singleton boxed value for the given integer if possible, otherwise boxes the integer. | |
/// </summary> | |
/// <param name="value">The value to box.</param> | |
/// <returns>The boxed value.</returns> | |
public static object Int32ToObject(Int32 value) { | |
// caches improves pystone by ~5-10% on MS .Net 1.1, this is a very integer intense app | |
// TODO: investigate if this still helps perf. There's evidence that it's harmful on | |
// .NET 3.5 and 4.0 | |
if (value < MAX_CACHE && value >= MIN_CACHE) { | |
return cache[value - MIN_CACHE]; | |
} | |
return (object)value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment