From 11d855c83708eeed967b701db1ea1b057653d5e3 Mon Sep 17 00:00:00 2001 From: David Reiss Date: Wed, 11 Jun 2008 00:59:12 +0000 Subject: [PATCH] micro optimization in thrift_processor git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@666427 13f79535-47bb-0310-9956-ffa450edef68 --- lib/alterl/src/thrift_buffered_transport.erl | 6 ++-- lib/alterl/src/thrift_processor.erl | 37 +++++++++----------- lib/alterl/src/thrift_server.erl | 7 ++-- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/lib/alterl/src/thrift_buffered_transport.erl b/lib/alterl/src/thrift_buffered_transport.erl index c16f26ac..dc11fff2 100644 --- a/lib/alterl/src/thrift_buffered_transport.erl +++ b/lib/alterl/src/thrift_buffered_transport.erl @@ -46,11 +46,9 @@ new(WrappedTransport) -> Else end. - - %%-------------------------------------------------------------------- %% Function: write(Transport, Data) -> ok -%% +%% %% Data = binary() %% %% Description: Writes data into the buffer @@ -68,7 +66,7 @@ flush(Transport) -> %%-------------------------------------------------------------------- %% Function: Read(Transport, Len) -> {ok, Data} -%% +%% %% Data = binary() %% %% Description: Reads data through from the wrapped transoprt diff --git a/lib/alterl/src/thrift_processor.erl b/lib/alterl/src/thrift_processor.erl index aa2666e4..2d2fe372 100644 --- a/lib/alterl/src/thrift_processor.erl +++ b/lib/alterl/src/thrift_processor.erl @@ -1,7 +1,7 @@ %%%------------------------------------------------------------------- %%% File : thrift_processor.erl %%% Author : -%%% Description : +%%% Description : %%% %%% Created : 28 Jan 2008 by %%%------------------------------------------------------------------- @@ -36,7 +36,6 @@ loop(State = #state{in_protocol = IProto, ok end. - handle_function(State = #state{in_protocol = IProto, out_protocol = OProto, handler = Handler, @@ -47,7 +46,8 @@ handle_function(State = #state{in_protocol = IProto, {ok, Params} = thrift_protocol:read(IProto, InParams), try - {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]), + Result = Handler:handle_function(Function, Params), + % {Micro, Result} = better_timer(Handler, handle_function, [Function, Params]), % error_logger:info_msg("Processed ~p(~p) in ~.4fms~n", % [Function, Params, Micro/1000.0]), handle_success(State, Function, Result) @@ -80,17 +80,17 @@ handle_success(State = #state{out_protocol = OProto, service = Service}, Function, Result) -> - ReplyType = Service:function_info(Function, reply_type), + ReplyType = Service:function_info(Function, reply_type), StructName = atom_to_list(Function) ++ "_result", - + case Result of - {reply, ReplyData} -> + {reply, ReplyData} -> Reply = {{struct, [{0, ReplyType}]}, {StructName, ReplyData}}, ok = send_reply(OProto, Function, ?tMessageType_REPLY, Reply); ok when ReplyType == {struct, []} -> ok = send_reply(OProto, Function, ?tMessageType_REPLY, {ReplyType, {StructName}}); - + ok when ReplyType == async_void -> % no reply for async void ok @@ -109,22 +109,17 @@ handle_exception(State = #state{out_protocol = OProto, {struct, XInfo} = ReplySpec, true = is_list(XInfo), - + + % Assuming we had a type1 exception, we'd get: [undefined, Exception, undefined] % e.g.: [{-1, type0}, {-2, type1}, {-3, type2}] - XPairs = [{Fid, Type} || {Fid, {struct, {_Module, Type}}} <- XInfo], - - Mapper = fun({Fid, Type}) -> - case Type of - ExceptionType -> - Exception; - _ -> - undefined + ExceptionList = [case Type of + ExceptionType -> Exception; + _ -> undefined end - end, - % Assuming we had a type1 exception, we get: [undefined, Exception, undefined] - ExceptionList = lists:map(Mapper, XPairs), + || {_Fid, {struct, {_Module, Type}}} <- XInfo], + ExceptionTuple = list_to_tuple([Function | ExceptionList]), - + % Make sure we got at least one defined case lists:all(fun(X) -> X =:= undefined end, ExceptionList) of true -> @@ -144,7 +139,7 @@ handle_unknown_exception(State, Function, Exception) -> handle_error(#state{out_protocol = OProto}, Function, Error) -> Stack = erlang:get_stacktrace(), error_logger:error_msg("~p had an error: ~p~n", [Function, {Error, Stack}]), - + Message = case application:get_env(thrift, exceptions_include_traces) of {ok, true} -> diff --git a/lib/alterl/src/thrift_server.erl b/lib/alterl/src/thrift_server.erl index 8a1704f6..1cc29206 100644 --- a/lib/alterl/src/thrift_server.erl +++ b/lib/alterl/src/thrift_server.erl @@ -1,7 +1,7 @@ %%%------------------------------------------------------------------- %%% File : thrift_server.erl %%% Author : -%%% Description : +%%% Description : %%% %%% Created : 28 Jan 2008 by %%%------------------------------------------------------------------- @@ -30,7 +30,6 @@ start_link(Port, Service, HandlerModule) when is_integer(Port), is_atom(HandlerModule) -> gen_server:start_link({local, ?SERVER}, ?MODULE, {Port, Service, HandlerModule}, []). - %%-------------------------------------------------------------------- %% Function: stop(Pid) -> ok, {error, Reason} %% Description: Stops the server. @@ -157,8 +156,6 @@ set_sockopt(ListenSocket, ClientSocket) -> Error end. - - start_processor(Socket, Service, Handler) -> Server = self(), @@ -170,5 +167,5 @@ start_processor(Socket, Service, Handler) -> {ok, Protocol} = thrift_binary_protocol:new(BufferedTransport), {ok, Protocol, Protocol} end, - + thrift_processor:start(ProtoGen, Service, Handler). -- 2.17.1