Skip to content

Instantly share code, notes, and snippets.

@xudifsd
xudifsd / t.py
Created August 19, 2013 02:47
there are two method for get timestamp, first is just use system call, second is use system call for the first time, and then start a background thread and let system to schedule a inc function every second. from this test, we can see that the second method is more efficient.
#!/usr/bin/env python
import threading, time
time1 = 0
def get_time1():
return time1
def get_time2():
@xudifsd
xudifsd / load-blancer.clj
Last active December 21, 2015 13:48
use zmq as concurrent server
(ns load-blancer
(:import [org.zeromq ZMQ ZMQ$PollItem ZMQ$Poller]))
; the functionality of this is same with zmq-server.clj, but we handle routing manually
(defn dequeue! [queue-ref]
(dosync
(let [q @queue-ref
result (peek q)
remain (if (= q [])
[]
@xudifsd
xudifsd / t.c
Created August 25, 2013 08:24
compile C and execute "python t.py", press CTRL+C or CTRL+\ when executing python. will print "^Cgot signal" in my laptop
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
int signaled = 0;
void handler(int ignored) {
signaled = 1;
}
@xudifsd
xudifsd / Complete.java
Last active December 29, 2023 11:57
output of smali AST tree
package com.example;
public class Complete {
public int i;
private String s;
public Complete(int i, String s) {
this.i = i;
this.s = s;
}
@xudifsd
xudifsd / ClassLoaderTest.java
Created December 14, 2013 08:17
class loader demo
import java.io.*;
public class ClassLoaderTest {
public static void main(String[] args) throws Exception {
ClassLoader myLoader = new ClassLoader() {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
try {
String fileName = "foo/" + name.replace('.', '/') + ".class";
System.out.println(fileName);
@xudifsd
xudifsd / T.java
Created December 27, 2013 06:09
annotation的使用
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.lang.NoSuchMethodException;
public class T {
public static void main(String[] args) throws Exception {
executeWithAnnotationSupport(T.class, "doSomething");
@xudifsd
xudifsd / mileston.clj
Created June 6, 2014 02:22
we could use `:repeat` key and `<*` syntax in Typed Clojure to do useful annotation
; It's been a long time that Typed Clojure couldn't annotate `hash-map` using
; first class type. Because these functions require even number of arguments,
; and also have some special type constrain on its arguments.
; So we used to check these functions using special handler in checker.
; One of my GSOC goal is to make it possible to type check these kind of
; function without special handler.
; And now I made it. Thanks Sam Tobin-Hochstadt and Asumu Takikawa who inspired
; us.
; You can try it at my fork of Typed Clojure
; https://github.com/xudifsd/core.typed/tree/repeat-support
@xudifsd
xudifsd / defasync.clj
Created December 20, 2014 06:27
defasync
(defmacro defasync [name args & body]
(if config/in-test?
`(defn ~name ~args ~@body)
(let [name-str (str name)]
`(defn ~name ~args
(future
(try
~@body
(catch Exception e#
(log/error (str ~name-str
@xudifsd
xudifsd / before-opt.clj
Created January 3, 2015 05:27
ACL checker before optimization
(defn self-is-active?
[{{:keys [uid]} :params :as req}]
(when-not (nil? uid)
(let [user-id (u/->int uid)
user (users/get-user-by-id user-id)]
(when-not (nil? user)
(not (:inactive user))))))
(defn self-belongs-to-team?
[{{:keys [uid subdomain]} :params :as req}]
@xudifsd
xudifsd / after-opt.clj
Created January 3, 2015 05:28
ACL checker after optimization
(defn self-is-active?
[{{:keys [uid]} :params :as req}]
(when-not (nil? uid)
(let [user-id (u/->int uid)
user (*get-user-by-id* user-id)]
(when-not (nil? user)
(not (:inactive user))))))
(defn self-belongs-to-team?
[{{:keys [uid subdomain]} :params :as req}]