Java整数标记和用于内存缩减的按位操作

前端之家收集整理的这篇文章主要介绍了Java整数标记和用于内存缩减的按位操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用整数标记和按位操作是减少大容量对象的内存占用的有效方法吗?

>记忆足迹

我的理解是,在JVM实现中通常将一个布尔值存储为一个int.它是否正确?在这种情况下,32个标志代表了大量的内存占用量减少.

虽然JVM实现当然不尽相同,但并不总是如此.
>表演

我的理解是,cpu是非常数字驱动的,按位操作与计算中的功能一样高效.

对布尔运算使用按位运算,是否有性能损失 – 甚至增益?
>替代品

有没有更好的方式来完成同样的事情?枚举允许标志的组合,即FLAGX = FLAG1 | FLAG2?

示例代码

注意最后一个方法propogateMove()是递归的,每秒可以调用几百次,并且对应用程序的响应性有直接影响,因此使用标志来避免逻辑位和调用其他方法.

  1. // FLAGS helper functions
  2. private final void setclear(int mask,boolean set) { if (set) set(mask); else clear(mask); }
  3. private final void set(int mask) { flags |= mask; }
  4. private final void clear(int mask) { flags &= ~mask; }
  5. private final boolean test(int mask) { return ((flags & mask) == mask); }
  6.  
  7.  
  8.  
  9. // Flags //////////////////////////////////////////////////////////////////////
  10.  
  11. private static final boolean HORIZONTAL = true;
  12. private static final boolean VERTICAL = false;
  13.  
  14. private static final int ORIENT = 0x00000001;
  15. private static final int DISPLAY = 0x00000002;
  16. private static final int HSHRINK = 0x00000004;
  17. private static final int VSHRINK = 0x00000008;
  18. private static final int SHRINK = HSHRINK | VSHRINK;
  19.  
  20. private static final int TILE_IMAGE = 0x00000010;
  21. private static final int CURSOR = 0x00000020;
  22. private static final int MOUSEINSIDE = 0x00000040;
  23. private static final int MOUSEINSIDE_BLOCKED = 0x00000080;
  24.  
  25. private static final int CONSTRAIN = 0x00000100;
  26. private static final int CONSTRAIN_DESCENDENT = 0x00000200;
  27. private static final int PLACE = 0x00000400;
  28. private static final int PLACE_DESCENDENT = 0x00000800;
  29. private static final int REFLOW = CONSTRAIN | CONSTRAIN_DESCENDENT | PLACE | PLACE_DESCENDENT;
  30.  
  31. private static final int PACK = 0x00001000;
  32. private static final int CLIP = 0x00002000;
  33. private static final int HAS_WIDTH_SLACK = 0x00004000;
  34. private static final int HAS_HEIGHT_SLACK = 0x00008000;
  35.  
  36. private static final int ALIGN_TOP = 0x00010000;
  37. private static final int ALIGN_BOTTOM = 0x00020000;
  38. private static final int ALIGN_LEFT = 0x00040000;
  39. private static final int ALIGN_RIGHT = 0x00080000;
  40. private static final int ALIGNS = ALIGN_TOP | ALIGN_BOTTOM | ALIGN_LEFT | ALIGN_RIGHT;
  41. private static final int ALIGN_TOPLEFT = ALIGN_TOP | ALIGN_LEFT;
  42. private static final int ALIGN_TOPRIGHT = ALIGN_TOP | ALIGN_RIGHT;
  43. private static final int ALIGN_BOTTOMLEFT = ALIGN_BOTTOM | ALIGN_LEFT;
  44. private static final int ALIGN_BOTTOMRIGHT = ALIGN_BOTTOM | ALIGN_RIGHT;
  45.  
  46. private static final int ENTER_TRAP = 0x00100000;
  47. private static final int LEAVE_TRAP = 0x00200000;
  48. private static final int _MOVE_TRAP = 0x00400000;
  49. private static final int MOVE_TRAP = 0x00800000;
  50.  
  51. private static final int CHILDREN_READ_TRAP = 0x01000000;
  52. private static final int CHILDREN_TRAP = 0x02000000;
  53. private static final int PLACE_CLEAN = 0x03000000;
  54.  
  55. private static final int SHRINK_TRAP = 0x04000000;
  56. private static final int HSHRINK_TRAP = 0x10000000;
  57. private static final int VSHRINK_TRAP = 0x20000000;
  58.  
  59. //private static final int UNUSED = 0x40000000;
  60. //private static final int UNUSED = 0x80000000;
  61.  
  62.  
  63.  
  64.  
  65. // Flags in switch ////////////////////////////////////////////////////////////
  66.  
  67. /** get align value as a string from align flags */
  68. private JS alignToJS() {
  69. switch(flags & ALIGNS) {
  70. case (ALIGN_TOPLEFT):
  71. return SC_align_topleft;
  72. case (ALIGN_BOTTOMLEFT):
  73. return SC_align_bottomleft;
  74. case (ALIGN_TOPRIGHT):
  75. return SC_align_topright;
  76. case (ALIGN_BOTTOMRIGHT):
  77. return SC_align_bottomright;
  78. case ALIGN_TOP:
  79. return SC_align_top;
  80. case ALIGN_BOTTOM:
  81. return SC_align_bottom;
  82. case ALIGN_LEFT:
  83. return SC_align_left;
  84. case ALIGN_RIGHT:
  85. return SC_align_right;
  86. case 0: // CENTER
  87. return SC_align_center;
  88. default:
  89. throw new Error("This should never happen; invalid alignment flags: " + (flags & ALIGNS));
  90. }
  91. }
  92.  
  93.  
  94.  
  95.  
  96. // Flags in logic /////////////////////////////////////////////////////////////
  97.  
  98. private final boolean propagateMove(int mousex,int mousey) throws JSExn {
  99. // start with pre-event _Move which preceeds Enter/Leave
  100. if (test(_MOVE_TRAP)) {
  101. if (Interpreter.CASCADE_PREVENTED == justTriggerTraps(SC__Move,JSU.T)) {
  102. // _Move cascade prevention induces Leave
  103. propagateLeave();
  104. // propagate cascade prevention
  105. return true;
  106. }
  107. }
  108.  
  109. // REMARK: anything from here on in is a partial interruption relative
  110. // to this Box so we can not call propagateLeave() directly upon it
  111. int i;
  112. boolean interrupted = false;
  113.  
  114. if (!test(PACK)) {
  115. // absolute layout - allows for interruption by overlaying siblings
  116. for (Box b = getChild(i=treeSize()-1); b != null; b = getChild(--i)) {
  117. if (!b.test(DISPLAY)) {
  118. continue;
  119. }
  120. if (interrupted) {
  121. b.propagateLeave();
  122. continue;
  123. }
  124. int b_mx = mousex-getXInParent(b);
  125. int b_my = mousey-getYInParent(b);
  126. if (b.inside(b_mx,b_my)) {
  127. if (b.propagateMove(b_mx,b_my)) {
  128. interrupted = true;
  129. }
  130. } else {
  131. b.propagateLeave();
  132. }
  133. }
  134. } else {
  135. // packed layout - interrupted still applies,plus packedhit shortcut
  136. boolean packedhit = false;
  137. for (Box b = getChild(i=treeSize()-1); b != null; b = getChild(--i)) {
  138. if (!b.test(DISPLAY)) {
  139. continue;
  140. }
  141. if (packedhit) {
  142. b.propagateLeave();
  143. continue;
  144. }
  145. int b_mx = mousex-getXInParent(b);
  146. int b_my = mousey-getYInParent(b);
  147. if (b.inside(b_mx,b_my)) {
  148. packedhit = true;
  149. if (b.propagateMove(b_mx,b_my)) {
  150. interrupted = true;
  151. }
  152. } else {
  153. b.propagateLeave();
  154. }
  155. }
  156. }
  157.  
  158. // child prevented cascade during _Move/Move which blocks
  159. // Enter on this Box - invoking Leave if necessary
  160. if (interrupted) {
  161. if (test(MOUSEINSIDE)) {
  162. if (!test(MOUSEINSIDE_BLOCKED)) {
  163. // mouse prevIoUsly inside,now blocked so invoke Leave
  164. set(MOUSEINSIDE_BLOCKED);
  165. if (test(LEAVE_TRAP)) {
  166. justTriggerTraps(SC_Leave,JSU.T);
  167. }
  168. }
  169. } else {
  170. // mouse not prevIoUsly inside,Enter not yet triggered,so
  171. // do not invoke Leave
  172. set(MOUSEINSIDE);
  173. set(MOUSEINSIDE_BLOCKED);
  174. }
  175. // propagate cascade prevention
  176. return true;
  177. }
  178.  
  179. // set cursor if applicable to this Box
  180. if (test(CURSOR)) {
  181. Surface s = getSurface();
  182. if (s!=null && !s.cursorset) {
  183. s.cursor = JSU.toString(getAndTriggerTraps(SC_cursor));
  184. s.cursorset = true;
  185. }
  186. }
  187.  
  188. // fire Enter traps
  189. if (!test(MOUSEINSIDE)) {
  190. set(MOUSEINSIDE);
  191. if (test(ENTER_TRAP)) {
  192. justTriggerTraps(SC_Enter,JSU.T);
  193. }
  194. }
  195.  
  196. // finish post-event Move which follows Enter/Leave
  197. if (test(MOVE_TRAP)) {
  198. if (Interpreter.CASCADE_PREVENTED == justTriggerTraps(SC_Move,JSU.T)) {
  199. // propagate cascade prevention
  200. return true;
  201. }
  202. }
  203.  
  204. // propagation uninterrupted
  205. return false;
  206. }

解决方法

It is my understanding that commonly a boolean is stored as an int in a JVM implementation. Is this correct?

当然这取决于JVM的实现,但对主流cpu的实现可能是正确的.

In which case surely the 32 flags represent a large memory footprint reduction.

如果你在一个类中实际上有32个标志,那么该类的大量实例就是.如果你没有超过几百个例子,那就不用担心了.

It is my understanding that cpus are very number driven and bitwise operations are about as efficient as things come in computing.

这是真的.

Is there a performance penalty – or even gain – to using bitwise operations over boolean operations?

这也取决于内存使用情况.如果你只用几个对象非常集中,那么按位操作可能会减慢速度.如果你有很多对象,由于更好的缓存行为,减少的内存可能会提高性能.

Is there a better way of accomplishing the same thing? Does an Enum allow the combination of flags i.e. FLAGX = FLAG1 | FLAG2?

而不是自己做点阵操作,你可以(而且应该)使用一个BitSet.是的,如果你可以使用Enums和一个EnumSet,它会更清晰,但是如果你有许多枚举,由于多个EnumSet实例的开销,它可能不会产生所需的内存节省.

猜你在找的Java相关文章