codegen: implement rest efficiently