Object Pooling in AS3

本文探讨了对象池设计模式的应用及其实现方式。介绍了三种不同类型的对象池:无界池、上限池和队列池,并讨论了它们各自的优缺点。此外还强调了对象清理的重要性以及在对象池中创建对象的方法。

If you're looking to save memory overhead and increase performance in your Flash creations then implementing the design pattern of Object Pooling is a great place to start. Object Pooling is most beneficial when the cost of construction is high or the frequency of construction is high.

 

Though it has to be known Object Pooling is not a silver bullet. There are definitely pitfalls to Object Pooling this most of all includes cleaning the Object for reuse. I'll get back to this in just a moment but first I have to talk about Object Pools generally.

 

In short an Object Pool allows you to retrieve an instance of a particular type of Object by checking it in and out of a resource pool. The concept of checking in and out resources is very consistent when using an Object Pool. Though handling the situation when a pool is empty is not.

 

There are three approaches I will talk about when handling an empty pool :

1) The Unbounded Pool

The pool has a boundless size and Objects are added as required to the resource pool on check out. This ensures that an Object will always be returned on check out.

 

2) The Capped Pool

The pool has a constant size and the resource pool is populated during the construction of the pool. This allows you to have all the memory overhead in the pool construction and not during check out. Though when check outs occur to an empty pool it won't return an Object.

 

3) The Queue Pool

The pool has a minimum size and the resource pool is populated during construction like option 1 it has the benefit of a maximum size - allowing the pool to grow. Unlike option 2 when the pool's maximum size or flood limit is reached and a check out occurs on an empty pool then this request is queued. Requests in the queue will be eventually dropped if not fulfilled within a defined timeout or alternatively resolved once an Object is checked in. This doesn't guarantee that the check out will be fulfilled by the Object Pool but does ensure that pools don't grow uncontrollably and when they reach flood limits that the next fulfilled check out should be the last.

 

Each of these approaches to controlling the resource pool size have unique benefits and the option you choose depends on how size matters :). Lets say in one scenario that Object memory allocation is inexpensive and having 1,000's to even 10,000's of Objects is no big deal then option 1 is a good choice. Especially considering it's simple and reliable. Though in another scenario where the same number of Objects are checked in as out and as a safe guard when a checkout occurs on an empty pool it can be suppressed then option 2 is a good choice. And doubly so considering a lot of the memory overhead of Object construction is on the Object Pool constructor and the pool will never grow out of your control. Though lastly if you can't suppress check outs on empty pools and the next Object checkout should be the last one and you need to limit your pool size due to the Objects being expensive then option 3 is more than likely the best approach for you.

 

Now I'll get back to Object cleaning, this sets an Object to the state it was in after construction which is important because check out is a replacement for constructing anew. The scenario when dirty Objects are added into the resource pool is often known as a cesspool and is considered an anti-pattern but honestly it's up to you to wether this worries you or not. Though in regard to Object cleaning it's also important to note this is where a clear pitfall emerges. If the process of cleaning is more expensive than construction then you should really consider not using an Object Pool.

 

If Object cleaning is important to you it gets interesting when consider the time and place to do this in your Object Pool class. It would seem logical to clean the Object within the check in method to ensure a cesspool never occurs. Though this could limit the Object Pool to containing a specific type of Object. This is because cleaning an Object is almost always subjective to type i.e. cleaning a DisplayObject is different to cleaning a BitmapData. Then thinking about this problem further the solution is "be kind rewind" - so the checkinee must clean before checkin. Problem solved.

 

And with every problem tackled there is another to solve. Creating an Object is also subjective to type due to parameters it may require in construction :( Unfortunately there isn't as ideal way to construct a Class with a dynamic number of parameters in Actionscript like function.apply offers for Functions. The simplest and most effective solution to this is once again the create process is external also. Another problem solved - hopefully that's all :)

 

So now that's enough talk on Object Pool's and hopefully it's given you some points to consider when creating your own Object Pool class. There's an example below demonstrating the Object Pool concepts I've used and discussed above and has a small performance victory. What this example demonstrates is the performance cost in reinitializing the particle effect constantly. As you will see there is quite a noticeable difference between pooling or not but when you compare one pool concept over another there's not much difference other than size handling features they offer which could save you performance.

 

If you would like to download the source code used in this example you can do so here


【无线传感器】使用 MATLAB和 XBee连续监控温度传感器无线网络研究(Matlab代码实现)内容概要:本文围绕使用MATLAB和XBee技术实现温度传感器无线网络的连续监控展开研究,介绍了如何构建无线传感网络系统,并利用MATLAB进行数据采集、处理与可视化分析。系统通过XBee模块实现传感器节点间的无线通信,实时传输温度数据至主机,MATLAB负责接收并处理数据,实现对环境温度的动态监测。文中详细阐述了硬件连接、通信协议配置、数据解析及软件编程实现过程,并提供了完整的MATLAB代码示例,便于读者复现和应用。该方案具有良好的扩展性和实用性,适用于远程环境监测场景。; 适合人群:具备一定MATLAB编程基础和无线通信基础知识的高校学生、科研人员及工程技术人员,尤其适合从事物联网、传感器网络相关项目开发的初学者与中级开发者。; 使用场景及目标:①实现基于XBee的无线温度传感网络搭建;②掌握MATLAB与无线模块的数据通信方法;③完成实时数据采集、处理与可视化;④为环境监测、工业测控等实际应用场景提供技术参考。; 阅读建议:建议读者结合文中提供的MATLAB代码与硬件连接图进行实践操作,先从简单的点对点通信入手,逐步扩展到多节点网络,同时可进一步探索数据滤波、异常检测、远程报警等功能的集成。
内容概要:本文系统讲解了边缘AI模型部署与优化的完整流程,涵盖核心挑战(算力、功耗、实时性、资源限制)与设计原则,详细对比主流边缘AI芯片平台(如ESP32-S3、RK3588、Jetson系列、Coral等)的性能参数与适用场景,并以RK3588部署YOLOv8为例,演示从PyTorch模型导出、ONNX转换、RKNN量化到Tengine推理的全流程。文章重点介绍多维度优化策略,包括模型轻量化(结构选择、输入尺寸调整)、量化(INT8/FP16)、剪枝与蒸馏、算子融合、批处理、硬件加速预处理及DVFS动态调频等,显著提升帧率并降低功耗。通过三个实战案例验证优化效果,最后提供常见问题解决方案与未来技术趋势。; 适合人群:具备一定AI模型开发经验的工程师,尤其是从事边缘计算、嵌入式AI、计算机视觉应用研发的技术人员,工作年限建议1-5年;熟悉Python、C++及深度学习框架(如PyTorch、TensorFlow)者更佳。; 使用场景及目标:①在资源受限的边缘设备上高效部署AI模型;②实现高帧率与低功耗的双重优化目标;③掌握从芯片选型、模型转换到系统级调优的全链路能力;④解决实际部署中的精度损失、内存溢出、NPU利用率低等问题。; 阅读建议:建议结合文中提供的代码实例与工具链(如RKNN Toolkit、Tengine、TensorRT)动手实践,重点关注量化校准、模型压缩与硬件协同优化环节,同时参考选型表格匹配具体应用场景,并利用功耗监测工具进行闭环调优。
### RoI Pooling in Deep Learning for Object Detection Region of Interest (RoI) Pooling 是一种用于处理不同大小区域的技术,在目标检测中扮演重要角色。该操作允许网络接受任意形状的输入并将其转换成固定尺寸的特征图,从而可以应用于后续的标准全连接层或其他分类器。 #### 工作原理 给定一系列候选框(即感兴趣区域),这些候选框可能来自Selective Search 或者其他提议算法,对于每一个候选框,RoI pooling会执行如下操作: - 将候选框映射到卷积层输出的空间位置上; - 对应于每个候选框的位置被划分为相同数量的小块或子窗口; - 应用最大池化或者平均池化来提取每个子窗口内的特征向量;最终得到一组长度固定的特征向量作为下一层的输入[^1]。 这种机制确保了无论原始图像中的物体如何缩放和平移变换,都能获得一致维度的特征描述符,提高了模型对尺度变化和空间布局差异的容忍度。 ```python import torch.nn.functional as F def roi_pooling(input, rois, pooled_height, pooled_width): """Apply ROI Pooling to input tensor.""" # Assume `input` is a feature map with shape [batch_size, channels, height, width] # and `rois` contains the coordinates of regions. batch_size = input.size(0) num_rois = rois.size(0) outputs = [] for i in range(num_rois): roi = rois[i] start_h, end_h = int(round(roi[0])), int(round(roi[2])) start_w, end_w = int(round(roi[1])), int(round(roi[3])) region = input[:, :, start_h:end_h, start_w:end_w] out = F.adaptive_max_pool2d(region, output_size=(pooled_height, pooled_width)) outputs.append(out.view(batch_size, -1)) return torch.cat(outputs, dim=1) ``` 上述代码展示了如何实现自适应的最大池化版本的ROI Pooling函数。这里使用PyTorch库来进行张量运算,并利用内置的功能实现了灵活调整输出大小的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值