Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-3797

Cache commonly used strings in C extensions

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 4.5
    • Affects Version/s: None
    • Component/s: None
    • None

      As we learned in PYTHON-3717 and PYTHON-3729, caching commonly used strings the C extensions can yield a big performance improvements. Let's cache the strings used in other places:

      $ git grep 'String('
      ...
      bson/_cbsonmodule.c:1540:            PyObject* _id = PyDict_GetItemString(dict, "_id");
      bson/_cbsonmodule.c:1547:        } else if (PyMapping_HasKeyString(dict, "_id")) {
      bson/_cbsonmodule.c:1548:            PyObject* _id = PyMapping_GetItemString(dict, "_id");
      bson/_cbsonmodule.c:1623:        raw_bson_document_bytes_obj = PyObject_GetAttrString(dict, "raw");
      bson/_cbsonmodule.c:1664:    if (PyMapping_HasKeyString(value, "$ref") && PyMapping_HasKeyString(value, "$id")) { /* DBRef */
      bson/_cbsonmodule.c:1665:        ref = PyMapping_GetItemString(value, "$ref");
      bson/_cbsonmodule.c:1670:        id = PyMapping_GetItemString(value, "$id");
      bson/_cbsonmodule.c:1676:        if (PyMapping_HasKeyString(value, "$db")) {
      bson/_cbsonmodule.c:1677:            database = PyMapping_GetItemString(value, "$db");
      bson/_cbsonmodule.c:1693:        PyMapping_DelItemString(value, "$ref");
      bson/_cbsonmodule.c:1694:        PyMapping_DelItemString(value, "$id");
      bson/_cbsonmodule.c:1696:            PyMapping_DelItemString(value, "$db");
      bson/_cbsonmodule.c:2105:            replace = PyObject_GetAttrString(naive, "replace");
      bson/_cbsonmodule.c:2122:            if (!utc_type || PyDict_SetItemString(kwargs, "tzinfo", utc_type) == -1) {
      bson/_cbsonmodule.c:2140:                astimezone = PyObject_GetAttrString(value, "astimezone");
      

      PyDict_GetItemString can be replaced with PyDict_GetItem, PyMapping_DelItemString with PyMapping_DelItem, etc...

        1. screenshot-1.png
          167 kB
          Iris Ho

            Assignee:
            iris.ho@mongodb.com Iris Ho
            Reporter:
            shane.harvey@mongodb.com Shane Harvey
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: