Forráskód Böngészése

optimize hloop_init

ithewei 2 éve
szülő
commit
a2428abddd
3 módosított fájl, 29 hozzáadás és 19 törlés
  1. 1 2
      event/hevent.c
  2. 9 0
      event/hevent.h
  3. 19 17
      event/hloop.c

+ 1 - 2
event/hevent.c

@@ -102,8 +102,7 @@ void hio_ready(hio_t* io) {
     io->last_read_hrtime = io->last_write_hrtime = io->loop->cur_hrtime;
     // readbuf
     io->alloced_readbuf = 0;
-    io->readbuf.base = io->loop->readbuf.base;
-    io->readbuf.len = io->loop->readbuf.len;
+    hio_use_loop_readbuf(io);
     io->readbuf.head = io->readbuf.tail = 0;
     io->read_flags = 0;
     io->read_until_length = 0;

+ 9 - 0
event/hevent.h

@@ -219,6 +219,15 @@ void hio_del_write_timer(hio_t* io);
 void hio_del_keepalive_timer(hio_t* io);
 void hio_del_heartbeat_timer(hio_t* io);
 
+static inline void hio_use_loop_readbuf(hio_t* io) {
+    hloop_t* loop = io->loop;
+    if (loop->readbuf.len == 0) {
+        loop->readbuf.len = HLOOP_READ_BUFSIZE;
+        HV_ALLOC(loop->readbuf.base, loop->readbuf.len);
+    }
+    io->readbuf.base = loop->readbuf.base;
+    io->readbuf.len  = loop->readbuf.len;
+}
 static inline bool hio_is_loop_readbuf(hio_t* io) {
     return io->readbuf.base == io->loop->readbuf.base;
 }

+ 19 - 17
event/hloop.c

@@ -247,7 +247,7 @@ static int hloop_create_eventfds(hloop_t* loop) {
         return -1;
     }
 #endif
-    hio_t* io = hread(loop, loop->eventfds[EVENTFDS_READ_INDEX], loop->readbuf.base, loop->readbuf.len, eventfd_read_cb);
+    hio_t* io = hread(loop, loop->eventfds[EVENTFDS_READ_INDEX], NULL, 0, eventfd_read_cb);
     io->priority = HEVENT_HIGH_PRIORITY;
     ++loop->intern_nevents;
     return 0;
@@ -298,6 +298,9 @@ void hloop_post_event(hloop_t* loop, hevent_t* ev) {
         hloge("hloop_post_event failed!");
         goto unlock;
     }
+    if (loop->custom_events.maxsize == 0) {
+        event_queue_init(&loop->custom_events, CUSTOM_EVENT_QUEUE_INIT_SIZE);
+    }
     event_queue_push_back(&loop->custom_events, ev);
 unlock:
     hmutex_unlock(&loop->custom_events_mutex);
@@ -324,18 +327,19 @@ static void hloop_init(hloop_t* loop) {
     heap_init(&loop->realtimers, timers_compare);
 
     // ios
-    io_array_init(&loop->ios, IO_ARRAY_INIT_SIZE);
+    // NOTE: io_array_init when hio_get -> io_array_resize
+    // io_array_init(&loop->ios, IO_ARRAY_INIT_SIZE);
 
     // readbuf
-    loop->readbuf.len = HLOOP_READ_BUFSIZE;
-    HV_ALLOC(loop->readbuf.base, loop->readbuf.len);
+    // NOTE: alloc readbuf when hio_use_loop_readbuf
+    // loop->readbuf.len = HLOOP_READ_BUFSIZE;
+    // HV_ALLOC(loop->readbuf.base, loop->readbuf.len);
 
-    // iowatcher
-    iowatcher_init(loop);
+    // NOTE: iowatcher_init when hio_add -> iowatcher_add_event
+    // iowatcher_init(loop);
 
     // custom_events
     hmutex_init(&loop->custom_events_mutex);
-    event_queue_init(&loop->custom_events, CUSTOM_EVENT_QUEUE_INIT_SIZE);
     // NOTE: hloop_create_eventfds when hloop_post_event or hloop_run
     loop->eventfds[0] = loop->eventfds[1] = -1;
 
@@ -703,13 +707,17 @@ const char* hio_engine() {
 #endif
 }
 
-hio_t* hio_get(hloop_t* loop, int fd) {
+static inline hio_t* __hio_get(hloop_t* loop, int fd) {
     if (fd >= loop->ios.maxsize) {
         int newsize = ceil2e(fd);
+        newsize = MAX(newsize, IO_ARRAY_INIT_SIZE);
         io_array_resize(&loop->ios, newsize > fd ? newsize : 2*fd);
     }
+    return loop->ios.ptr[fd];
+}
 
-    hio_t* io = loop->ios.ptr[fd];
+hio_t* hio_get(hloop_t* loop, int fd) {
+    hio_t* io = __hio_get(loop, fd);
     if (io == NULL) {
         HV_ALLOC_SIZEOF(io);
         hio_init(io);
@@ -735,22 +743,16 @@ void hio_detach(hio_t* io) {
 
 void hio_attach(hloop_t* loop, hio_t* io) {
     int fd = io->fd;
-    if (fd >= loop->ios.maxsize) {
-        int newsize = ceil2e(fd);
-        io_array_resize(&loop->ios, newsize > fd ? newsize : 2*fd);
-    }
-
     // NOTE: hio was not freed for reused when closed, but attached hio can't be reused,
     // so we need to free it if fd exists to avoid memory leak.
-    hio_t* preio = loop->ios.ptr[fd];
+    hio_t* preio = __hio_get(loop, fd);
     if (preio != NULL && preio != io) {
         hio_free(preio);
     }
 
     io->loop = loop;
     // NOTE: use new_loop readbuf
-    io->readbuf.base = loop->readbuf.base;
-    io->readbuf.len = loop->readbuf.len;
+    hio_use_loop_readbuf(io);
     loop->ios.ptr[fd] = io;
 }