Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up PHP loading by lazy libpostal loading #30

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions php_postal.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ ZEND_DECLARE_MODULE_GLOBALS(postal)
/* True global resources - no need for thread safety here */
static int le_postal;

// Lazy libpostal init
bool lazy_libpostal_init();

// libpostal initialization flag
static bool libpostal_is_inited = false;

zend_class_entry expand_ce;
zend_class_entry *expand_class_entry_ptr;
Expand Down Expand Up @@ -120,9 +125,10 @@ PHP_MINIT_FUNCTION(postal)
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
if (!libpostal_setup() || !libpostal_setup_language_classifier() || !libpostal_setup_parser()) {
return FAILURE;
}

if ( !libpostal_setup() ) {
return FAILURE;
}

REGISTER_LONG_CONSTANT("ADDRESS_NONE", LIBPOSTAL_ADDRESS_NONE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("ADDRESS_ANY", LIBPOSTAL_ADDRESS_ANY, CONST_CS | CONST_PERSISTENT);
Expand Down Expand Up @@ -158,9 +164,11 @@ PHP_MSHUTDOWN_FUNCTION(postal)
UNREGISTER_INI_ENTRIES();
*/

libpostal_teardown();
libpostal_teardown_language_classifier();
libpostal_teardown_parser();
if( libpostal_is_inited ) {
libpostal_teardown();
libpostal_teardown_language_classifier();
libpostal_teardown_parser();
}

return SUCCESS;
}
Expand Down Expand Up @@ -203,6 +211,10 @@ PHP_METHOD(Expand, expand_address) {
return;
}

if( !lazy_libpostal_init() ) {
RETURN_NULL();
}

libpostal_normalize_options_t options = libpostal_get_default_options();

size_t num_languages = 0;
Expand Down Expand Up @@ -593,6 +605,10 @@ PHP_METHOD(Parser, parse_address) {
RETURN_NULL();
}

if( !lazy_libpostal_init() ) {
RETURN_NULL();
}

libpostal_address_parser_options_t options = libpostal_get_address_parser_default_options();

char *language = NULL;
Expand Down Expand Up @@ -683,6 +699,20 @@ PHP_METHOD(Parser, parse_address) {

}


// Lazy libpostal init
bool lazy_libpostal_init() {

if( !libpostal_is_inited ) {

if ( libpostal_setup_language_classifier() && libpostal_setup_parser()) {
libpostal_is_inited = true;
}
}

return libpostal_is_inited;
}

/*
* Local variables:
* tab-width: 4
Expand Down