why first mapping consumes a lot of time

423 views
Skip to first unread message

liubangc...@gmail.com

unread,
Feb 20, 2013, 7:03:36 AM2/20/13
to dozer-...@googlegroups.com
first I new a DozerBeanMapper instance, code is like this:

public class MapConfig {
private static MapConfig instance = new MapConfig();
private  Mapper mapper;
private MapConfig(){
init();
}
public  Mapper getMapper(){
return mapper;
}
public static MapConfig getInstance(){
return instance;
}
private void init(){
// mapper = DozerBeanMapperSingletonWrapper.getInstance();
mapper = new DozerBeanMapper();
List<String> myMappingFiles = new ArrayList<String>();
myMappingFiles.add("dozer-bean-mappings.xml");
DozerBeanMapper dbm = (DozerBeanMapper)mapper;
dbm.setMappingFiles(myMappingFiles);
}
}


then do mapping:

Mapper mapper = MapConfig.getInstance().getMapper();
Meeting m = new Meeting();
m.setTitle("my meeting");
m.setRecurrenceFlag(true);
Meeting2 m2 = new Meeting2();
long startTime = System.nanoTime();
mi.setTopic(m.getTitle());
mi.setRecurrence(m.isRecurrenceFlag());
long endTime = System.nanoTime();
System.out.println("set mi value = " + (endTime - startTime) / 1000000.0 + "ms");
long startTime3 = System.nanoTime();
mapper.map(m, m2);
long endTime3 = System.nanoTime();
System.out.println("map m2 value = " + (endTime3 - startTime3) / 1000000.0 +"ms");

it consumes 495.911581ms first time, when i do it again, it consumes 0.4578ms

So, what's the reason, I want the first time is fast.

Dmitry Buzdin

unread,
Feb 20, 2013, 9:25:07 AM2/20/13
to dozer-...@googlegroups.com
Hi,

When you do your first mapping Dozer builds metadata about classes you are mapping and is also performing some internal initialisation.
This involves a lot of class introspection logic and putting results into in-memory cache.

When you are creating a new DozerMapper instance try to do one call to map() before using it, just to initialise everything.

Regards,
Dmitry

среда, 20 февраля 2013 г., 14:03:36 UTC+2 пользователь liubangc...@gmail.com написал:

liubangc...@gmail.com

unread,
Feb 20, 2013, 7:56:56 PM2/20/13
to dozer-...@googlegroups.com
Thanks, So please help to tell me how I can make the first mapping fast.

I tried do first mapping in init() method, just like this:
dbm.map(new Object(),Object.class);

then when I do really mapping, it became second mapping, I think it'll be faster,and the result is 13.3473ms, it's really faster,
but I have two concern:
1. the result is still slow, 13ms is unacceptable.
2. the mapping "dbm.map(new Object(),Object.class)", its purpose is just reduce mapping time, it doesn't make sense.

could you help to show some code sample?

在 2013年2月20日星期三UTC+8下午10时25分07秒,Dmitry Buzdin写道:

Dmitry Buzdin

unread,
Feb 21, 2013, 4:10:38 AM2/21/13
to liubangc...@gmail.com, dozer-...@googlegroups.com
Mapping dbm.map(new Object(),Object.class); solves only part of problems.

You should map the exact types you are going to map later. Beans could be empty of course.

So for proper warm-up you should run Dozer with all possible pairs of Classes. It would be even better to warm-up JVM and do the mapping few 100 or 1000 times before starting accepting requests. But this goes to the topic of general application warm-up theory.

Dmitry

--
You received this message because you are subscribed to the Google Groups "Dozer Mapper" group.
To unsubscribe from this group and stop receiving emails from it, send an email to dozer-mapper...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

liubangc...@gmail.com

unread,
Feb 21, 2013, 8:29:33 PM2/21/13
to dozer-...@googlegroups.com, liubangc...@gmail.com
Got it, Thanks, I see

Thanks for your kind help

在 2013年2月21日星期四UTC+8下午5时10分38秒,Dmitry Buzdin写道:
Reply all
Reply to author
Forward
0 new messages