Skip to content

Instantly share code, notes, and snippets.

@amirshim
amirshim / gist:5602245
Created May 17, 2013 21:52
Huge memory leak
def mainLoop() : Unit@cps[Future[Any]] = {
while (true) {
val fut = Promise.successful(new Array[Byte](1000000*100)).future // 100MB
// This shift is the same as Akka DataFlow's DataflowFuture.apply()
val result = shift { onComplete: (Array[Byte] => Future[Any]) =>
fut flatMap onComplete
}
println(result)
@amirshim
amirshim / gist:5602077
Created May 17, 2013 21:25
Future's flatmap holds on to too much in it's closure... here's a simple fixed. Based on code from: https://github.com/scala/scala/blob/1f4a52b4ed9457863e00fe16d18705b6c6cd5db9/src/library/scala/concurrent/Future.scala#L271
def flatMap[S](f: T => Future[S])(implicit executor: ExecutionContext): Future[S] = {
val p = Promise[S]()
onComplete {
case f: Failure[_] => p complete f.asInstanceOf[Failure[S]]
case Success(v) => {
val tempP = p
try {
f(v).onComplete({
case f: Failure[_] => tempP complete f.asInstanceOf[Failure[S]]
class MyOpt[T] {
private var _name : String = null
def name = _name
}
abstract class BaseConfig {
protected def opt[T](unused : Int = 0) : MyOpt[T] = new MyOpt[T]
protected def onVerify {
val field = classOf[MyOpt[_]].getDeclaredField("_name")
field.setAccessible(true)
@amirshim
amirshim / coffee_output.js
Created July 22, 2011 00:56
CoffeeScript With tamejs - example 1
var x;
for (x = 1; x < 10; x++) {
twait {;
setTimeout(defer(), 100);
};
console.log('hello ' + x);
}
Thing = (function() {
var private_name;
__extends(Thing, EventEmitter);
function Thing(name) {
private_name = name;
}
return Thing;
})();
@amirshim
amirshim / updated getSelectedIds()
Created January 28, 2011 17:49
returns name/id object pair list
this.getSelectedIds = function() {
var ids = [];
$.each(elem.find(".jfmfs-friend.selected"), function(i, friend) {
ids.push({id:$(friend).attr("data-id"), name:$(friend).find('.friend-name').text()});
});
return ids;
};
# for GAE - tries get the image size... not too efficient...
# could throw lots of different exceptions, including DeadlineExceededError
class ImageBlobHelper(object):
@staticmethod
def TryGetWidthHeight(blobinfo, read_increments = 32768):
total_read = 0
blob_key = blobinfo.key();
data = ''
while total_read < blobinfo.size:
data_read = blobstore.fetch_data(blob_key, total_read, total_read+read_increments)
// see http://github.com/amirshim/plupload/commit/edf12ae91a361c63260e5228ee7fc0e8934819fd
// and http://github.com/moxiecode/plupload/issues/issue/73
var preUploadCb = function(file, onComplete) {
up = this;
up.settings.url = null;
$.ajax({
url: '/upload/generate_upload_url',
success: function(data) {
up.settings.url = data;